Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix insertion performance issues for our Group structures #168

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/future/future_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct FutureGroup<F> {
wakers: WakerVec,
states: PollVec,
keys: BTreeSet<usize>,
capacity: usize,
}

impl<T: Debug> Debug for FutureGroup<T> {
Expand Down Expand Up @@ -108,6 +109,7 @@ impl<F> FutureGroup<F> {
wakers: WakerVec::new(capacity),
states: PollVec::new(capacity),
keys: BTreeSet::new(),
capacity,
}
}

Expand Down Expand Up @@ -142,7 +144,7 @@ impl<F> FutureGroup<F> {
/// # let group: FutureGroup<usize> = group;
/// ```
pub fn capacity(&self) -> usize {
self.futures.capacity()
self.capacity
}

/// Returns true if there are no futures currently active in the group.
Expand Down Expand Up @@ -231,9 +233,13 @@ impl<F: Future> FutureGroup<F> {

// If our slab allocated more space we need to
// update our tracking structures along with it.
let max_len = self.capacity().max(index);
self.wakers.resize(max_len);
self.states.resize(max_len);
let capacity = self.capacity();
let max_len = capacity.max(index + 1);
if max_len > capacity {
self.wakers.resize(max_len);
self.states.resize(max_len);
self.capacity = max_len;
}

// Set the corresponding state
self.states[index].set_pending();
Expand Down Expand Up @@ -421,4 +427,14 @@ mod test {
assert!(group.is_empty());
});
}

#[test]
fn insert_many() {
futures_lite::future::block_on(async {
let mut group = FutureGroup::new();
for _ in 0..100 {
group.insert(future::ready(2));
}
});
}
}
24 changes: 20 additions & 4 deletions src/stream/stream_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct StreamGroup<S> {
states: PollVec,
keys: BTreeSet<usize>,
key_removal_queue: SmallVec<[usize; 10]>,
capacity: usize,
}

impl<T: Debug> Debug for StreamGroup<T> {
Expand Down Expand Up @@ -108,6 +109,7 @@ impl<S> StreamGroup<S> {
states: PollVec::new(capacity),
keys: BTreeSet::new(),
key_removal_queue: smallvec![],
capacity,
}
}

Expand Down Expand Up @@ -141,7 +143,7 @@ impl<S> StreamGroup<S> {
/// # let group: StreamGroup<usize> = group;
/// ```
pub fn capacity(&self) -> usize {
self.streams.capacity()
self.capacity
}

/// Returns true if there are no futures currently active in the group.
Expand Down Expand Up @@ -230,9 +232,13 @@ impl<S: Stream> StreamGroup<S> {

// If our slab allocated more space we need to
// update our tracking structures along with it.
let max_len = self.capacity().max(index);
self.wakers.resize(max_len);
self.states.resize(max_len);
let capacity = self.capacity();
let max_len = capacity.max(index + 1);
if max_len > capacity {
self.wakers.resize(max_len);
self.states.resize(max_len);
self.capacity = max_len;
}

// Set the corresponding state
self.states[index].set_pending();
Expand Down Expand Up @@ -440,4 +446,14 @@ mod test {
assert!(group.is_empty());
});
}

#[test]
fn insert_many() {
futures_lite::future::block_on(async {
let mut group = StreamGroup::new();
for _ in 0..100 {
group.insert(stream::once(2));
}
});
}
}
Loading