-
Notifications
You must be signed in to change notification settings - Fork 57
Performance improvements #108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf4b559
Make lookup benchmark deterministic and more stable
oschwald 8da6205
Speed up lookup and decode hot paths
oschwald 7075c56
Optimize integer decoding paths
oschwald 40f8823
Add ASCII fast path for string decoding
oschwald e525987
Set release log level to info at compile time
oschwald c20e7e8
Optimize control-byte size decoding
oschwald 3b38816
Handle truncated control-byte headers gracefully
oschwald d8a9715
Prepare changelog for 0.27.2
oschwald 98a777c
Add serde usage benchmark
oschwald 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,21 @@ | ||
| use std::net::{IpAddr, Ipv4Addr}; | ||
|
|
||
| // Generate `count` IPv4 addresses from a deterministic LCG stream. | ||
| #[must_use] | ||
| pub fn generate_ipv4(count: u64) -> Vec<IpAddr> { | ||
| let mut ips = Vec::with_capacity(count as usize); | ||
| let mut state = 0x4D59_5DF4_D0F3_3173_u64; | ||
| for _ in 0..count { | ||
| state = state | ||
| .wrapping_mul(6_364_136_223_846_793_005) | ||
| .wrapping_add(1_442_695_040_888_963_407); | ||
| let ip = Ipv4Addr::new( | ||
| (state >> 24) as u8, | ||
| (state >> 32) as u8, | ||
| (state >> 40) as u8, | ||
| (state >> 48) as u8, | ||
| ); | ||
| ips.push(IpAddr::V4(ip)); | ||
| } | ||
| ips | ||
| } |
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,112 @@ | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use maxminddb::geoip2; | ||
| use maxminddb::{LookupResult, PathElement, Reader}; | ||
| use std::hint::black_box; | ||
|
|
||
| use std::net::IpAddr; | ||
|
|
||
| mod common; | ||
| use common::generate_ipv4; | ||
|
|
||
| const DB_FILE: &str = "GeoLite2-City.mmdb"; | ||
|
|
||
| fn cache_lookups<'a, T>(ips: &[IpAddr], reader: &'a Reader<T>) -> Vec<LookupResult<'a, T>> | ||
| where | ||
| T: AsRef<[u8]>, | ||
| { | ||
| ips.iter() | ||
| .map(|ip| reader.lookup(*ip).unwrap()) | ||
| .filter(|r| r.has_data()) | ||
| .collect() | ||
| } | ||
|
|
||
| fn bench_lookup_only<T>(ips: &[IpAddr], reader: &Reader<T>) | ||
| where | ||
| T: AsRef<[u8]>, | ||
| { | ||
| for ip in ips { | ||
| let result = reader.lookup(*ip).unwrap(); | ||
| black_box(result.has_data()); | ||
| } | ||
| } | ||
|
|
||
| fn bench_decode_city_only<T>(results: &[LookupResult<'_, T>]) | ||
| where | ||
| T: AsRef<[u8]>, | ||
| { | ||
| for result in results { | ||
| let city: Option<geoip2::City<'_>> = result.decode().unwrap(); | ||
| black_box(city); | ||
| } | ||
| } | ||
|
|
||
| fn bench_decode_country_only<T>(results: &[LookupResult<'_, T>]) | ||
| where | ||
| T: AsRef<[u8]>, | ||
| { | ||
| for result in results { | ||
| let country: Option<geoip2::Country<'_>> = result.decode().unwrap(); | ||
| black_box(country); | ||
| } | ||
| } | ||
|
|
||
| fn bench_decode_path_country_iso<T>(results: &[LookupResult<'_, T>]) | ||
| where | ||
| T: AsRef<[u8]>, | ||
| { | ||
| let path = [PathElement::Key("country"), PathElement::Key("iso_code")]; | ||
| for result in results { | ||
| let value: Option<&str> = result.decode_path(&path).unwrap(); | ||
| black_box(value); | ||
| } | ||
| } | ||
|
|
||
| fn bench_decode_path_city_name<T>(results: &[LookupResult<'_, T>]) | ||
| where | ||
| T: AsRef<[u8]>, | ||
| { | ||
| let path = [ | ||
| PathElement::Key("city"), | ||
| PathElement::Key("names"), | ||
| PathElement::Key("en"), | ||
| ]; | ||
| for result in results { | ||
| let value: Option<&str> = result.decode_path(&path).unwrap(); | ||
| black_box(value); | ||
| } | ||
| } | ||
|
|
||
| pub fn serde_usage_benchmark(c: &mut Criterion) { | ||
| let ips = generate_ipv4(100); | ||
|
|
||
| #[cfg(not(feature = "mmap"))] | ||
| let reader = Reader::open_readfile(DB_FILE).unwrap(); | ||
| #[cfg(feature = "mmap")] | ||
| // SAFETY: The benchmark database file will not be modified during the benchmark. | ||
| let reader = unsafe { Reader::open_mmap(DB_FILE) }.unwrap(); | ||
|
|
||
| let cached_results = cache_lookups(&ips, &reader); | ||
|
|
||
| c.bench_function("serde_usage/lookup_only", |b| { | ||
| b.iter(|| bench_lookup_only(&ips, &reader)) | ||
| }); | ||
| c.bench_function("serde_usage/decode_city_only", |b| { | ||
| b.iter(|| bench_decode_city_only(&cached_results)) | ||
| }); | ||
| c.bench_function("serde_usage/decode_country_only", |b| { | ||
| b.iter(|| bench_decode_country_only(&cached_results)) | ||
| }); | ||
| c.bench_function("serde_usage/decode_path_country_iso", |b| { | ||
| b.iter(|| bench_decode_path_country_iso(&cached_results)) | ||
| }); | ||
| c.bench_function("serde_usage/decode_path_city_name", |b| { | ||
| b.iter(|| bench_decode_path_city_name(&cached_results)) | ||
| }); | ||
| } | ||
|
|
||
| criterion_group! { | ||
| name = benches; | ||
| config = Criterion::default().sample_size(20); | ||
| targets = serde_usage_benchmark | ||
| } | ||
| criterion_main!(benches); |
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.
Uh oh!
There was an error while loading. Please reload this page.