Skip to content

Add Amber::Pipe::Compression with pooled zlib streams - #1457

Merged
crimson-knight merged 4 commits into
v2-devfrom
perf/pooled-response-compression
Nov 21, 2026
Merged

crimson-knight merged 4 commits into
v2-devfrom
perf/pooled-response-compression

Conversation

@crimson-knight

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

Copy link
Copy Markdown
Member

Crystal's HTTP::CompressHandler creates a new zlib stream for every compressed response, and Compress::Deflate::Writer gives zlib GC.malloc as its allocator. Each response therefore took about 270 KB of zlib state and buffers from the collected heap, which made the generated CompressedResponsePipe the largest allocation in an Amber CLI app: POST /posts, answered with a 302 through :web, allocated 309,857 bytes per request.

Amber::Pipe::Compression writes the same bytes, but its zlib streams come from malloc, outside the collected heap, and go back to a small pool when the response closes (idle_compressors:, 4 by default; a stream beyond that is freed at once, and one dropped without closing is freed by its finalizer). It decides on the first write exactly as the stdlib handler does (gzip first, then deflate, nothing when the response already has a Content-Encoding), answers range requests uncompressed and adds Vary: Accept-Encoding, like the generated pipe. Content-Encoding: deflate follows the compiler it is built with: a zlib stream from Crystal 1.22.0 on (as HTTP::CompressHandler and RFC 9110 do), bare DEFLATE data before.

Request bodies are untouched: #1454's refusal runs in Pipeline#call before any pipe, and a spec checks that a compressed request body is still answered 415 in a pipeline that plugs this pipe.

An Amber CLI app can keep its CompressedResponsePipe name and make it class CompressedResponsePipe < Amber::Pipe::Compression; end; amber_cli will generate that once it pins an Amber commit with this pipe.

Measurements

Harness: home-page-memory-bench (mem.rb, mem_post.rb) on an app from Amber CLI main 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, oha with accept-encoding: gzip, compress, deflate, br, 32 connections, 20 s. Before = amber v2-dev 814705e with the generated CompressedResponsePipe around HTTP::CompressHandler; after = the same app whose CompressedResponsePipe subclasses this pipe. Three rounds per side, interleaved ABBA; medians. The machine was shared with heavy jobs (load average 40 to 270), so requests/s and p99 are approximate; bytes per request and CPU time per request are the steadier figures.

Generated default heap (gc_initial_heap_mib: 16):

Route Metric Before After
GET full page, gzipped Bytes allocated per request 299,887 23,050 (-92%)
Server CPU per request 62.1 µs 55.7 µs
Requests/s 6,872 7,995
p50 2.05 ms 1.86 ms
Peak RSS 23.08 MiB 22.31 MiB
POST /posts (302, form + CSRF) Bytes allocated per request 309,965 33,112 (-89%)
Server CPU per request 84.7 µs 74.7 µs
Requests/s 5,565 6,076
p50 2.81 ms 2.44 ms
Peak RSS 26.28 MiB 25.47 MiB

Starting heap 0 (first batch, same method): GET full page 299,867 -> 23,050 bytes per request, p50 2.45 -> 2.11 ms, peak RSS 14.17 -> 13.16 MiB, 2,111 -> 941 collections in the first pair; POST /posts 309,935 -> 33,108 bytes, p50 3.21 -> 3.14 ms. p99 moved both ways between rounds under the shared load (POST 51.6 vs 56.4 ms in the medians above, 24.2 vs 32.1 ms in the first batch) and showed no direction.

Specs

  • Byte-for-byte equality with HTTP::CompressHandler for gzip and deflate on one small write, one long write, many writes, an empty write, 70 KB of random bytes, and a flush after the first write (the gzip modification time blanked on both sides). Passes on crystal-alpha 1.21 (bare DEFLATE) and Crystal 1.22 (zlib-framed).
  • Round trips through the readers at levels 0, 1, 4 and 9; gzip preferred over deflate; no compression without Accept-Encoding, for a range request, after a flush that sent the headers, or when Content-Encoding is already set.
  • The pool returns and reuses its stream, keeps at most idle_compressors, and the pipe allocates at least 200 KB less per response than HTTP::CompressHandler.
  • A compressed request body is still refused with 415 through a pipeline that plugs the pipe.

HTTP::CompressHandler allocates a new zlib stream (about 270 KB of state and buffers) from the collected heap for every compressed response, which made it the largest allocation in a generated app: POST /posts allocated 309,857 bytes per request. Amber::Pipe::Compression writes the same bytes, but its zlib streams come from malloc, outside the collected heap, and return to a small pool when the response closes. It also skips range requests and adds Vary: Accept-Encoding, as the generated CompressedResponsePipe does, so apps can plug it in place of that file.
Crystal 1.22.0 made HTTP::CompressHandler write a zlib stream for deflate, as RFC 9110 defines it; earlier compilers write bare DEFLATE data. The pipe now picks the same framing at compile time, so its bytes match the handler on every supported compiler.
@crimson-knight
crimson-knight merged commit 54be76f into v2-dev Nov 21, 2026
7 checks passed
@crimson-knight
crimson-knight deleted the perf/pooled-response-compression 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