Skip to content

fix(macOS): avoid Replace All memory spike on huge match counts#250

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-inspection-cd24
Draft

fix(macOS): avoid Replace All memory spike on huge match counts#250
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-inspection-cd24

Conversation

@cursor

@cursor cursor Bot commented May 18, 2026

Copy link
Copy Markdown

Bug and impact

Replace All in the macOS note editor built an in-memory array of every match NSRange before 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

replaceAllMatches used a Swift String scan that appended every NSRange into [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 .caseInsensitive over 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 install and npm test in web/ (257 tests) — all passed. (Change is macOS-only; no Swift test runner in this Linux environment.)
  • No new linter issues on EditorView.swift.
Open in Web View Automation 

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>
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