Skip to content

Cache-hit reads: definition deep-compare/clone and per-read cache-key hashing cost ~26% of API CPU #1089

Backlog

Description

@ChiragAgg5k

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 Collection model 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 (saveWithLease already 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_encode and md5 would 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

  1. ChiragAgg5k commented on Nov 30, 2026

    @ChiragAgg5k
    MemberAuthor

    PRs for this issue, benchmarked on PHP 8.5 with tracing JIT against real MariaDB and Redis (median CPU µs per cache-hit getDocument):

    getDocument hit before both PRs
    20 attributes, JSON cache 170.9 85.4
    80 attributes, JSON cache 343.3 152.3
    20 attributes, igbinary cache 118.8 89.3
    80 attributes, igbinary cache 226.8 127.5

    I tried a cheaper Document::__clone and a version token in place of the deep compare, and dropped both; the reasons are in the PR bodies.

    🤖 Generated with Claude Code

  2. ChiragAgg5k commented on Nov 30, 2026

    @ChiragAgg5k
    MemberAuthor

    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 inside registerDefaultFilters(), so any line added to Database.php above it changes every document cache key:

    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 above registerDefaultFilters() will hit the same trap.

    Signing built-ins with a stable string, such as builtin:json:1 bumped 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions