Skip to content

Logger: allocate nothing when the level filters every line out - #1456

Merged
crimson-knight merged 2 commits into
v2-devfrom
perf/logger-skips-filtered-lines
Nov 21, 2026
Merged

crimson-knight merged 2 commits into
v2-devfrom
perf/logger-skips-filtered-lines

Conversation

@crimson-knight

@crimson-knight crimson-knight commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Amber::Pipe::Logger built every debug line before Log dropped it, so at the default info level it wrote nothing yet allocated about 11.8 KB per request. It also parsed the params and created a session on pipelines that have none.

The pipe now holds its Log sources and checks their level first. When no source writes debug lines, it passes the request on and allocates nothing: no clock read, no params, no session. When it does write, each line is built inside the Log block, and headers are read in place instead of copied into a hash.

Measurements

Harness: home-page-memory-bench (mem.rb) on an app from Amber CLI main (2.0.11 template) with the Post scaffold and 50 seeded rows, crystal-alpha build --release (Crystal 1.21.0, LLVM 21.1.8), arm64 macOS, production mode, one process. Route /b/logger/full: the home page fully rendered through Error, SecurityHeaders and Logger. Before = amber v2-dev 814705e, after = this branch. Six rounds per side, interleaved in ABBA order; medians shown. Load runs: oha, 32 connections, 20 s, starting heap 0. The machine was shared with other heavy jobs (load average 40 to 110), so requests/s is approximate; bytes per request is exact.

Metric Before After
Bytes allocated per request (sequential, 10,000 requests) 34,610 22,752 (-11,858)
Bytes allocated per request (under load) 34,470 22,747
Requests/s, 32 connections 14,284 19,188
p50 latency 1.12 ms 0.79 ms
Peak RSS 13.61 MiB 13.48 MiB

The after build allocates exactly what the same render allocates without the Logger pipe (22,745 bytes per request on /b/lean/full).

Specs

  • allocates nothing when the level filters every line out: 100 calls at info level add 0 bytes (the old pipe: 550,256 bytes, so the spec fails against it).
  • writes the request, header, cookie and param lines at debug level, with the filter and skip lists.
  • The existing colorize specs pass unchanged.

At info level Amber::Pipe::Logger built every line before Log dropped it: the time stamp, each header, cookie and param line, and the session (which it also created on pipelines without one). It allocated 11.8 KB per request while writing nothing. The pipe now holds its Log sources and checks their level first; when no source writes debug lines it only passes the request on. When it does write, each line is built inside the Log block and headers are walked in place.
@crimson-knight
crimson-knight merged commit cca5e06 into v2-dev Nov 21, 2026
7 checks passed
@crimson-knight
crimson-knight deleted the perf/logger-skips-filtered-lines branch November 21, 2026 12:37
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