Skip to content

Conversation

@CybotTM
Copy link
Contributor

@CybotTM CybotTM commented Jan 22, 2026

Summary

Adds O(1) caching for frequently called ProjectNode methods:

  • getRootDocumentEntry(): Caches root document entry on add, eliminating O(n) scan
  • getDocumentEntry($file): Direct hash lookup by file path instead of iteration
  • setDocumentEntries(): Re-keys array properly via addDocumentEntry() to maintain invariants

Changes

ProjectNode.php

  • Added $rootDocumentEntry cache property
  • addDocumentEntry(): Caches root entry if isRoot() is true
  • getRootDocumentEntry(): Returns cached root directly (O(1))
  • getDocumentEntry(): Uses isset() for O(1) lookup
  • setDocumentEntries(): Delegates to addDocumentEntry() to maintain cache
  • reset(): Clears cache

FunctionalTest.php

  • Updated test to mark document as root via withIsRoot(true) before compilation
  • Removed pre-creation of DocumentEntryNode (let compiler create it correctly)

Rationale

For a project with 500 documents, getRootDocumentEntry() and getDocumentEntry() were O(n) iterations called during rendering. Now they're O(1) lookups.

Performance Impact

See Performance Analysis Report for detailed benchmarks.


Related PRs

PR Description Status
#1287 Rendering caching layer Independent
#1288 RST parsing optimizations Independent
#1289 CLI container caching Independent
#1290 DocumentEntryNode identity refactoring Independent
#1292 This PR - ProjectNode O(1) document lookup

All PRs can be merged independently in any order.

@CybotTM CybotTM marked this pull request as draft January 22, 2026 20:22
- Cache root document entry for O(1) getRootDocumentEntry()
- Use direct hash lookup for getDocumentEntry() instead of iteration
- Properly rekey array in setDocumentEntries() via addDocumentEntry()
- Invalidate cache on reset()

The functional test is updated to mark the document as root before
compilation (using withIsRoot(true)) so the compiler creates the
DocumentEntryNode correctly, rather than pre-creating an entry that
gets overwritten by the compiler.
@CybotTM
Copy link
Contributor Author

CybotTM commented Jan 23, 2026

Closing in favor of a cleaner PR with distinct commits for: (1) preparatory refactor, (2) performance improvement, (3) test adaptation. See new PR for combined changes.

@CybotTM CybotTM closed this Jan 23, 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