sum_tree: Spawn less tasks in SumTree::from_iter_async (#41793)

Release Notes:

- N/A *or* Added/Fixed/Improved ...
This commit is contained in:
Lukas Wirth
2025-11-03 10:02:31 +00:00
committed by GitHub
parent dc503e9975
commit f2b539598e
4 changed files with 48 additions and 42 deletions
+7 -15
View File
@@ -323,21 +323,13 @@ impl Rope {
const PARALLEL_THRESHOLD: usize = 4 * (2 * sum_tree::TREE_BASE);
if new_chunks.len() >= PARALLEL_THRESHOLD {
let cx2 = executor.clone();
executor
.scoped(|scope| {
// SAFETY: transmuting to 'static is safe because the future is scoped
// and the underlying string data cannot go out of scope because dropping the scope
// will wait for the task to finish
let new_chunks =
unsafe { std::mem::transmute::<Vec<&str>, Vec<&'static str>>(new_chunks) };
let async_extend = self
.chunks
.async_extend(new_chunks.into_iter().map(Chunk::new), cx2);
scope.spawn(async_extend);
})
// SAFETY: transmuting to 'static is sound here. We block on the future making use of this
// and we know that the result of this computation is not stashing the static reference
// away.
let new_chunks =
unsafe { std::mem::transmute::<Vec<&str>, Vec<&'static str>>(new_chunks) };
self.chunks
.async_extend(new_chunks.into_iter().map(Chunk::new), executor)
.await;
} else {
self.chunks