-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an optimized token router filter (#948)
* Remove unneccessary allocations Both serialization and deserialization were doing unnecessary temporary heap allocations * Remove dead proto includes * Add an alternative HashedTokenRouter filter
- Loading branch information
1 parent
6badc24
commit 1556b19
Showing
17 changed files
with
394 additions
and
164 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,60 @@ | ||
use divan::Bencher; | ||
use quilkin::filters::token_router::{HashedTokenRouter, Router, TokenRouter}; | ||
use rand::SeedableRng; | ||
|
||
mod shared; | ||
|
||
#[divan::bench(types = [TokenRouter, HashedTokenRouter], args = ["single:duplicates", "single:unique", "multi:2..128:duplicates", "multi:2..128:unique"])] | ||
fn token_router<T>(b: Bencher, token_kind: &str) | ||
where | ||
T: Router + Sync, | ||
{ | ||
let filter = <T as Router>::new(); | ||
let gc = shared::gen_cluster_map::<42>(token_kind.parse().unwrap()); | ||
|
||
let mut tokens = Vec::new(); | ||
|
||
let cm = std::sync::Arc::new(gc.cm); | ||
cm.build_token_maps(); | ||
|
||
// Calculate the amount of bytes for all the tokens | ||
for eps in cm.iter() { | ||
for ep in &eps.value().endpoints { | ||
for tok in &ep.metadata.known.tokens { | ||
tokens.push(tok.clone()); | ||
} | ||
} | ||
} | ||
|
||
let total_token_size: usize = tokens.iter().map(|t| t.len()).sum(); | ||
let pool = std::sync::Arc::new(quilkin::pool::BufferPool::new(1, 1)); | ||
|
||
let mut rand = rand::rngs::SmallRng::seed_from_u64(42); | ||
|
||
b.with_inputs(|| { | ||
use rand::seq::SliceRandom as _; | ||
let tok = tokens.choose(&mut rand).unwrap(); | ||
|
||
let mut rc = quilkin::filters::ReadContext::new( | ||
cm.clone(), | ||
quilkin::net::EndpointAddress::LOCALHOST, | ||
pool.clone().alloc(), | ||
); | ||
rc.metadata.insert( | ||
quilkin::net::endpoint::metadata::Key::from_static( | ||
quilkin::filters::capture::CAPTURED_BYTES, | ||
), | ||
quilkin::net::endpoint::metadata::Value::Bytes((*tok).clone().into()), | ||
); | ||
|
||
rc | ||
}) | ||
.counter(divan::counter::BytesCount::new(total_token_size)) | ||
.bench_local_values(|mut rc| { | ||
let _ = divan::black_box(filter.sync_read(&mut rc)); | ||
}) | ||
} | ||
|
||
fn main() { | ||
divan::main(); | ||
} |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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.