perf: Add cache to speed up reference resolution#20
Draft
ryodine wants to merge 9 commits intofeat/shorthandfrom
Draft
perf: Add cache to speed up reference resolution#20ryodine wants to merge 9 commits intofeat/shorthandfrom
ryodine wants to merge 9 commits intofeat/shorthandfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a file-scoped cache to reduce repeated file reads and YAML parsing during !reference / !reference-all resolution, aiming to improve performance for workloads that repeatedly touch the same files.
Changes:
- Added a
FileCacheimplementation for memoizing parsed YAML and (intended) reusing raw file contents. - Threaded an optional cache through
parseYamlWithReferences*and the resolver’s recursive reference resolution. - Added a dedicated
FileCacheunit test suite.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
src/cache.ts |
Adds the cache implementation and keying scheme. |
src/parser.ts |
Adds cache-aware fast paths and file-content reuse during parsing. |
src/resolver.ts |
Instantiates and threads the cache through all resolution calls. |
src/index.ts |
Exposes FileCache as a public export. |
test/cache.test.ts |
Adds unit tests for cache behaviors and edge cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+63
to
+69
| // Optimization: If we have an anchor and fileContent, also store whole file for future anchor extraction | ||
| if (anchor && fileContent) { | ||
| const wholeFileKey = this.createCacheKeyString(filePath); // No anchor = whole file | ||
| // Only store if whole file isn't already cached | ||
| if (!this.cache.has(wholeFileKey)) { | ||
| this.cache.set(wholeFileKey, { content, fileContent }); | ||
| } |
Comment on lines
+21
to
+23
| */ | ||
| private createCacheKeyString(filePath: string, anchor?: string): string { | ||
| return `${filePath}|${anchor || ""}`; |
Comment on lines
+112
to
+116
| if (options?.cache) { | ||
| const cached = options.cache.get(absolutePath, options.extractAnchor); | ||
| if (cached !== undefined) { | ||
| return cached; | ||
| } |
Comment on lines
+121
to
+126
| const cachedContent = options?.cache?.getFileContent(absolutePath); | ||
| if (cachedContent) { | ||
| content = cachedContent; | ||
| } else { | ||
| content = fsSync.readFileSync(absolutePath, "utf8"); | ||
| } |
Comment on lines
+175
to
+182
| // Get file content from cache or disk | ||
| let content: string; | ||
| const cachedContent = options?.cache?.getFileContent(absolutePath); | ||
| if (cachedContent) { | ||
| content = cachedContent; | ||
| } else { | ||
| content = await fs.readFile(absolutePath, "utf8"); | ||
| } |
Comment on lines
+123
to
+130
| // Should have 2 entries: anchor-specific and whole file | ||
| expect(cache.size()).toBe(2); | ||
|
|
||
| // Anchor-specific entry | ||
| expect(cache.get(filePath, anchor)).toEqual(content); | ||
|
|
||
| // Whole file entry for optimization | ||
| expect(cache.get(filePath)).toEqual(content); |
Collaborator
Author
|
@copilot open a new pull request to apply changes based on the comments in this thread |
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.
Implements file-level memoization to reduce redundant file I/O and parsing operations during
!referenceand!reference-all resolution. This optimization provides significant performance improvements, especially for workloads with repeated file access patterns.In the following test, it resulted in over 100% speedup: