-
Notifications
You must be signed in to change notification settings - Fork 2.6k
1 of 3: memfix: implement and use safeReadJson to reduce memory usage #5332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@daniel-lxs please move this into review |
|
stand by on merging, I have a small change for this, moving back to draft |
|
rebased on origin/main, ready for review |
daniel-lxs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @KJ7LNW!
I left a couple of suggestions, let me know what you think!
053c02f to
0fe6cda
Compare
|
@daniel-lxs I resolved your suggested changes and rebased on v3.23.12 |
daniel-lxs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @KJ7LNW, looks good to me!
Implement safeReadJson function to complement the existing safeWriteJson functionality: - Uses stream-json for efficient processing of large JSON files - Supports both full object reading and selective path extraction - Provides file locking to prevent concurrent access - Includes comprehensive error handling - Adds complete test coverage - Passthrough all exceptions This enables efficient and safe JSON reading operations throughout the codebase. Signed-off-by: Eric Wheeler <[email protected]>
Replace manual file reading and JSON parsing with the safer safeReadJson utility across multiple files in the codebase. This change: - Provides atomic file access with proper locking to prevent race conditions - Streams file contents efficiently for better memory usage - Improves error handling consistency - Reduces code duplication Fixes: #5331 Signed-off-by: Eric Wheeler <[email protected]>
Updated test files to properly mock and use safeReadJson/safeWriteJson: - Added proper imports for safeReadJson from safeWriteJson module - Updated mock implementations to mock both functions correctly - Replaced direct fs operations with calls to safe functions - Updated assertions to match the new behavior This fixes all failing tests after the conversion to safeReadJson. Signed-off-by: Eric Wheeler <[email protected]>
0fe6cda to
b0ec3f4
Compare
|
rebased on v3.23.14 |
|
Thanks for the contribution. At this stage, the impact of these changes isn’t clear and we’re focusing on higher-priority items. Closing for now, but we can reconsider in the future if priorities shift. |
Dependencies
Note to Reviewer
The commits tell a clean story, it will be easier to understand what is happening here by looking at each commit individually under "Commits" than by looking at all of the files that were changed.
This PR introduces
safeReadJson, a new utility to read JSON files safely and efficiently, reducing memory pressure when loading large JSON files by streaming them instead of double-buffering viaJSON.parse.Key Changes
safeReadJsonUtility:src/utils/safeReadJson.tsthat usesstream-jsonto parse files without buffering them entirely into memory.proper-lockfileto prevent race conditions during reads, sharing the lock mechanism withsafeWriteJson.Codebase Adoption:
JSON.parse(fs.readFile(...))withsafeReadJsonacross the codebase to improve performance and consistency.Refactoring:
safeWriteJson.tshas been extracted into a shared_acquireLockfunction, now used by bothsafeReadJsonandsafeWriteJson.Testing:
safeReadJsonutility.How to Test
Get in Touch
Discord: KJ7LNW
Important
Introduces
safeReadJsonfor efficient JSON reading, refactors codebase to use it, and adds comprehensive tests.safeReadJsoninsrc/utils/safeReadJson.tsfor efficient JSON file reading using streaming and file locking.JSON.parse(fs.readFile(...))withsafeReadJsoninmodelCache.ts,modelEndpointCache.ts, andimportExport.ts._acquireLockshared bysafeReadJsonandsafeWriteJson.safeReadJsoninsafeReadJson.spec.ts.safeReadJsoninimportExport.spec.tsandcache-manager.spec.ts..roo/rules/use-safeReadJson.mdto enforcesafeReadJsonusage.This description was created by
for bb27ee94901f66f2492dc1327f0ca351c0ca1e5b. You can customize this summary. It will automatically update as commits are pushed.