Skip to content

Perf improvements#184

Open
jonstacks wants to merge 2 commits into
inconshreveable:masterfrom
jonstacks:perf-improvements
Open

Perf improvements#184
jonstacks wants to merge 2 commits into
inconshreveable:masterfrom
jonstacks:perf-improvements

Conversation

@jonstacks

Copy link
Copy Markdown
Contributor

While working on a slog shim for log15, claude and I found some small performance improvements that look mostly straightforward.

Benchmark Before ns/op After ns/op Δ ns Before B/op After B/op Δ B Before allocs After allocs Δ allocs
LogfmtNoCtx 552 527 -5% 256 208 -19% 10 9 -1
LogfmtWithIntCtx 1099 886 -19% 624 572 -8% 16 14 -2
LogfmtWithMixedCtx 1093 931 -15% 646 597 -8% 13 12 -1
TerminalFormatNoCtx 443 235 -47% 184 88 -52% 7 3 -4
TerminalFormatWithIntCtx 1204 532 -55% 448 256 -43% 18 7 -11
Log15AddingFields 6899 6798 -1% 3806 3807 ~0% 47 47 0

jonstacks and others added 2 commits June 5, 2026 15:58
Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>
Avoid fmt's reflection dispatch and internal buffering in hot paths:
- formatLogfmtValue: expand combined integer case into per-type strconv
  calls (FormatInt/FormatUint/Itoa) instead of fmt.Sprintf("%d", value)
- TerminalFormat/logfmt: replace fmt.Fprintf into *bytes.Buffer with
  explicit WriteString/WriteByte calls

Benchmarks show: TerminalFormat -47% ns/op and -4 allocs/op with no
context; with integer context -55% ns/op and -11 allocs/op. Logfmt
integer formatting -19% ns/op and -2 allocs/op.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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