Skip to content

perf: cache document structures to avoid repeated disk reads#4

Merged
mrjunos merged 1 commit into
mainfrom
feat/structure-cache
Apr 7, 2026
Merged

perf: cache document structures to avoid repeated disk reads#4
mrjunos merged 1 commit into
mainfrom
feat/structure-cache

Conversation

@mrjunos

@mrjunos mrjunos commented Apr 7, 2026

Copy link
Copy Markdown
Owner

Summary

Each chat request was reading and parsing the PageIndex JSON structure for every indexed document on every call — O(n) disk I/O per request.

Fix: get_structure() now caches results in _structure_cache on first call. Cache is invalidated when a document is re-indexed.

def get_structure(self, doc_id: str) -> str:
    if doc_id not in self._structure_cache:
        self._structure_cache[doc_id] = self.client.get_document_structure(doc_id)
    return self._structure_cache[doc_id]

Tests added (46 total, up from 38)

New test_indexer.py (8 tests):

  • First call hits client, second call uses cache
  • Cache returns consistent values
  • Cache invalidated after re-index
  • Independent caching per document
  • Deduplication: existing doc not re-indexed, new doc is indexed

Test plan

  • 46 tests passing
  • CI green

🤖 Generated with Claude Code

get_structure() now caches results in _structure_cache on first call.
Cache is invalidated when a document is re-indexed.

Without this, every chat request reads and parses the PageIndex JSON
for all indexed documents on every call — O(n) disk I/O per request.

Also adds test_indexer.py (8 tests) covering cache hits, cache misses,
independent caching per doc, cache invalidation, and dedup logic.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@mrjunos
mrjunos merged commit 09d71b2 into main Apr 7, 2026
1 check passed
@mrjunos
mrjunos deleted the feat/structure-cache branch April 7, 2026 15:16
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