Skip to content

Commit

Permalink
Removed more allocations + other optimizations
Browse files Browse the repository at this point in the history
The most noisy change is that all public APIs are now marked as inline,
allowing the optimizer to see through many APIs. This produces nearly
identical results to enabling LTO.

The next optimization was to change read_buffered_bytes to reuse a
scratch buffer. This allows buffered reads to share a single allocation,
preventing many tiny allocations when reading things like bytes/strings
from a reader.

The final big optimization was to make SymbolMap store its list of
'ephemeral' strings in a single String, only storing the offsets. This
further reduced the number of allocations deserializing data required.

Some minor optimizations to how headers are encoded to optimize for the
most common paths also have been made.

All said, these changes account for ~10% improvement in the logs
benchmark on average, depending on the data set.
  • Loading branch information
ecton committed Jul 30, 2023
1 parent dadcb1d commit 3b94205
Show file tree
Hide file tree
Showing 11 changed files with 600 additions and 179 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["xtask", "pot"]
members = ["xtask", "pot", "benchmarks"]

[profile.bench]
# debug = true
lto = true
24 changes: 24 additions & 0 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "benchmarks"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies]
serde = { version = "1.0.136", features = ["derive"] }
pot = { path = "../pot" }
rand = "0.8.4"
fake = "2.4.3"
chrono = { version = "0.4.19", features = ["serde"] }
anyhow = "1.0.53"
cli-table = "0.4.6"
thousands = "0.2.0"
ciborium = "0.2.0"
bincode = "1.3.3"
rmp-serde = "1.1.0"
criterion = { version = "0.5", features = ["html_reports"] }


[[bench]]
name = "benchmarks"
harness = false
34 changes: 32 additions & 2 deletions pot/benches/benchmarks.rs → benchmarks/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! Proper benchmarks will be coming.

use std::env;
use std::fmt::Display;

use chrono::{DateTime, Utc};
Expand All @@ -10,7 +11,8 @@ use fake::faker::filesystem::en::FilePath;
use fake::faker::internet::en::Username;
use fake::faker::lorem::en::Sentence;
use fake::Fake;
use rand::{thread_rng, Rng};
use rand::rngs::StdRng;
use rand::{thread_rng, Rng, SeedableRng};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
Expand Down Expand Up @@ -107,8 +109,36 @@ fn bench_logs(c: &mut Criterion) {
let mut logs = LogArchive {
entries: Vec::with_capacity(LOG_ENTRIES),
};
let random_seed = env::args().find(|arg| arg.starts_with("-s")).map_or_else(
|| thread_rng().gen(),
|seed| {
let (_, seed) = seed.split_at(2);
let (upper, lower) = if seed.len() > 32 {
let (upper, lower) = seed.split_at(seed.len() - 32);
(
u128::from_str_radix(upper, 16).expect("invalid hexadecimal seed"),
u128::from_str_radix(lower, 16).expect("invalid hexadecimal seed"),
)
} else {
(
0,
u128::from_str_radix(seed, 16).expect("invalid hexadecimal seed"),
)
};
let mut seed = [0; 32];
seed[..16].copy_from_slice(&upper.to_be_bytes());
seed[16..].copy_from_slice(&lower.to_be_bytes());
seed
},
);
print!("Using random seed -s");
for b in random_seed {
print!("{b:02x}");
}
println!();
let mut rng = StdRng::from_seed(random_seed);
for _ in 0..LOG_ENTRIES {
logs.entries.push(Log::generate(&mut thread_rng()));
logs.entries.push(Log::generate(&mut rng));
}

let mut serialize_group = c.benchmark_group("logs/serialize");
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions pot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,5 @@ half = "2.2.1"
[dev-dependencies]
tracing-subscriber = "0.3.8"
tracing = "0.1.30"
rand = "0.8.4"
fake = "2.4.3"
chrono = { version = "0.4.19", features = ["serde"] }
anyhow = "1.0.53"
cli-table = "0.4.6"
thousands = "0.2.0"
ciborium = "0.2.0"
bincode = "1.3.3"
rmp-serde = "1.1.0"
criterion = { version = "0.5", features = ["html_reports"] }
serde_bytes = "0.11.5"
serde_json = "1.0.78"
approx = "0.5.1"

[[bench]]
name = "benchmarks"
harness = false
Loading

0 comments on commit 3b94205

Please sign in to comment.