fix(macOS): avoid Replace All memory spike on huge match counts#250
Draft
cursor[bot] wants to merge 1 commit into
Draft
fix(macOS): avoid Replace All memory spike on huge match counts#250cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
Replace All previously collected every match NSRange before mutating the text storage. Notes with huge match counts could allocate unbounded memory and freeze the UI. Use NSMutableString.replaceOccurrences for the same non-overlapping case-insensitive semantics without materializing all ranges. Co-authored-by: Danny Peck <dannypeck@gmail.com>
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.
Bug and impact
Replace All in the macOS note editor built an in-memory array of every match
NSRangebefore applying edits. A large note with a very common search string (for example replacing a single repeated character or newline) could create millions of ranges on the main thread, causing severe memory growth and a prolonged UI freeze (watchdog or user force-quit in the worst case).Root cause
replaceAllMatchesused a SwiftStringscan that appended everyNSRangeinto[NSRange], then applied replacements from that list. Match-highlighting paths already cap visible matches for performance, but Replace All had no equivalent guard and paid O(match count) extra memory.Fix
Use
NSMutableString.replaceOccurrences(of:with:options:range:)with.caseInsensitiveover the full string. This performs non-overlapping forward replacements consistent with the prior loop, without materializing every range. When the replacement count is zero, the handler still refreshes highlights so the find UI stays in sync.Validation
npm installandnpm testinweb/(257 tests) — all passed. (Change is macOS-only; no Swift test runner in this Linux environment.)EditorView.swift.