Repository navigation
Add Amber::Pipe::Compression with pooled zlib streams - #1457
Merged
Merged
Conversation
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.
…e-compression # Conflicts: # CHANGELOG.md
This was referenced Nov 21, 2026
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.
Crystal's
HTTP::CompressHandlercreates a new zlib stream for every compressed response, andCompress::Deflate::Writergives zlibGC.mallocas its allocator. Each response therefore took about 270 KB of zlib state and buffers from the collected heap, which made the generatedCompressedResponsePipethe largest allocation in an Amber CLI app: POST /posts, answered with a 302 through:web, allocated 309,857 bytes per request.Amber::Pipe::Compressionwrites the same bytes, but its zlib streams come frommalloc, 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 aContent-Encoding), answers range requests uncompressed and addsVary: Accept-Encoding, like the generated pipe.Content-Encoding: deflatefollows the compiler it is built with: a zlib stream from Crystal 1.22.0 on (asHTTP::CompressHandlerand RFC 9110 do), bare DEFLATE data before.Request bodies are untouched: #1454's refusal runs in
Pipeline#callbefore 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
CompressedResponsePipename and make itclass 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 withaccept-encoding: gzip, compress, deflate, br, 32 connections, 20 s. Before = amber v2-dev 814705e with the generatedCompressedResponsePipearoundHTTP::CompressHandler; after = the same app whoseCompressedResponsePipesubclasses 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):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
HTTP::CompressHandlerfor 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).Accept-Encoding, for a range request, after a flush that sent the headers, or whenContent-Encodingis already set.idle_compressors, and the pipe allocates at least 200 KB less per response thanHTTP::CompressHandler.