-
Notifications
You must be signed in to change notification settings - Fork 155
feat: Use a BinaryHeap for the RxStreamOrderer
#3054
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
Draft
larseggert
wants to merge
3
commits into
mozilla:main
Choose a base branch
from
larseggert:feat-RxStreamOrderer-heap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
| // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
| // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
| // option. This file may not be copied, modified, or distributed | ||
| // except according to those terms. | ||
|
|
||
| use std::hint::black_box; | ||
|
|
||
| use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
| use neqo_transport::{ | ||
| recv_stream::RxStreamOrdererBTreeMap as BTreeMapOrderer, | ||
| rx_stream_orderer_heap::RxStreamOrdererView as HeapOrderer, | ||
| }; | ||
|
|
||
| const FRAME_SIZE: usize = 1337; | ||
|
|
||
| fn benchmark_in_order_btreemap(c: &mut Criterion) { | ||
| c.bench_function("BTreeMap: in-order frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_in_order_heap(c: &mut Criterion) { | ||
| c.bench_function("BinaryHeap: in-order frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_reverse_order_btreemap(c: &mut Criterion) { | ||
| c.bench_function("BTreeMap: reverse-order frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in (0..1000).rev() { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_reverse_order_heap(c: &mut Criterion) { | ||
| c.bench_function("BinaryHeap: reverse-order frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in (0..1000).rev() { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_random_order_btreemap(c: &mut Criterion) { | ||
| // Create a deterministic "random" order by using a simple permutation. | ||
| let mut order: Vec<usize> = (0..1000).collect(); | ||
| // Simple shuffle using XOR swap. | ||
| for i in 0..order.len() { | ||
| let j = (i * 7 + 13) % order.len(); | ||
| order.swap(i, j); | ||
| } | ||
|
|
||
| c.bench_function("BTreeMap: random-order frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for &i in &order { | ||
| rx.inbound_frame(black_box(i as u64 * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_random_order_heap(c: &mut Criterion) { | ||
| let mut order: Vec<usize> = (0..1000).collect(); | ||
| for i in 0..order.len() { | ||
| let j = (i * 7 + 13) % order.len(); | ||
| order.swap(i, j); | ||
| } | ||
|
|
||
| c.bench_function("BinaryHeap: random-order frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for &i in &order { | ||
| rx.inbound_frame(black_box(i as u64 * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_with_gaps_btreemap(c: &mut Criterion) { | ||
| c.bench_function("BTreeMap: frames with gaps", |b| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| // Send every other frame. | ||
| for i in 0..1000 { | ||
| if i % 2 == 0 { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_with_gaps_heap(c: &mut Criterion) { | ||
| c.bench_function("BinaryHeap: frames with gaps", |b| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| if i % 2 == 0 { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_overlapping_btreemap(c: &mut Criterion) { | ||
| c.bench_function("BTreeMap: overlapping frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| // Each frame overlaps with the previous by half. | ||
| rx.inbound_frame(black_box(i * (FRAME_SIZE / 2) as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_overlapping_heap(c: &mut Criterion) { | ||
| c.bench_function("BinaryHeap: overlapping frames", |b| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| rx.inbound_frame(black_box(i * (FRAME_SIZE / 2) as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_read_to_end_btreemap(c: &mut Criterion) { | ||
| c.bench_function("BTreeMap: read_to_end after in-order insert", |b| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| rx.inbound_frame(i * FRAME_SIZE as u64, data); | ||
| } | ||
|
|
||
| let mut buf = Vec::new(); | ||
| black_box(rx.read_to_end(&mut buf)); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_read_to_end_heap(c: &mut Criterion) { | ||
| c.bench_function("BinaryHeap: read_to_end after in-order insert", |b| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..1000 { | ||
| rx.inbound_frame(i * FRAME_SIZE as u64, data); | ||
| } | ||
|
|
||
| let mut buf = Vec::new(); | ||
| black_box(rx.read_to_end(&mut buf)); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_varying_sizes(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("varying_frame_counts"); | ||
|
|
||
| for count in [100, 500, 1000, 5000, 10000].iter() { | ||
| group.bench_with_input(BenchmarkId::new("BTreeMap", count), count, |b, &count| { | ||
| b.iter(|| { | ||
| let mut rx = BTreeMapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..count { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| group.bench_with_input(BenchmarkId::new("BinaryHeap", count), count, |b, &count| { | ||
| b.iter(|| { | ||
| let mut rx = HeapOrderer::new(); | ||
| let data: &[u8] = &[0; FRAME_SIZE]; | ||
|
|
||
| for i in 0..count { | ||
| rx.inbound_frame(black_box(i * FRAME_SIZE as u64), black_box(data)); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!( | ||
| benches, | ||
| benchmark_in_order_btreemap, | ||
| benchmark_in_order_heap, | ||
| benchmark_reverse_order_btreemap, | ||
| benchmark_reverse_order_heap, | ||
| benchmark_random_order_btreemap, | ||
| benchmark_random_order_heap, | ||
| benchmark_with_gaps_btreemap, | ||
| benchmark_with_gaps_heap, | ||
| benchmark_overlapping_btreemap, | ||
| benchmark_overlapping_heap, | ||
| benchmark_read_to_end_btreemap, | ||
| benchmark_read_to_end_heap, | ||
| benchmark_varying_sizes, | ||
| ); | ||
| criterion_main!(benches); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an awful lot of benchmarking.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, no intention of leaving this in if there is a signal that the core change is at all positive.