Repository navigation
perf(cache): reuse the decoded envelope while the stored string is unchanged - #13915
ChiragAgg5k wants to merge 1 commit into
Conversation
…changed Hot keys (collection metadata, the project, the user) are read from Redis on every request and their JSON decoded each time, although the stored string rarely changes. Redis and Multiplexing now keep a small per-process memo of decoded envelopes and reuse one only while the stored string is byte-for-byte unchanged. Ported from utopia-php/monorepo#279. Co-authored-by: loks0n <[email protected]>
|
| if ($entry !== null && $entry[0] === $value) { | ||
| return $entry[1] + $ttl > $now ? $entry[2] : false; |
There was a problem hiding this comment.
Mutable objects are shared If a large cached value contains a mutable object and one caller modifies it, a later load returns that same object while Redis still holds the unchanged string. The later caller can therefore receive data that was never saved. The supported Igbinary codec can restore objects, and the JSON codec preserves empty objects as
stdClass. Return an independent value on each load or avoid memoizing mutable values.
How this was verified: A byte-identical cache hit returns the retained decoded value, and both supported codecs can produce mutable objects.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/cache/src/DecodedEnvelopes.php
Line: 39-40
Comment:
**Mutable objects are shared** If a large cached value contains a mutable object and one caller modifies it, a later load returns that same object while Redis still holds the unchanged string. The later caller can therefore receive data that was never saved. The supported Igbinary codec can restore objects, and the JSON codec preserves empty objects as `stdClass`. Return an independent value on each load or avoid memoizing mutable values.
**How this was verified:** A byte-identical cache hit returns the retained decoded value, and both supported codecs can produce mutable objects.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| if ($oldest !== null && \count($this->entries) >= $this->capacity) { | ||
| unset($this->entries[$oldest]); | ||
| } | ||
| $this->entries[$key] = [$value, $envelope['time'], $envelope['data']]; |
There was a problem hiding this comment.
Large entries lack a byte limit The 128-entry cap limits the number of keys, but each entry retains both the full stored string and its decoded data. With no maximum value size or total byte budget, large cache values can keep substantial memory resident for the adapter’s lifetime. Consider limiting memoized value size or total retained bytes.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/cache/src/DecodedEnvelopes.php
Line: 53
Comment:
**Large entries lack a byte limit** The 128-entry cap limits the number of keys, but each entry retains both the full stored string and its decoded data. With no maximum value size or total byte budget, large cache values can keep substantial memory resident for the adapter’s lifetime. Consider limiting memoized value size or total retained bytes.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| $this->assertSame(2, $memo->count()); | ||
| // 'a' was evicted, so it decodes again and pushes 'b' out in turn. | ||
| $this->assertSame('a', $this->marker($memo->decode('a', $a, 60, 110))); | ||
| $this->assertSame(2, $memo->count()); |
There was a problem hiding this comment.
Tests assert memo internals These checks use
count() and assume a particular eviction order instead of testing an observable decoding outcome. Similar occupancy checks appear in the repeated-load, malformed-input, and small-payload tests. The repository requires tests of observable behavior rather than assertions that mirror implementation details; this requirement should be satisfied before merging.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/cache/tests/DecodedEnvelopesTest.php
Line: 100-103
Comment:
**Tests assert memo internals** These checks use `count()` and assume a particular eviction order instead of testing an observable decoding outcome. Similar occupancy checks appear in the repeated-load, malformed-input, and small-payload tests. The repository requires tests of observable behavior rather than assertions that mirror implementation details; this requirement should be satisfied before merging.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
|
@loks0n your utopia-php/monorepo#279 is ported here, now that |
|
Closing this port. @loks0n, re-open it if you still want the change in |
The Redis and Multiplexing adapters keep a small per-process memo of decoded envelopes (128 keys, payloads under 1 KB pass through) and reuse one only while the stored string is byte-for-byte unchanged, so hot keys stop paying
json_decodeon every load. Redis stays the source of truth, and TTL is still applied on every hit.Ported from utopia-php/monorepo#279 after cache moved to packages/cache (#13899). Adapted to the codec-aware
Envelopeinstance:DecodedEnvelopessits besideUtopia\Cache\Envelopeand takes the adapter's envelope, andEnvelope::unwrap()is now public, returning{time, data}or false.bin/monorepo check cache(Pint, PHPStan max, Rector)appwrite/appwrite:2.3.0image, since host lacks igbinary)bin/monorepo validate,git diff --check🤖 Generated with Claude Code