Repository navigation
Description
Summary
On Appwrite Cloud's API pods, reads served from the cache cost far more CPU than the SQL queries behind them. Most of it is in three places in utopia-php/database 10.2.5, and all three look fixable in PHP without changing behaviour. @abnegate, raising these for you since they're in the cache and definition code you've been reworking.
Profile
Pyroscope CPU profile of the API pods, 12 hours on 2026-11-30, as a share of all API CPU. "Self" is time in the function itself; "with callees" includes what it calls.
| Function | Self | With callees |
|---|---|---|
Database::getDocument |
1.2% | 42% |
Database::findCollection (collection definition reads) |
0.1% | 23% |
Database::createDefinitionInstance |
3.4% | 9.2% |
Database::getCacheKeys |
0.4% | 8.6% |
Document::__clone |
3.8% | 4.4% |
Collection::__clone |
0.1% | 4.0% |
Database::getActiveFilterSignatures |
1.0% | 2.1% |
Cache\Codec\Json::decode (utopia-php/cache) |
0.3% | 8.2% |
PDOStatement::execute, for comparison |
0.8% | 0.9% |
Every getDocument reads its collection's definition through findCollection, so the definition path runs on nearly every request. Profiles from 2026-11-23 show the same shape, so this is steady state, not a spike.
1. createDefinitionInstance (9.2%)
src/Database/Trait/Documents.php:961. On every definition read:
- the cached array that was just decoded is compared with
===against the kept source array: a deep comparison of the whole definition, including every attribute and index; - the kept
Collectionmodel is deep-cloned (Collection::__clone→Document::__clone, together about 8% when counted across all callers).
Ideas:
- Compare a cheap identity instead of the whole array, such as the version or epoch the entry was saved with (
saveWithLeasealready carries a version), or a hash stored with it. - Avoid the deep clone, for example with copy-on-write or with a read-only shared model that copies only when someone mutates it.
- Going further: keep definitions in a per-worker in-memory map keyed by their epoch, so a hit skips the Redis load and the JSON decode altogether. Today each read pays for both.
2. getCacheKeys (8.6%)
src/Database/Database.php:2780. Every call with a document id sorts the selects, json_encodes {selects, relationships, filters} and md5s it. filters comes from getActiveFilterSignatures() (Database.php:2944), which rebuilds the full signature map on every call with array_diff_key and a ksort.
Ideas:
- Memoise the filter signatures. They only change when filters, codecs or exclusions change, so invalidate the memo there.
- Memoise the hash per (filter-signature state, relationships flag, selects). Almost every call has empty or repeated selects, so the
json_encodeandmd5would run once per state instead of on every read.
3. Cache decode (8.3% in json_decode)
This is in utopia-php/cache, not this library, and we're looking into it separately: a faster codec that Rust can also read. It's listed here because idea 1's in-memory definition map would also remove the definition share of this cost.
Context
This came out of the PHP→Rust migration work. Porting the database library to Rust doesn't change the picture here: these are algorithm costs, so fixing them in PHP gets the gain now, and the Rust port would carry the same fixes.
Happy to send PRs for any of these if you'd rather review than write them. Tell me which approach you prefer.
🤖 Generated with Claude Code
Activity
ChiragAgg5k commentedon Nov 30, 2026
PRs for this issue, benchmarked on PHP 8.5 with tracing JIT against real MariaDB and Redis (median CPU µs per cache-hit
getDocument):getCacheKeysdrops from 16 µs to 0.4 µs. The key bytes are unchanged, so the Rust port is unaffected.Codec\Memoizedto utopia-php/cache, which skips the decode when the bytes are unchanged. That also makescreateDefinitionInstance's deep===constant-time, because both sides are the same array.getDocumenthitI tried a cheaper
Document::__cloneand a version token in place of the deep compare, and dropped both; the reasons are in the PR bodies.🤖 Generated with Claude Code
ChiragAgg5k commentedon Nov 30, 2026
Proposal that came out of #1092: sign the built-in filters by name instead of by closure location.
Today a closure filter's cache-key signature is
file:line(computeCallableSignature). The built-in filters are closures insideregisterDefaultFilters(), so any line added toDatabase.phpabove it changes every document cache key:crates/utopia/database/src/database/filters.rs) stop matching. feat(database): the utopia-database crate, with MariaDB and MySQL compared to PHP in CI appwrite/appwrite#16314 now runs a check that fails when they drift.An earlier revision of #1092 hit this: it shifted the closures by 18 lines. That revision was never released, and #1092 now declares its state below the filters, so its keys are byte-identical to
main. But any future edit aboveregisterDefaultFilters()will hit the same trap.Signing built-ins with a stable string, such as
builtin:json:1bumped when the filter's behaviour changes, would make keys independent of source layout. That change itself alters every key once, so it would ship as a deliberate one-time cache flip, coordinated with the Rust side. It's a maintainer call, so I'm raising it here rather than sending a PR.🤖 Generated with Claude Code
perf(cache): memoise filter signatures and document key hashes (#1092)