Skip to content

Commit

Permalink
Use retain to clear out old streams (#2144)
Browse files Browse the repository at this point in the history
This is simpler code overall.  It might even be faster.  It certainly
avoids an allocation.
  • Loading branch information
martinthomson authored Oct 2, 2024
1 parent bafc244 commit 824d7c4
Showing 1 changed file with 5 additions and 18 deletions.
23 changes: 5 additions & 18 deletions neqo-transport/src/recv_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,19 @@ impl RecvStreams {
}

pub fn clear_terminal(&mut self, send_streams: &SendStreams, role: Role) -> (u64, u64) {
let recv_to_remove = self
.streams
.iter()
.filter_map(|(id, stream)| {
// Remove all streams for which the receiving is done (or aborted).
// But only if they are unidirectional, or we have finished sending.
if stream.is_terminal() && (id.is_uni() || !send_streams.exists(*id)) {
Some(*id)
} else {
None
}
})
.collect::<Vec<_>>();

let mut removed_bidi = 0;
let mut removed_uni = 0;
for id in &recv_to_remove {
self.streams.remove(id);
if id.is_remote_initiated(role) {
self.streams.retain(|id, s| {
let dead = s.is_terminal() && (id.is_uni() || !send_streams.exists(*id));
if dead && id.is_remote_initiated(role) {
if id.is_bidi() {
removed_bidi += 1;
} else {
removed_uni += 1;
}
}
}
!dead
});

(removed_bidi, removed_uni)
}
Expand Down

0 comments on commit 824d7c4

Please sign in to comment.