Skip to content

Investigate binary size footprint and callsite metadata layout of logging macros #273

Backlog

Description

@tisonkun

Context

Each logforth_core logging macro invocation (info!, debug!, etc.) checks Logger::enabled(level) and passes &LOC to the non-generic helper __private::log (core/src/macros.rs), where LOC is a per-callsite static Location descriptor:

Location (40 bytes, align = 8 on 64-bit targets):
+--------------------+--------------------+----------+----------+
| module_path (16 B) |    file (16 B)     | line(4B) | col (4B) |
+--------------------+--------------------+----------+----------+

Two structural properties stand out when comparing against log (see rust-lang/log#569, rust-lang/log#575, and rust-lang/log#705):

  1. Per-callsite .rodata duplication & alignment padding: Because line and column differ on each line, the compiler cannot merge static LOC instances within the same file or module, duplicating the 32-byte (module_path, file) fat pointers in .rodata at every callsite. Simply dropping column: u32 alone does not shrink .rodata on 64-bit targets because 8-byte struct alignment pads 16 + 16 + 4 = 36 back up to 40 bytes (4 B tail padding), whereas splitting a module-shared (module_path, file) constant from a per-callsite line argument allows constant merging. Meanwhile, across built-in components, only logforth-append-opentelemetry currently reads Record::column.
  2. Non-generic callsite .text vs. compile-time level stripping: Because __private::log is non-generic, LLVM does not inline Record construction into every callsite under opt-level = 3, keeping .text smaller than log when all levels remain compiled. Under opt-level = "z", log saves ~16 B/call in .rodata via Location::caller() (24 B) while spending ~10 B/call more in .text to assemble &(target, module_path, loc) on the stack (net ~6 B/call difference). However, logforth-core does not currently offer compile-time max_level_* / release_max_level_* feature gates to dead-code-eliminate verbose callsites altogether.

Preliminary Measurements

Binary section deltas follow a linear model $\Delta\text{Size}(N, P, K) \approx C_0 + \alpha N + \beta P + \gamma K$ (C_0: fixed runtime code, N: compiled callsites, P: format arguments, K: key-value pairs). Sample numbers for N = 1,000 calls across 20 modules (50 calls/module, 1 format argument, aarch64-apple-darwin, -C strip=symbols):

Configuration (N = 1,000) log v0.4 (.text + .rodata = total) logforth-core (.text + .rodata = total) Marginal slope (log vs. logforth)
opt-level = "z", 1 format arg 43.0 KB + 25.3 KB = 68.3 KB 37.2 KB + 42.1 KB = 79.4 KB ~68 B/call vs. ~74 B/call
opt-level = "z", + 2 KV pairs 55.2 KB + 25.6 KB = 80.8 KB 49.5 KB + 42.4 KB = 91.9 KB +6.2 B/KV on both
opt-level = 3, 1 format arg 118.4 KB + 1.3 KB = 119.7 KB 59.1 KB + 42.1 KB = 101.3 KB ~119 B/call vs. ~97 B/call
opt-level = 3, + 2 KV pairs 171.5 KB + 1.4 KB = 172.8 KB 103.9 KB + 42.3 KB = 146.3 KB +26.6 B/KV vs. +22.5 B/KV

In an isolated 1,000-call microbenchmark (opt-level = "z"), splitting shared &(module_path!(), file!()) metadata from a register-passed line!() argument (and omitting column) reduced .rodata by ~39.3 KB at a cost of +320 B in .text.

Scope of Investigation

  • Build a reproducible benchmark script or standalone crate measuring .text, .rodata, total stripped binary size, and callsite latency across Linux (ELF x86_64 / aarch64) and macOS (Mach-O), covering opt-level = 3, "s", and "z" (with and without LTO).
  • Evaluate splitting shared (module_path, file) metadata from per-callsite line, and assess whether Record::column / RecordBuilder::column should be retained, removed before logforth-core 1.0, or represented differently.
  • Evaluate micro-optimizations at macro callsites, such as avoiding empty &[] key-value slice construction when no fields are passed and checking #[inline] behavior of Logger::enabled.
  • Explore whether optional compile-time static level filtering (max_level_* / release_max_level_* Cargo features) makes sense alongside explicit Logger instances.

Activity

  1. added
    help wanted
    good first issue
    on Nov 28, 2026
  2. meditecnic commented on Nov 29, 2026

    @meditecnic

    Edit: collapsed the raw data tables to keep the thread readable. No wording or numbers changed.

    Hi! 👋 I ran a standalone Linux x86_64 ELF benchmark and a set of single-variable prototype experiments against logforth-core (5cf0480) and log (0.4.31) to complement the macOS Mach-O measurements in the issue description.

    Environment and Methodology

    • Host: Linux 7.0.0-38-generic (x86_64, glibc 2.43), AMD Ryzen 7 4800H (8C/16T), rustc 1.100.1 (20ddd0171 2026-11-25), cargo 1.100.1, GNU ld 2.46, GNU readelf 2.46.
    • Targets: fast/logforth at 5cf0480a2641b7807b742d2db099fa2594070b0e (logforth-core 0.4.0, default features) vs. log 0.4.31 (features = ["std", "kv"] for KV feature parity, plus a features = ["std"] control on the no-KV case).
    • Harness and patches: https://github.com/meditecnic/logforth-bench-273/tree/abfd910 (run ./reproduce.sh --clean alongside a sibling ../logforth checkout; single-variable prototype diffs are under patches/).
    • Size harness: 20 separate module files (src/mods/m00.rs..m19.rs), measured at N = 0, 20, 200, 1,000 callsites (0, 1, 10, 50 calls/module) for 1 format arg ("event {}", x) and 1 format arg + 2 KV pairs (u64 + bool), across all 9 opt-level (3, "s", "z") x lto (false, "thin", "fat") profiles with fixed codegen-units = 1, panic = "unwind", and strip = "symbols". A cold multi-shape call in main() (warn! with KV + error!) prevents unrealistic whole-program IPCP from folding level/kvs/format template under Fat LTO, and compiler_fence(Ordering::SeqCst) prevents coalescing sequential level checks without adding machine instructions.
    • Latency harness: Separate standalone binary measuring 11 independent process invocations per profile (10M iterations for disabled fast path, 2M for enabled no-I/O path using a no-op Append/log::Log), with std::hint::black_box on runtime inputs and inside the no-op sink; reports median and IQR (Q3 - Q1). Because logforth_core::record::RecordBuilder::new() calls SystemTime::now() while log::Record does not, standalone SystemTime::now() is measured in the same harness to separate timestamp cost.
    • Verification: Anti-DCE checked via nm -C -S and objdump -d -C on unstripped builds (20/20 module functions retained, 1,000 callsites emitted with zero loop rerolling or DCE), full ELF section diff checked across N = 200 -> 1,000, and a clean-slate rerun (./reproduce.sh --clean) reproduced 180/180 size builds byte-for-byte.

    1. Linux ELF Binary Size Matrix (N = 1,000, stripped)

    Show tables

    1 format arg

    opt / lto Crate .text .rodata .data.rel.ro .rela.dyn .eh_frame File (B) N=0 (B) Slope (B/call)
    3 / false log 470,387 22,684 6,816 15,792 21,860 567,824 336,224 228.0
    3 / false logforth 340,851 23,924 47,832 65,976 24,568 536,152 355,736 177.2
    3 / thin log 499,989 22,921 6,360 13,776 20,360 583,032 318,472 260.8
    3 / thin logforth 465,797 24,929 7,312 15,840 23,232 558,264 337,704 217.2
    3 / fat log 487,174 21,842 5,584 8,208 17,312 556,784 291,264 262.0
    3 / fat logforth 324,438 23,898 45,968 56,808 19,640 488,616 309,192 176.0
    s / false log 341,219 22,684 30,864 39,936 22,392 487,536 336,960 147.0
    s / false logforth 326,387 23,916 47,832 66,456 24,748 522,936 354,536 165.0
    s / thin log 323,013 22,729 30,440 37,968 20,204 454,016 310,704 140.0
    s / thin logforth 310,565 23,961 47,408 64,344 22,620 490,088 326,440 160.0
    s / fat log 305,846 21,439 29,632 32,256 17,128 422,992 283,648 136.0
    s / fat logforth 297,878 23,567 45,968 56,808 18,856 460,760 298,088 159.0
    z / false log 333,427 22,684 30,864 39,960 31,876 489,288 337,240 148.0
    z / false logforth 320,051 23,916 47,832 66,528 43,292 535,264 354,960 177.0
    z / thin log 311,381 22,729 30,440 38,088 27,700 450,120 311,384 135.0
    z / thin logforth 306,309 23,961 47,408 64,608 36,248 499,984 327,392 169.0
    z / fat log 293,946 21,439 29,632 32,256 24,432 418,384 283,632 131.0
    z / fat logforth 293,178 23,567 45,968 56,808 31,048 468,216 297,640 167.0

    1 format arg + 2 KV pairs

    opt / lto Crate .text .rodata .data.rel.ro .rela.dyn .eh_frame File (B) N=0 (B) Slope (B/call)
    3 / false log 591,331 22,780 6,816 15,792 21,860 688,864 336,224 349.2
    3 / false logforth 441,971 24,020 47,832 65,976 24,568 637,368 355,736 278.0
    3 / thin log 620,933 23,013 6,360 13,776 20,360 704,072 318,488 382.0
    3 / thin logforth 591,573 25,021 7,312 15,840 23,232 684,136 337,704 342.9
    3 / fat log 608,118 21,938 5,584 8,208 17,312 677,824 291,264 383.2
    3 / fat logforth 425,558 23,994 45,968 56,808 19,640 589,832 309,192 277.2
    s / false log 554,643 22,780 30,864 39,960 22,572 701,272 336,960 360.2
    s / false logforth 512,739 23,996 47,832 66,480 24,928 709,568 354,552 351.0
    s / thin log 402,949 22,821 30,440 37,968 20,044 533,888 310,704 220.0
    s / thin logforth 393,621 24,053 47,408 64,344 22,616 573,224 326,440 243.0
    s / fat log 386,790 21,527 29,632 32,256 16,968 503,872 283,664 217.0
    s / fat logforth 380,934 23,663 45,968 56,808 18,856 543,912 298,088 242.0
    z / false log 544,851 22,780 30,864 39,984 40,840 709,808 337,240 368.3
    z / false logforth 501,507 23,996 47,832 66,552 50,832 724,360 354,976 365.0
    z / thin log 395,301 22,821 30,440 38,088 37,604 544,024 311,384 229.0
    z / thin logforth 383,525 24,053 47,408 64,608 40,564 581,616 327,392 250.0
    z / fat log 377,850 21,527 29,632 32,256 34,336 512,272 283,632 225.0
    z / fat logforth 371,402 23,663 45,968 56,808 36,408 551,896 297,640 250.0
    • Linearity and KV slope: Slope (B/call) is the marginal slope (File(1,000) - File(200)) / 800, isolating per-callsite cost from N = 0 fixed overhead; marginal slopes matched within ~1–3 B/call between N = 20 -> 200 and N = 200 -> 1,000 across all profiles. Marginal KV cost ((kv2 - arg1) / 2) was +50.4 to +50.6 B/KV for logforth vs. +60.6 B/KV for log under 3/false and 3/fat, and +40.5 to +41.5 B/KV (logforth) vs. +40.0 to +47.0 B/KV (log) under s/z with LTO (without LTO under s/z, both crates jump to +93 to +110 B/KV because LogValue::log_value / ToValue::to_value are not inlined across crates).
    • Fairness notes: Disabling log's kv feature (features = ["std"]) reduces log arg1 at N = 1,000 by -50.5 KB under 3/false (517,352 B, 178.8 B/call) and -50.7 KB under 3/fat (506,048 B, 212.8 B/call) because log::__private_api::log_impl inlines &None KV vtable setup at -O3, whereas under s/z the difference is only -1.1 to -4.0 KB. Separately, log uses a global singleton whereas logforth-core passes an explicit &Logger parameter. A side probe with codegen-units = 16 on logforth arg1 left .data.rel.ro and .rela.dyn unchanged and shifted .text by +1.1 to +3.0 KB.

    2. Callsite Latency Baseline (11 independent runs, median ns/op, IQR in parens)

    Show tables
    Scenario 3/off 3/fat s/off z/off z/fat
    Loop baseline (black_box) 0.55 (0.50) 0.68 (0.15) 0.57 (0.39) 0.57 (0.40) 0.25 (0.50)
    SystemTime::now() only 22.92 (1.48) 24.13 (1.97) 23.72 (1.48) 24.16 (2.02) 22.74 (1.21)
    log disabled (1 arg) 0.24 (0.01) 0.25 (0.02) 0.49 (0.01) 0.76 (0.02) 0.48 (0.01)
    log disabled (1 arg + 2 KV) 0.24 (0.00) 0.25 (0.01) 0.74 (0.02) 0.75 (0.08) 0.49 (0.01)
    logforth disabled, filtered (1 arg) 0.49 (0.01) 0.50 (0.03) 1.53 (0.10) 1.98 (0.04) 0.75 (0.01)
    logforth disabled, filtered (2 KV) 0.50 (0.03) 0.51 (0.05) 1.89 (0.11) 2.10 (0.09) 0.77 (0.01)
    logforth disabled, noop (1 arg) 0.31 (0.03) 0.34 (0.04) 1.83 (0.08) 2.03 (0.07) 0.52 (0.01)
    log enabled, no-op (1 arg) 5.14 (0.30) 5.37 (0.44) 10.45 (0.32) 11.19 (0.24) 10.23 (0.25)
    log enabled, no-op (1 arg + 2 KV) 8.08 (0.14) 8.27 (0.57) 25.33 (0.76) 26.51 (0.67) 14.96 (0.24)
    logforth enabled, no-op (1 arg) 29.40 (1.30) 27.88 (1.65) 30.16 (1.38) 31.42 (1.25) 28.59 (1.12)
    logforth enabled, no-op (2 KV) 32.58 (0.49) 31.09 (2.54) 51.05 (0.72) 51.79 (1.10) 32.24 (0.98)

    3. Single-Variable Prototype Experiments (vs. Baseline 5cf0480 at N = 1,000)

    Show tables

    Each prototype isolates one change on a local branch from 5cf0480 before testing combinations: A adds __private::log_no_kv so empty [] calls skip &[]; B adds #[inline] to Logger::enabled; C0 drops column from Location without splitting (module_path, file); C1 splits const { &ModuleLocation::new(module_path!(), file!()) } (32 B, align 8) and passes line: u32, column: u32; C1p packs (line, column) into u64 (preserving Record::column); C2 splits ModuleLocation, passes line: u32, and removes Record::column / RecordBuilder::column (also updating appenders/opentelemetry and integration tests; Record shrinks 128 -> 120 B, RecordOwned 136 -> 128 B).

    Stripped file size delta vs. baseline (logforth, 1 format arg, bytes)

    Prototype 3/off 3/thin 3/fat s/off s/fat z/off z/fat
    A: log_no_kv (no &[]) -13,800 +280 +38,248 -11,752 -11,880 -15,752 -15,944
    B: #[inline] Logger::enabled +0 +0 +0 -4,856 +0 -7,816 +0
    C0: drop column (unsplit LOC) +0 +10,416 +16 +0 -16 -32 -16
    C1: split ModuleLoc + line, col -55,440 +16 -55,344 -53,392 -53,328 -45,424 -45,360
    C1p: split ModuleLoc + packed u64 -64,352 +0 -64,240 -62,448 -62,400 -63,296 -65,232
    C2: split ModuleLoc, remove column -69,504 -6,912 -69,408 -67,488 -67,424 -64,512 -66,432
    A + C1p (no #[inline(never)]) -89,960 +280 +38,272 -88,136 -88,256 -92,088 -91,280
    A + C2 (no #[inline(never)]) -95,160 -6,648 +31,632 -93,192 -93,280 -93,256 -93,456
    A + B + C1p + #[inline(never)] -89,960 -130,120 -89,904 -92,960 -88,160 -99,888 -91,200

    Stripped file size delta vs. baseline (logforth, 1 format arg + 2 KV pairs, bytes)

    Prototype 3/off 3/thin 3/fat s/off s/fat z/off z/fat
    A: log_no_kv (no &[]) +280 +296 +63,224 +296 +152 +280 +72
    B: #[inline] Logger::enabled +0 +16 +0 -6,680 +0 -12,648 +0
    C0: drop column (unsplit LOC) +0 +10,400 +16 +0 -16 -16 -16
    C1: split ModuleLoc + line, col -51,360 +16 -51,264 -49,376 -48,352 -50,992 -48,992
    C1p: split ModuleLoc + packed u64 -60,352 -16 -60,224 -59,440 -59,344 -61,328 -62,288
    C2: split ModuleLoc, remove column -65,488 -6,608 -65,424 -64,480 -64,384 -60,528 -61,472
    A + B + C1p + #[inline(never)] -60,040 -124,840 -59,888 -65,872 -59,024 -73,744 -62,016

    Section breakdown at oz-lto-off (logforth, 1 format arg, N = 1,000) and prototype latency (median ns, 11 runs)

    Prototype d.text d.data.rel.ro d.rela.dyn d.eh_frame Dis 3/off Dis s/off Dis z/off En 3/off En 3/fat
    Baseline (5cf0480) 0 0 0 0 0.49 1.53 1.98 29.40 27.88
    A: log_no_kv -9,856 +0 +24 -5,924 0.49 1.93 1.94 28.16 26.90
    B: #[inline] Logger::enabled -2,768 +0 -24 -4,996 0.48 0.48 0.74 28.59 26.74
    C1: split ModuleLoc + l, c +14,000 -39,408 -47,088 +27,080 0.49 1.46 1.97 29.97 27.30
    C1p: split ModuleLoc + u64 +14,016 -39,408 -47,088 +9,192 0.48 1.73 1.94 29.76 28.03
    C2: split, remove column +6,960 -39,408 -47,088 +15,036 0.48 1.68 1.94 28.32 26.82
    A + B + C1p + #[inline(never)] -2,608 -39,408 -47,088 -10,776 0.48 0.73 0.49 29.37 30.72

    All non-control prototypes (A, B, C1, C1p, C2, and A + B + C1p + #[inline(never)]) passed cargo +nightly check -Dwarnings, cargo test (unit, integration, and doctests), cargo +nightly clippy -- -D warnings, cargo +nightly fmt --all --check, and docsrs rustdoc checks across all workspace crates except logforth-append-syslog's native-tls feature (requires system libssl-dev, absent on this host); taplo, typos, and hawkeye are also not installed locally.

    Key Findings

    1. Linux ELF PIE doubles per-callsite Location cost to 88 B/call: Because Location holds two &'static str pointers, PIE binaries place static LOC in .data.rel.ro (+40 B/call) rather than .rodata (+0.02 B/call) and emit two 24-byte R_X86_64_RELATIVE entries in .rela.dyn (+48 B/call). Enabling RELR (-Wl,-z,pack-relative-relocs) shrinks .rela.dyn (66,528 -> 1,608 B plus 848 B .relr.dyn at oz-lto-off), but .data.rel.ro still pays 40 B/call. Dropping column without splitting (C0) leaves size_of::<Location>() == 40 and saves 0 B in .data.rel.ro and .rela.dyn across all profiles (the +10.4 KB delta at 3/thin is a .text +11.2 KB Thin LTO codegen shift), confirming the alignment-padding observation.
    2. Splitting ModuleLocation + packing (line, column) + log_no_kv captures ~95% of the win without breaking Record::column: Splitting const { &ModuleLocation } saves -39.4 KB .data.rel.ro and -47.1 KB .rela.dyn (-86.5 KB total), but on x86_64 SysV ABI (6 integer argument registers), adding line and column without log_no_kv spills arguments to the stack and adds +10 to +27 KB of .eh_frame unwind metadata. Combining log_no_kv with packed line_col: u64 (A + C1p) fits all 6 registers (logger, level, loc, line_col, fmt::Arguments 2 words) with zero stack spills, saving -88 to -100 KB on arg1 (-59 to -74 KB on arg1_kv2) while keeping Record::column intact; removing Record::column (C2 vs. C1p) yields an additional ~2–5 B/call on arg1 (and -8 B on Record/RecordOwned) while breaking logforth-append-opentelemetry and Record::column callers.
    3. #[inline] on Logger::enabled is a free win under -Os/-Oz without LTO: On rustc 1.100.1, MIR inlining already inlines Logger::enabled at opt-level = 3 (even with lto = false), and LTO inlines it under s/z. Under s/z with lto = false, adding #[inline] both shrinks stripped binaries (-4.9 to -12.6 KB, eliminating GOT calls and push/pop .eh_frame entries) and reduces disabled fast-path latency from 1.53–1.98 ns to 0.48–0.74 ns (2.7x–3.2x faster), with 0 B / 0 ns regression elsewhere.
    4. LTO inlining of __private::log is a hidden size trap: In baseline o3-lto-thin, Thin LTO inlines __private::log into all 1,000 callsites (+125 KB .text, -88 KB .data.rel.ro + .rela.dyn, net +40 B/call). Introducing log_no_kv (A) splits Logger::log from 1 caller to 2, causing Fat LTO (o3-lto-fat) to stop inlining Logger::log into __private::log and instead inline __private::log* into every callsite (+38 to +63 KB). Marking __private::log and __private::log_no_kv #[inline(never)] prevents that regression across all 18 matrix cells (-90 to -130 KB combined), trading ~2–3.8 ns on the no-I/O enabled path under Fat LTO (vs. ~23–24 ns for SystemTime::now()). Note also that under panic = "unwind", opt-level = "z" produces slightly larger stripped binaries than opt-level = "s" because push $imm8; pop sequences inflate .eh_frame.
    5. Brief note on compile-time max_level_* / release_max_level_* features: Because logforth-core models channels as explicit Logger instances rather than a global singleton (DESIGN.md) and disabled callsites already short-circuit in ~0.49 ns via a 24-bit mask test, crate-wide additive Cargo features create a real footgun where one dependency enabling release_max_level_info would strip verbose callsites from unrelated Logger channels (e.g., dedicated audit or diagnostic loggers) across the entire workspace; if added, they would likely need strict top-level-binary-only guidance similar to log.

    Disclosure: I used AI assistance to help draft the benchmark harness scripts and analyze the ELF/disassembly data; all builds, tests, and measurements were compiled and executed locally on Linux and verified from a clean rebuild.

    Happy to open a small focused PR for a non-breaking subset first (e.g., #[inline] on Logger::enabled and/or the const &ModuleLocation + packed (line, column) + log_no_kv + #[inline(never)] macro layout), or integrate the benchmark script in-tree if you think that's useful 🤗

  3. tisonkun commented on Nov 30, 2026

    @tisonkun
    ContributorAuthor

    @meditecnic Please try to write the comment top-down and convince me what you want to do is trustworthy, instead of pushing a 2000 words of AI reports and expect others to interpret it.

    if you think that's useful

    Have you read the report you throw here? Do you know what it means? Do you think it's useful? If so, why?

  4. meditecnic commented on Nov 30, 2026

    @meditecnic

    Fair point — I'm sorry for dumping a long AI-generated report and ending with a vague "if you think that's useful".

    To answer your questions honestly: I'm still learning Rust and used AI to build the benchmark and write up the report, so I don't want to pretend I designed that patch myself. Looking back, the combined u64-packing + #[inline(never)] proposal I added at the end was a hack around compiler heuristics rather than a sound design, so I'm withdrawing it.

    The one data point I can explain and stand behind is the Linux x86_64 baseline: each callsite's static LOC holds two pointers, so in a PIE binary it lands in .data.rel.ro (40 B) and also needs two dynamic relocations in .rela.dyn (48 B), ~88 B per callsite vs. ~40 B in the macOS numbers. So the per-callsite cost that the metadata split targets is roughly twice as large on Linux.

    I'll take the time to understand the design trade-offs properly before coming back with anything else. Thanks for the direct feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions