perf: O(1) document entry lookups in ProjectNode#1293
Merged
jaapio merged 3 commits intophpDocumentor:mainfrom Jan 24, 2026
Merged
perf: O(1) document entry lookups in ProjectNode#1293jaapio merged 3 commits intophpDocumentor:mainfrom
jaapio merged 3 commits intophpDocumentor:mainfrom
Conversation
Replace object identity comparison (===) with file path comparison when matching DocumentEntryNode instances. The file path is the natural unique identifier for a document entry. This change: - Makes the code more robust to serialization/deserialization (cached objects create new instances) - Aligns with how SectionEntryNode comparison already works - Enables future optimizations that may cache or recreate entry objects - Is a prerequisite for O(1) lookup caching in ProjectNode Changed locations: - GlobalMenuPass: root document lookup - ToctreeValidationPass: root document check - RenderContext: getDocumentNodeForEntry() - DocumentTreeIterator: current() No behavior change - documents are still matched by their file path, which was always the semantic intent.
Replace O(n) iteration with O(1) hash-based lookups for document entry retrieval in ProjectNode. Changes: - Cache root document entry for instant getRootDocumentEntry() - Use direct hash lookup in getDocumentEntry() by file path - Properly rekey array in setDocumentEntries() via addDocumentEntry() - Invalidate cache on reset() - Update type annotation to array<string, DocumentEntryNode> Performance impact: - getRootDocumentEntry(): O(n) -> O(1) - getDocumentEntry(): O(n) -> O(1) This optimization is enabled by the previous commit's refactoring from object identity to file path comparison, which ensures lookups work correctly even with cached/deserialized document entries.
Update FunctionalTest to mark the document as root using withIsRoot(true) before compilation, rather than pre-creating a DocumentEntryNode that gets overwritten by the compiler. This is the correct approach because: - The compiler creates DocumentEntryNode during compilation - Pre-creating an entry that gets replaced is misleading - withIsRoot(true) signals intent to the compiler Removed unused imports: DocumentEntryNode, TitleNode
This was referenced Jan 23, 2026
jaapio
approved these changes
Jan 24, 2026
Member
|
Thanks, this makes a lot of sense. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Optimizes document entry lookups in
ProjectNodefrom O(n) iteration to O(1) hash-based lookups.This PR contains 3 distinct commits, each addressing a specific concern:
1. Preparatory Refactor: File path comparison (4 files)
Replaces object identity comparison (
===) with file path comparison when matchingDocumentEntryNodeinstances.Why this matters:
Changed files:
GlobalMenuPass.php- root document lookupToctreeValidationPass.php- root document checkRenderContext.php-getDocumentNodeForEntry()DocumentTreeIterator.php-current()2. Performance Improvement: O(1) caching (1 file)
Adds hash-based O(1) lookups to
ProjectNode:Changes to
ProjectNode.php:getRootDocumentEntry()getDocumentEntry()by file pathsetDocumentEntries()viaaddDocumentEntry()reset()DocumentEntryNode[]→array<string, DocumentEntryNode>3. Test Adaptation (1 file)
Updates
FunctionalTest.phpto usewithIsRoot(true)instead of manually creating aDocumentEntryNodethat gets overwritten by the compiler.Performance Impact
getRootDocumentEntry()getDocumentEntry($file)This is particularly impactful for large documentation projects with hundreds or thousands of documents.
See Performance Analysis Report for detailed benchmarks.
Test Plan
Related PRs
All PRs can be merged independently in any order.