Skip to content

perf: Cut idle memory, response stalls, and large-theme bytes - #5

Merged
Fuwn merged 11 commits into
mainfrom
perf/optimisations
Nov 8, 2026
Merged

Fuwn merged 11 commits into
mainfrom
perf/optimisations

Conversation

@Fuwn

@Fuwn Fuwn commented Nov 8, 2026

Copy link
Copy Markdown
Owner

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

  • Disable Nagle's algorithm on accepted sockets. Under Linux, a response written in more than one piece waited for the client's delayed acknowledgement. @demo served 376 requests per second with a 46 millisecond p99, and lain at padding 12 stalled for up to 115 milliseconds. Production has this stall today.
  • Rewrite rendering around 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.
  • Compile ./themes into 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.
  • Serve images over 64 KiB as gzip to clients that accept it. Cloudflare compresses asoul to 43% of its size, but sent urushi at 98.5% and lain at 99.2%. The generator deflates every glyph at compile time, and each response places those blocks between stored blocks for its dynamic parts, so gzip costs no compression work per request.
  • Recompress the tracked theme images losslessly with oxipng and gifsicle. The tracked PNGs shrink by 14%, lain by 19%.
  • Flush request logs once per burst instead of once per line, which makes logging to a pipe or file 3 to 8% cheaper. A lock holds writers while a flush waits on a full pipe. Without it, a stalled log reader turned 20,001 requests into over 112,000 lines.
  • Add specs for the logger's lock, TCP_NODELAY, rendering sizes, and gzip, and check gzip in test/http_smoke.py.

Compatibility

This change has no database migration or schema change, so rolling back to 0.3.0 is safe. Counter routes, increment semantics, HEAD counting, name encoding, the demo counter, and caching are unchanged. Uncompressed SVG bytes match 0.3.0 for every theme and padding, apart from the four recompressed themes, whose image IDs follow their new bytes.

These changes can affect existing deployments:

  • The image build fixes the set of themes. Adding or changing a theme needs a rebuild, and files placed in /mayu/themes at run time have no effect.
  • MAYU_THEMES still limits which themes visitors can select but no longer reduces memory.
  • Clients that send Accept-Encoding: gzip receive large images gzip-encoded, and every image response sends Vary: Accept-Encoding.
  • The compressed image grows from 23 MB to 31 MB, because it carries each glyph's compressed form.

Validation

  • The format check and all 20 specs passed, including the PostgreSQL 17 integration spec. The workflow's check job passed locally under act.

  • The ARM64 and AMD64 images built and passed the HTTP smoke test and the UID 1000 check. The new logger and TCP_NODELAY specs 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.0 apart from the Vary header.

  • 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.

    Measure 0.3.0 This branch
    Idle memory in docker stats 22 MiB 4 MiB
    Peak memory after large themes 89 MB 22 MB
    @demo 376 requests/s, p99 46 ms 94,000 requests/s, p99 0.7 ms
    urushi 4,900 requests/s 12,500 requests/s
    lain at padding 12 2,800 requests/s, p99 74 ms 9,900 requests/s, p99 8 ms
    Distinct counters 20,000 requests/s 23,100 requests/s
  • Whether 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%:

    curl -s -H 'Accept-Encoding: gzip' -o /dev/null -w '%{size_download}\n' 'https://mayu.due.moe/get/@demo?theme=lain'

Fuwn added 11 commits November 8, 2026 06:44
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.
@Fuwn
Fuwn merged commit 0754490 into main Nov 8, 2026
@Fuwn
Fuwn deleted the perf/optimisations branch November 8, 2026 08:13
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