configurable dynamic stream concurrency scalar

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-23 04:32:28 +00:00 committed by strawberry
parent b195107053
commit 7a6d657558
9 changed files with 144 additions and 20 deletions

View file

@ -7,9 +7,7 @@ use futures::{
Future,
};
use super::ReadyExt;
const WIDTH: usize = 32;
use super::{automatic_width, ReadyExt};
/// Concurrency extensions to augment futures::StreamExt. broad_ combinators
/// produce out-of-order
@ -95,7 +93,7 @@ where
Fut: Future<Output = bool> + Send,
{
self.map(f)
.buffer_unordered(n.into().unwrap_or(WIDTH))
.buffer_unordered(n.into().unwrap_or_else(automatic_width))
.ready_all(identity)
}
@ -107,7 +105,7 @@ where
Fut: Future<Output = bool> + Send,
{
self.map(f)
.buffer_unordered(n.into().unwrap_or(WIDTH))
.buffer_unordered(n.into().unwrap_or_else(automatic_width))
.ready_any(identity)
}
@ -120,7 +118,7 @@ where
U: Send,
{
self.map(f)
.buffer_unordered(n.into().unwrap_or(WIDTH))
.buffer_unordered(n.into().unwrap_or_else(automatic_width))
.ready_filter_map(identity)
}
@ -132,6 +130,7 @@ where
Fut: Future<Output = U> + Send,
U: Send,
{
self.map(f).buffer_unordered(n.into().unwrap_or(WIDTH))
self.map(f)
.buffer_unordered(n.into().unwrap_or_else(automatic_width))
}
}

View file

@ -19,3 +19,32 @@ pub use tools::Tools;
pub use try_broadband::TryBroadbandExt;
pub use try_ready::TryReadyExt;
pub use wideband::WidebandExt;
/// Stream concurrency factor; this is a live value.
static WIDTH: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(32);
/// Practicable limits on the stream width
pub const WIDTH_LIMIT: (usize, usize) = (1, 1024);
/// 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) {
use std::sync::atomic::Ordering;
let width = width.clamp(WIDTH_LIMIT.0, WIDTH_LIMIT.1);
(WIDTH.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 {
use std::sync::atomic::Ordering;
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
}

View file

@ -2,10 +2,9 @@
use futures::{TryFuture, TryStream, TryStreamExt};
use super::automatic_width;
use crate::Result;
const WIDTH: usize = 32;
/// Concurrency extensions to augment futures::TryStreamExt. broad_ combinators
/// produce out-of-order
pub trait TryBroadbandExt<T, E>
@ -49,6 +48,6 @@ where
Fut: TryFuture<Ok = U, Error = E, Output = Result<U, E>> + Send,
{
self.map_ok(f)
.try_buffer_unordered(n.into().unwrap_or(WIDTH))
.try_buffer_unordered(n.into().unwrap_or_else(automatic_width))
}
}

View file

@ -7,9 +7,7 @@ use futures::{
Future,
};
use super::ReadyExt;
const WIDTH: usize = 32;
use super::{automatic_width, ReadyExt};
/// Concurrency extensions to augment futures::StreamExt. wideband_ combinators
/// produce in-order.
@ -66,7 +64,7 @@ where
U: Send,
{
self.map(f)
.buffered(n.into().unwrap_or(WIDTH))
.buffered(n.into().unwrap_or_else(automatic_width))
.ready_filter_map(identity)
}
@ -78,6 +76,7 @@ where
Fut: Future<Output = U> + Send,
U: Send,
{
self.map(f).buffered(n.into().unwrap_or(WIDTH))
self.map(f)
.buffered(n.into().unwrap_or_else(automatic_width))
}
}