Skip to content

Keep log_impl out of callsites - #751

Closed
patrick-immometrica wants to merge 2 commits into
rust-lang:mainfrom
patrick-immometrica:log-impl-out-of-line
Closed

patrick-immometrica wants to merge 2 commits into
rust-lang:mainfrom
patrick-immometrica:log-impl-out-of-line

Conversation

@patrick-immometrica

Copy link
Copy Markdown

log_impl is commented as "split from log to reduce generics and code size", but since #664 it's generic over the logger. So it's instantiated in the calling crate and LLVM inlines it there, and every log statement builds the whole Record at the callsite.

The first commit marks it #[inline(never)]. The global logger is still passed as a ZST.

The second commit passes the location as its own argument. &(target, module_path!(), loc()) can't be promoted to a static because of the loc() call, so since #633 the tuple is written to the stack before each call. This only makes a difference together with the first commit, on its own the size is the same as on main.

Measured with a workspace of 3 crates with 300 log statements each and a logger that does nothing (release build, Rust 1.99.1):

__text aarch64-apple-darwin __text x86_64-apple-darwin per record (aarch64)
0.4.26 331132 376640 2.8 ns
main 358652 429296 1.8 ns
first commit 331324 376976 3.0 ns
both commits 318076 348176 2.0 ns

That's about 45 bytes less per log statement on aarch64 and 90 on x86_64. An enabled record gets about 0.3 ns slower than on main, because the record isn't built from constants at the callsite anymore.

Since the logger param was added log_impl is generic, so it's compiled
in the calling crate and inlined there. Every log statement then builds
the whole Record itself.
A tuple holding the result of loc() can't be promoted to a static, so
it was written to the stack before each call. Without it the target and
module path are a constant again whenever the target is one.
@patrick-immometrica
patrick-immometrica marked this pull request as ready for review November 15, 2026 18:49
@KodrAus KodrAus closed this Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants