Repository navigation
perf: Cut idle memory, response stalls, and large-theme bytes - #5
Merged
Merged
Conversation
A response written in more than one piece left its last segment waiting for the client's delayed acknowledgement. Under Linux, @demo served 376 requests per second with a 46 ms p99, and large themes stalled for up to 115 ms.
Glyphs are looked up by slot and each image's exact length is known before writing. Images up to 64 KiB are built once and sent in one write; larger ones stream instead of growing the garbage-collected heap, which had reached 195 MB after serving the largest themes.
A generator encodes ./themes at compile time into string literals in the binary's read-only pages, so a theme is read into memory only once it is served. An idle container falls from 22 MiB to 4 MiB, and start-up from 85 ms to 32 ms. Adding or changing a theme now needs a rebuild.
oxipng and gifsicle shrink the tracked PNGs by 14%, lain by 19%. Every decoded image matches its original, including each composed frame and delay of the animated GIFs.
Cloudflare compresses small images well but sent urushi at 98.5% and lain at 99.2% of their size. Images over 64 KiB are now gzip for clients that accept it, assembled from glyphs deflated at compile time, so large themes send 25% fewer bytes at no compression cost per request.
A flush waiting on a full pipe let request fibers append to the buffer it was writing, and resetting the buffer afterwards lost or repeated lines. With the log reader stalled for three seconds, 20,001 requests logged over 112,000 lines.
Comments that restated the code are gone. Rendering#compressible? decides when gzip is served, and the generator compresses every glyph rather than estimating which themes can exceed the buffering limit.
A glyph built from its parts compresses its definition on the spot, so the compressed form is never missing. That removes the struct copy, the nil checks, and Rendering#compressible?; gzip now depends only on whether an image streams. Comments that restated the code are trimmed.
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.
Remove a delayed-acknowledgement stall that held multi-write responses for up to 115 milliseconds, stop large themes from growing the heap, compile themes into the binary, and serve large images as precompressed gzip. An idle container falls from 22 MiB to 4 MiB, peak memory from 89 MB to 22 MB, and large themes send a quarter fewer bytes at two to three times the throughput.
Changes
@demoserved 376 requests per second with a 46 millisecond p99, andlainat padding 12 stalled for up to 115 milliseconds. Production has this stall today.Theme::Rendering, which looks glyphs up by slot and knows an image's exact length before writing it. Mayu builds an image of up to 64 KiB once and sends it in one write. Larger images stream, so a megabyte-sized render no longer grows the garbage-collected heap, which had reached 195 MB../themesinto the binary. A generator run by the compiler encodes every theme into string literals in the binary's read-only pages, so Mayu reads a theme into memory only when it first serves that theme. Start-up falls from 85 to 32 milliseconds.TCP_NODELAY, rendering sizes, and gzip, and check gzip intest/http_smoke.py.Compatibility
This change has no database migration or schema change, so rolling back to
0.3.0is safe. Counter routes, increment semantics, HEAD counting, name encoding, thedemocounter, and caching are unchanged. Uncompressed SVG bytes match0.3.0for every theme and padding, apart from the four recompressed themes, whose image IDs follow their new bytes.These changes can affect existing deployments:
/mayu/themesat run time have no effect.MAYU_THEMESstill limits which themes visitors can select but no longer reduces memory.Accept-Encoding: gzipreceive large images gzip-encoded, and every image response sendsVary: Accept-Encoding.Validation
The format check and all 20 specs passed, including the PostgreSQL 17 integration spec. The workflow's
checkjob passed locally underact.The ARM64 and AMD64 images built and passed the HTTP smoke test and the UID 1000 check. The new logger and
TCP_NODELAYspecs and the smoke test's gzip check each fail without their fix.Every theme at paddings 0, 6, and 12 returned gzip that decompressed to exactly the uncompressed SVG. Uncompressed responses were byte-identical to
0.3.0apart from theVaryheader.All 592 theme images decode to the same pixels after recompression, including each composed frame and delay of the animated GIFs.
Benchmarks ran in Docker on ARM64, with the load generator inside each container's network namespace and the two images alternating over three rounds. Hot-counter throughput was unchanged.
0.3.0docker stats@demoWhether Cloudflare passes Mayu's gzip through unchanged can only be checked after deploying. This request does not count, and should report about 75% of the uncompressed size rather than 99%: