rust-lang / log

Logging implementation for Rust
https://docs.rs/log
Apache License 2.0
2.16k stars 250 forks source link

Group `target_module`, `path` and `file` arguments #575

Closed EFanZh closed 1 year ago

EFanZh commented 1 year ago

This is the first step of bring optimizations from #569 into the log crate.

This PR contains two commits:

For one of my internal projects, this PR will reduce the binary size by about 2%.

I also tested this PR on cargo-binstall, this is the test result:

opt-level lto codegen-units original size optimized size diff
3 off 1 14211752 14199464 -12288
3 off 16 16739048 16739048 0
3 thin 1 15170120 15166024 -4096
3 thin 16 17566376 17566376 0
3 fat 1 13859336 13859336 0
3 fat 16 15231496 15231496 0
"z" off 1 11451048 11438760 -12288
"z" off 16 13531848 13511368 -20480
"z" thin 1 12307048 12298856 -8192
"z" thin 16 13699720 13695624 -4096
"z" fat 1 9951752 9935368 -16384
"z" fat 16 10476040 10459656 -16384
EFanZh commented 1 year ago

OK, I have tested the performance using this code:

use criterion::Criterion;

fn log_string_literal(c: &mut Criterion) {
    let mut benchmark_group = c.benchmark_group("log string literal");

    benchmark_group.bench_function("original", |b| b.iter(|| log::info!("abc")));
    benchmark_group.bench_function("optimized", |b| b.iter(|| log_optimized::info!("abc")));

    benchmark_group.finish();
}

criterion::criterion_group!(benches, log_string_literal);

criterion::criterion_main!(benches);

with the following dependencies in Cargo.toml:

criterion = "*"
log = "*"
log_optimized = { git = "ssh://git@github.com/EFanZh/log.git", branch = "group-target-module-path-and-file", package = "log" }

On average, the original one takes 246.84 ps per iteration, and the optimized one takes 245.54 ps per iteration, I consider this difference insignificant.

EFanZh commented 1 year ago

Hi @KodrAus, I’d really appreciate it if you could take a look at this PR. I intend to do some binary size optimizations with ideas from #569. I plan to test and implement each idea with a separate PR, so if this PR gets accepted, I can move on to the next PR. Will these sort of optimizations be accepted?