Skip to content

Commit b1b762a

Browse files
committed
feat(benchmarks): add jemalloc integration for factor benchmarks
- Integrate jemalloc allocator in factor benchmark suite for better memory profiling - Add jemalloc-ctl and jemallocator dependencies with OS-specific dev-dependencies - Implement logging of allocated and resident memory stats before benchmark runs - Update CI workflow to show output for uu_factor benchmarks without suppressing it - Enables precise memory usage tracking on Linux, macOS, and FreeBSD during benchmarking
1 parent aea625a commit b1b762a

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

.github/workflows/benchmarks.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,9 @@ jobs:
7575
mode: instrumentation
7676
run: |
7777
echo "Running benchmarks for ${{ matrix.benchmark-target.package }}"
78-
cargo codspeed run -p ${{ matrix.benchmark-target.package }} > /dev/null
78+
if [[ "${{ matrix.benchmark-target.package }}" == "uu_factor" ]]; then
79+
cargo codspeed run -p ${{ matrix.benchmark-target.package }}
80+
else
81+
cargo codspeed run -p ${{ matrix.benchmark-target.package }} > /dev/null
82+
fi
7983
token: ${{ secrets.CODSPEED_TOKEN }}

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ icu_locale = "2.0.0"
331331
icu_provider = "2.0.0"
332332
indicatif = "0.18.0"
333333
itertools = "0.14.0"
334+
jemalloc-ctl = "0.5"
335+
jemallocator = "0.5"
334336
jiff = { version = "0.2.10", default-features = false, features = [
335337
"std",
336338
"alloc",

src/uu/factor/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ divan = { workspace = true }
3434
rand = { workspace = true }
3535
uucore = { workspace = true, features = ["benchmark"] }
3636

37+
[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))'.dev-dependencies]
38+
jemalloc-ctl = { workspace = true }
39+
jemallocator = { workspace = true }
40+
3741
[lib]
3842
path = "src/factor.rs"
3943

src/uu/factor/benches/factor_bench.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,42 @@ use divan::{Bencher, black_box};
99
use uu_factor::uumain;
1010
use uucore::benchmark::run_util_function;
1111

12+
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
13+
use jemallocator::Jemalloc;
14+
15+
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
16+
#[global_allocator]
17+
static ALLOC: Jemalloc = Jemalloc;
18+
19+
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))]
20+
fn log_jemalloc_stats(label: &str) {
21+
use jemalloc_ctl::{epoch, stats};
22+
23+
epoch::advance().unwrap();
24+
let allocated = stats::allocated::read().unwrap();
25+
let resident = stats::resident::read().unwrap();
26+
27+
println!(
28+
"jemalloc {label}: allocated={} bytes, resident={} bytes",
29+
allocated, resident
30+
);
31+
}
32+
33+
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "freebsd")))]
34+
fn log_jemalloc_stats(_label: &str) {}
35+
1236
/// Benchmark multiple u64 digits
1337
#[divan::bench(args = [(2)])]
1438
fn factor_multiple_u64s(bencher: Bencher, start_num: u64) {
1539
bencher
1640
// this is a range of 5000 different u128 integers
1741
.with_inputs(|| (start_num, start_num + 2500))
1842
.bench_values(|(start_u64, end_u64)| {
43+
log_jemalloc_stats("before factor_multiple_u64s");
1944
for u64_digit in start_u64..=end_u64 {
2045
black_box(run_util_function(uumain, &[&u64_digit.to_string()]));
2146
}
47+
log_jemalloc_stats("after factor_multiple_u64s");
2248
});
2349
}
2450

0 commit comments

Comments
 (0)