Repository navigation
Tep's gzip cache answers a repeated body before hashing it - #741
Merged
Merged
Conversation
Tep.gzip_cached computed SHA-256 of the whole identity body on every request, hits included, to find the cached gzip. In a perf profile of the Spinel binary serving Campfire's older-messages page (408 KB, gzip), 40% of the samples were in that digest. The CRuby overlay's GzipCache (rubys#488) compares the last body served with `==` before any key, and Tep now does the same. The pair (last body, its gzip) is stored on a digest hit, a body seen before, and not on a miss: a page with a per-request CSRF token never repeats, so copying it would be wasted. It is read under the lock and compared outside it. The stored body is a `dup`: on Spinel a String can be a shared mutable buffer, and a caller that mutated it afterwards would otherwise make `==` match bytes whose gzip this is not. A miss runs as before, and the digest-keyed table is unchanged (SHA-256, rubys#432). Test: tests/gzip_cache.rs tep_gzip_cached_repeats_the_last_body_without_a_digest counts digests and gzips: a miss keeps no copy, a digest hit becomes the last body, a repeat of it runs neither, a same-length body misses and leaves the last body in place, a mutated source misses, and the pair survives the table emptying at GZIP_CACHE_MAX. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FpReMQmF4AACf5cmD9N6Cq
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.
What
Tep.gzip_cached, which gzips identity HTML on the Spinel lane, looks up the gzip by the SHA-256 of the body (#432: Spinel's Hash reads the whole key, and with no GVL the lock had to stay short). It computed that digest on every request, hits included. In aperfprofile of the Spinel binary serving Campfire's older-messages page (408,174 bytes, gzip, one client; binary fromadee8959, samegzip_cached), 40% of the samples were insp_crypto_sha256_block.The CRuby overlay's
GzipCache(#488) compares the last body served with==before any key. Tep now does the same:GZIP_LOCKand compared outside it;dup. On Spinelrawcan be a shared mutable string handle (SP_BUILTIN_STRBUF), and a caller that mutated it afterwards would make==match bytes whose gzip this is not. The overlay snapshots for the same reason (last_hit_does_not_follow_a_mutated_source_string);Cost: the last repeated body stays in memory (up to the largest gzipped page; 7.4 MB for a sidebar with 10,000 direct rooms in our seed), and a digest hit copies the body once. Two repeated pages that alternate (A, B, A, B) both go through the digest every time, as before, plus that copy.
Pages with a per-request token never repeat byte for byte, so they still go through the digest. That is what the overlay's splicing (
85c9f67d, then #526 and #546) solves on CRuby; it is not part of this change.Repro and test
wrkon main (below).gzip_cache::tep_gzip_cached_repeats_the_last_body_without_a_digestloadstep_core.rbon CRuby and countsDigest::SHA256.hexdigestandZlib.gzipcalls. A miss keeps no copy; the same bytes again are a digest hit and become the last body; a fresh String with those bytes then runs neither; a body of the same length with one byte changed misses and leaves the last body in place; a caller that mutates its String after the call misses; the pair survives the table emptying atGZIP_CACHE_MAX; an empty body with nothing served yet is gzipped; four threads alternating three bodies of one length always get the gzip of their own body. Every answer is checked withZlib.gunzip. On main it fails atlast-hit ran the digest (3). Five wrong versions fail it too: storingrawwithoutdup, comparingbytesizeonly, dropping the empty-pair guard, storing the pair on a miss, and not storing it on a digest hit.Verification
Run locally:
cargo test --test gzip_cache: 14 passed, 1 ignored (JRuby).store-check, unit (4,870 passed, 217 ignored),compare-ruby, the Campfire suite (417 tests, 69 files),campfire-compareanddb-differentialon the Ruby target. All green.91233b2d; the branch is the same patch rebased on0ffebca2.32b4144), built byscripts/build-campfire-archivefrom main (91233b2d) and from91233b2dwith this change, with the same Spinel (2afc69c54). Both binaries ran in the same image on 4 CPUs (AMD Ryzen 9 7940HX, Docker Desktop on Windows 11), one at a time, against the same database (2M messages, 1M of them in room 1).On our machine,
wrk -t2 -c16on 4 other CPUs, gzip, 4 s discarded and 10 s measured, 3 rounds in rotated order, median (min–max):CPU per request on the messages page goes from 1.70 ms to 0.57 ms (4 CPUs / req/s). The earlier version that also stored the pair on a miss was measured in a separate series, with another job on the machine's other CPUs: messages ×2.88, search 355.0 → 346.1 req/s, room unchanged.
Not run: hosted CI;
compare-ruby-next; the Spinel and Campfire CI jobs, apart from the Campfire build above. The thread test runs on CRuby, under the GVL: on Spinel the pair is still written and read under one lock and the snapshot is never mutated, but no Spinel run exercised the race.🤖 Generated with Claude Code
Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.
Summary by CodeRabbit