Skip to content

perf(corpus): cache the SHA-256 version fingerprint#7

Merged
silversurfer562 merged 1 commit into
mainfrom
perf/cache-corpus-version
May 8, 2026
Merged

perf(corpus): cache the SHA-256 version fingerprint#7
silversurfer562 merged 1 commit into
mainfrom
perf/cache-corpus-version

Conversation

@silversurfer562
Copy link
Copy Markdown
Member

Spotted in this morning's code-review. DirectoryCorpus.version re-hashed every entry on every call. The property is the cache key for attune-gui's /api/rag routes, so the cost compounds with template count and request rate.

Cache the result on the instance, invalidate inside _ensure_loaded whenever entries are rebuilt. Two new tests cover the cache (patches hashlib.sha256, asserts one call across three reads) and the invalidation (sentinel + forced rebuild).

300 passed, 3 xpassed.

🤖 Generated with Claude Code

``DirectoryCorpus.version`` hashed every entry on every call. Spotted
in this morning's code-review (#code-review-2026-05-07): the version
property is the cache key for attune-gui's /api/rag routes — hashing
the full corpus on every API request is the kind of N-per-request work
that turns a 5ms route into a 50ms route once a corpus has hundreds of
templates.

Cache the fingerprint on the instance, invalidate inside
``_ensure_loaded`` whenever the entries are rebuilt (which already
happens on file changes when ``cache=False``, and is a no-op when
``cache=True`` after the first load).

Tests:
- ``test_version_cached_after_first_computation`` — patches ``hashlib.sha256``
  and asserts it's called exactly once across three ``version`` reads.
- ``test_version_invalidated_when_corpus_reloaded`` — sets a sentinel,
  forces a rebuild, confirms the recomputed hash overwrites the sentinel.

300 passed, 3 xpassed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@silversurfer562 silversurfer562 merged commit ba713c0 into main May 8, 2026
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