Skip to content

Format log context only when a line is logged - #98

Open
WYCLIFF001 wants to merge 1 commit into
narrowlink:mainfrom
WYCLIFF001:perf/lazy-log-context
Open

WYCLIFF001 wants to merge 1 commit into
narrowlink:mainfrom
WYCLIFF001:perf/lazy-log-context

Conversation

@WYCLIFF001

Copy link
Copy Markdown

The session loop builds format!("local {{ seq: {seq}, ack: {ack} }}") and tcp_header_fmt(tcp_header) for every packet, and poll_write builds another string for every held write, whatever the log level. At the default level none of them is printed, so every packet pays for allocations and formatting that go unused. In a profile of a 600 Mbit/s tunnel these showed up as core::fmt plus the matching malloc/free on every packet.

  • l_info is now a small LocalInfo(seq, ack) value that implements Display, so it formats only inside a log line that is actually emitted. The six call sites are unchanged apart from the constructor.
  • The per-packet header description is built only under log::log_enabled!(log::Level::Trace).
  • The held-write message is formatted inline in its trace!.

Log output is identical at every level.

cargo fmt --check, cargo clippy --all-targets (no warnings) and cargo test pass.

🤖 Generated with Claude Code

The session loop built the `local { seq, ack }` string and the header
description for every packet, and poll_write another string for every
held write, whatever the log level. At the default level none of them is
ever printed, so every packet paid for an allocation and a format that
went unused. The context is now a value that formats itself when a log
line is emitted, and the header description is built only when trace is
enabled.
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.

1 participant