continuwuity/src/core/utils/stream/band.rs
Jason Volk 2259e2c82f batch queries to maximize throughput
query-side streams for first level of callsites

Signed-off-by: Jason Volk <jason@zemos.net>
2025-01-01 23:28:01 -05:00

50 lines
2.1 KiB
Rust

use std::sync::atomic::{AtomicUsize, Ordering};
/// Stream concurrency factor; this is a live value.
static WIDTH: AtomicUsize = AtomicUsize::new(32);
/// Stream throughput amplifier; this is a live value.
static AMPLIFICATION: AtomicUsize = AtomicUsize::new(1024);
/// Practicable limits on the stream width.
pub const WIDTH_LIMIT: (usize, usize) = (1, 1024);
/// Practicable limits on the stream amplifier.
pub const AMPLIFICATION_LIMIT: (usize, usize) = (32, 32768);
/// Sets the live concurrency factor. The first return value is the previous
/// width which was replaced. The second return value is the value which was set
/// after any applied limits.
pub fn set_width(width: usize) -> (usize, usize) {
let width = width.clamp(WIDTH_LIMIT.0, WIDTH_LIMIT.1);
(WIDTH.swap(width, Ordering::Relaxed), width)
}
/// Sets the live concurrency amplification. The first return value is the
/// previous width which was replaced. The second return value is the value
/// which was set after any applied limits.
pub fn set_amplification(width: usize) -> (usize, usize) {
let width = width.clamp(AMPLIFICATION_LIMIT.0, AMPLIFICATION_LIMIT.1);
(AMPLIFICATION.swap(width, Ordering::Relaxed), width)
}
/// Used by stream operations where the concurrency factor hasn't been manually
/// supplied by the caller (most uses). Instead we provide a default value which
/// is adjusted at startup for the specific system and also dynamically.
#[inline]
pub fn automatic_width() -> usize {
let width = WIDTH.load(Ordering::Relaxed);
debug_assert!(width >= WIDTH_LIMIT.0, "WIDTH should not be zero");
debug_assert!(width <= WIDTH_LIMIT.1, "WIDTH is probably too large");
width
}
/// Used by stream operations where the amplification hasn't been manually
/// supplied by the caller. Instead we provide a computed value.
#[inline]
pub fn automatic_amplification() -> usize {
let amplification = AMPLIFICATION.load(Ordering::Relaxed);
debug_assert!(amplification >= AMPLIFICATION_LIMIT.0, "amplification is too low");
debug_assert!(amplification <= AMPLIFICATION_LIMIT.1, "amplification is too high");
amplification
}