Skip to content

Commit 2760e4b

Browse files
udoprogcramertj
authored andcommitted
Optimize future::select_all by not preserving order
1 parent 0805eac commit 2760e4b

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

futures-util/src/future/select_all.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ impl<Fut: Unpin> Unpin for SelectAll<Fut> {}
2121
/// completion the item resolved will be returned, along with the index of the
2222
/// future that was ready and the list of all the remaining futures.
2323
///
24+
/// There are no guarantees provided on the order of the list with the remaining
25+
/// futures. They might be swapped around, reversed, or completely random.
26+
///
2427
/// This function is only available when the `std` or `alloc` feature of this
2528
/// library is activated, and it is activated by default.
2629
///
@@ -50,7 +53,7 @@ impl<Fut: Future + Unpin> Future for SelectAll<Fut> {
5053
});
5154
match item {
5255
Some((idx, res)) => {
53-
self.inner.remove(idx);
56+
let _ = self.inner.swap_remove(idx);
5457
let rest = mem::replace(&mut self.inner, Vec::new());
5558
Poll::Ready((res, idx, rest))
5659
}

futures/tests/select_all.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use futures::executor::block_on;
22
use futures::future::{ready, select_all};
3+
use std::collections::HashSet;
34

45
#[test]
56
fn smoke() {
@@ -9,17 +10,20 @@ fn smoke() {
910
ready(3),
1011
];
1112

13+
let mut c = vec![1, 2, 3].into_iter().collect::<HashSet<_>>();
14+
1215
let (i, idx, v) = block_on(select_all(v));
13-
assert_eq!(i, 1);
16+
assert!(c.remove(&i));
1417
assert_eq!(idx, 0);
1518

1619
let (i, idx, v) = block_on(select_all(v));
17-
assert_eq!(i, 2);
20+
assert!(c.remove(&i));
1821
assert_eq!(idx, 0);
1922

2023
let (i, idx, v) = block_on(select_all(v));
21-
assert_eq!(i, 3);
24+
assert!(c.remove(&i));
2225
assert_eq!(idx, 0);
2326

27+
assert!(c.is_empty());
2428
assert!(v.is_empty());
2529
}

0 commit comments

Comments
 (0)