Skip to content

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

Merged
SajjadPourali merged 1 commit into
narrowlink:mainfrom
WYCLIFF001:perf/lazy-log-context
Oct 12, 2026
Merged

SajjadPourali merged 1 commit into
narrowlink:mainfrom
WYCLIFF001:perf/lazy-log-context

Conversation

@WYCLIFF001

Copy link
Copy Markdown
Contributor

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.
@SajjadPourali SajjadPourali self-assigned this Oct 6, 2026

@SajjadPourali SajjadPourali left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the logging changes. The expensive header formatting and contextual string construction are deferred until the matching log level is enabled; packet handling is unchanged. CI passes.

@SajjadPourali
SajjadPourali merged commit e15e90a into narrowlink:main Oct 12, 2026
2 checks passed
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