This causes the background executor to hang Release Notes: - N/A *or* Added/Fixed/Improved ...
This commit is contained in:
+57
-110
@@ -3,30 +3,17 @@ mod tree_map;
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
pub use cursor::{Cursor, FilterCursor, Iter};
|
||||
use futures::{StreamExt, stream};
|
||||
use futures_lite::future::yield_now;
|
||||
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator as _};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem;
|
||||
use std::{cmp::Ordering, fmt, iter::FromIterator, sync::Arc};
|
||||
pub use tree_map::{MapSeekTarget, TreeMap, TreeSet};
|
||||
|
||||
#[cfg(all(test, not(rust_analyzer)))]
|
||||
#[cfg(test)]
|
||||
pub const TREE_BASE: usize = 2;
|
||||
#[cfg(not(all(test, not(rust_analyzer))))]
|
||||
#[cfg(not(test))]
|
||||
pub const TREE_BASE: usize = 6;
|
||||
|
||||
pub trait BackgroundSpawn {
|
||||
type Task<R>: Future<Output = R> + Send + Sync
|
||||
where
|
||||
R: Send + Sync;
|
||||
fn background_spawn<R>(
|
||||
&self,
|
||||
future: impl Future<Output = R> + Send + Sync + 'static,
|
||||
) -> Self::Task<R>
|
||||
where
|
||||
R: Send + Sync + 'static;
|
||||
}
|
||||
|
||||
/// An item that can be stored in a [`SumTree`]
|
||||
///
|
||||
/// Must be summarized by a type that implements [`Summary`]
|
||||
@@ -311,85 +298,62 @@ impl<T: Item> SumTree<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn from_iter_async<I, S>(iter: I, spawn: S) -> Self
|
||||
pub fn from_par_iter<I, Iter>(iter: I, cx: <T::Summary as Summary>::Context<'_>) -> Self
|
||||
where
|
||||
T: 'static + Send + Sync,
|
||||
for<'a> T::Summary: Summary<Context<'a> = ()> + Send + Sync,
|
||||
S: BackgroundSpawn,
|
||||
I: IntoIterator<Item = T, IntoIter: ExactSizeIterator>,
|
||||
I: IntoParallelIterator<Iter = Iter>,
|
||||
Iter: IndexedParallelIterator<Item = T>,
|
||||
T: Send + Sync,
|
||||
T::Summary: Send + Sync,
|
||||
for<'a> <T::Summary as Summary>::Context<'a>: Sync,
|
||||
{
|
||||
let iter = iter.into_iter();
|
||||
let num_leaves = iter.len().div_ceil(2 * TREE_BASE);
|
||||
|
||||
if num_leaves == 0 {
|
||||
return Self::new(());
|
||||
}
|
||||
|
||||
let mut nodes = stream::iter(iter)
|
||||
.chunks(num_leaves.div_ceil(4))
|
||||
.map(|chunk| async move {
|
||||
let mut chunk = chunk.into_iter();
|
||||
let mut leaves = vec![];
|
||||
loop {
|
||||
let items: ArrayVec<T, { 2 * TREE_BASE }> =
|
||||
chunk.by_ref().take(2 * TREE_BASE).collect();
|
||||
if items.is_empty() {
|
||||
break;
|
||||
}
|
||||
let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
|
||||
items.iter().map(|item| item.summary(())).collect();
|
||||
let mut summary = item_summaries[0].clone();
|
||||
for item_summary in &item_summaries[1..] {
|
||||
<T::Summary as Summary>::add_summary(&mut summary, item_summary, ());
|
||||
}
|
||||
leaves.push(SumTree(Arc::new(Node::Leaf {
|
||||
summary,
|
||||
items,
|
||||
item_summaries,
|
||||
})));
|
||||
yield_now().await;
|
||||
let mut nodes = iter
|
||||
.into_par_iter()
|
||||
.chunks(2 * TREE_BASE)
|
||||
.map(|items| {
|
||||
let items: ArrayVec<T, { 2 * TREE_BASE }> = items.into_iter().collect();
|
||||
let item_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> =
|
||||
items.iter().map(|item| item.summary(cx)).collect();
|
||||
let mut summary = item_summaries[0].clone();
|
||||
for item_summary in &item_summaries[1..] {
|
||||
<T::Summary as Summary>::add_summary(&mut summary, item_summary, cx);
|
||||
}
|
||||
leaves
|
||||
SumTree(Arc::new(Node::Leaf {
|
||||
summary,
|
||||
items,
|
||||
item_summaries,
|
||||
}))
|
||||
})
|
||||
.map(|future| spawn.background_spawn(future))
|
||||
.buffered(4)
|
||||
.flat_map(|it| stream::iter(it.into_iter()))
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut height = 0;
|
||||
while nodes.len() > 1 {
|
||||
height += 1;
|
||||
let current_nodes = mem::take(&mut nodes);
|
||||
nodes = stream::iter(current_nodes)
|
||||
nodes = nodes
|
||||
.into_par_iter()
|
||||
.chunks(2 * TREE_BASE)
|
||||
.map(|chunk| {
|
||||
spawn.background_spawn(async move {
|
||||
let child_trees: ArrayVec<SumTree<T>, { 2 * TREE_BASE }> =
|
||||
chunk.into_iter().collect();
|
||||
let child_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> = child_trees
|
||||
.iter()
|
||||
.map(|child_tree| child_tree.summary().clone())
|
||||
.collect();
|
||||
let mut summary = child_summaries[0].clone();
|
||||
for child_summary in &child_summaries[1..] {
|
||||
<T::Summary as Summary>::add_summary(&mut summary, child_summary, ());
|
||||
}
|
||||
SumTree(Arc::new(Node::Internal {
|
||||
height,
|
||||
summary,
|
||||
child_summaries,
|
||||
child_trees,
|
||||
}))
|
||||
})
|
||||
.map(|child_nodes| {
|
||||
let child_trees: ArrayVec<SumTree<T>, { 2 * TREE_BASE }> =
|
||||
child_nodes.into_iter().collect();
|
||||
let child_summaries: ArrayVec<T::Summary, { 2 * TREE_BASE }> = child_trees
|
||||
.iter()
|
||||
.map(|child_tree| child_tree.summary().clone())
|
||||
.collect();
|
||||
let mut summary = child_summaries[0].clone();
|
||||
for child_summary in &child_summaries[1..] {
|
||||
<T::Summary as Summary>::add_summary(&mut summary, child_summary, cx);
|
||||
}
|
||||
SumTree(Arc::new(Node::Internal {
|
||||
height,
|
||||
summary,
|
||||
child_summaries,
|
||||
child_trees,
|
||||
}))
|
||||
})
|
||||
.buffered(4)
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
.collect::<Vec<_>>();
|
||||
}
|
||||
|
||||
if nodes.is_empty() {
|
||||
Self::new(())
|
||||
Self::new(cx)
|
||||
} else {
|
||||
debug_assert_eq!(nodes.len(), 1);
|
||||
nodes.pop().unwrap()
|
||||
@@ -633,15 +597,15 @@ impl<T: Item> SumTree<T> {
|
||||
self.append(Self::from_iter(iter, cx), cx);
|
||||
}
|
||||
|
||||
pub async fn async_extend<S, I>(&mut self, iter: I, spawn: S)
|
||||
pub fn par_extend<I, Iter>(&mut self, iter: I, cx: <T::Summary as Summary>::Context<'_>)
|
||||
where
|
||||
S: BackgroundSpawn,
|
||||
I: IntoIterator<Item = T, IntoIter: ExactSizeIterator>,
|
||||
T: 'static + Send + Sync,
|
||||
for<'b> T::Summary: Summary<Context<'b> = ()> + Send + Sync,
|
||||
I: IntoParallelIterator<Iter = Iter>,
|
||||
Iter: IndexedParallelIterator<Item = T>,
|
||||
T: Send + Sync,
|
||||
T::Summary: Send + Sync,
|
||||
for<'a> <T::Summary as Summary>::Context<'a>: Sync,
|
||||
{
|
||||
let other = Self::from_iter_async(iter, spawn);
|
||||
self.append(other.await, ());
|
||||
self.append(Self::from_par_iter(iter, cx), cx);
|
||||
}
|
||||
|
||||
pub fn push(&mut self, item: T, cx: <T::Summary as Summary>::Context<'_>) {
|
||||
@@ -1106,23 +1070,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_random() {
|
||||
struct NoSpawn;
|
||||
impl BackgroundSpawn for NoSpawn {
|
||||
type Task<R>
|
||||
= std::pin::Pin<Box<dyn Future<Output = R> + Sync + Send>>
|
||||
where
|
||||
R: Send + Sync;
|
||||
fn background_spawn<R>(
|
||||
&self,
|
||||
future: impl Future<Output = R> + Send + Sync + 'static,
|
||||
) -> Self::Task<R>
|
||||
where
|
||||
R: Send + Sync + 'static,
|
||||
{
|
||||
Box::pin(future)
|
||||
}
|
||||
}
|
||||
|
||||
let mut starting_seed = 0;
|
||||
if let Ok(value) = std::env::var("SEED") {
|
||||
starting_seed = value.parse().expect("invalid SEED variable");
|
||||
@@ -1140,7 +1087,7 @@ mod tests {
|
||||
|
||||
let rng = &mut rng;
|
||||
let mut tree = SumTree::<u8>::default();
|
||||
let count = rng.random_range(0..128);
|
||||
let count = rng.random_range(0..10);
|
||||
if rng.random() {
|
||||
tree.extend(rng.sample_iter(StandardUniform).take(count), ());
|
||||
} else {
|
||||
@@ -1148,13 +1095,13 @@ mod tests {
|
||||
.sample_iter(StandardUniform)
|
||||
.take(count)
|
||||
.collect::<Vec<_>>();
|
||||
pollster::block_on(tree.async_extend(items, NoSpawn));
|
||||
tree.par_extend(items, ());
|
||||
}
|
||||
|
||||
for _ in 0..num_operations {
|
||||
let splice_end = rng.random_range(0..tree.extent::<Count>(()).0 + 1);
|
||||
let splice_start = rng.random_range(0..splice_end + 1);
|
||||
let count = rng.random_range(0..128);
|
||||
let count = rng.random_range(0..10);
|
||||
let tree_end = tree.extent::<Count>(());
|
||||
let new_items = rng
|
||||
.sample_iter(StandardUniform)
|
||||
@@ -1170,7 +1117,7 @@ mod tests {
|
||||
if rng.random() {
|
||||
new_tree.extend(new_items, ());
|
||||
} else {
|
||||
pollster::block_on(new_tree.async_extend(new_items, NoSpawn));
|
||||
new_tree.par_extend(new_items, ());
|
||||
}
|
||||
cursor.seek(&Count(splice_end), Bias::Right);
|
||||
new_tree.append(cursor.slice(&tree_end, Bias::Right), ());
|
||||
|
||||
Reference in New Issue
Block a user