refactor: format log records without allocating - #68
Merged
Conversation
Every emitted record allocated twice: std::format built a string for the message and std::println another for the line. Both now format into stack buffers, written out in one fwrite. The message gets its own buffer so an over-long one loses its own tail rather than the source location after it, and is marked [...] rather than cut silently. The record buffer holds back its last slot for the newline, which keeps a truncated record on one line. Formatters that go through ToString (IpAddress, MacAddress) still allocate; removing that needs a non-allocating conversion on those types.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every emitted record allocated twice —
std::formatbuilt a string for the message,std::printlnanother for the line. Both now format into stack buffers and go out in onefwrite. Suppressed records were already free (the level gate runs first), so this is about the lines that do emit, which at Info includes per-packet paths.Two details worth a look:
(file:line)suffix — losing the source location exactly when it is most wanted. Splitting them means the message loses its own tail instead, and it is marked[...]rather than cut silently.- 1yields a stack-buffer-overflow, caught by the new record-truncation test.Scope: this does not make logging allocation-free.
IpAddress::ToStringandMacAddress::ToStringstill allocate inside their formatters (a MAC is 17 chars, past SSO, so every time). Removing those needs a non-allocating conversion on the types themselves — tracked as follow-up.Also carries the signed/unsigned comparison convention this review settled, as its own commit.
Native (891) and docker/Linux (878) green.