-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_msg.txt
More file actions
7 lines (4 loc) · 1.57 KB
/
Copy pathcommit_msg.txt
File metadata and controls
7 lines (4 loc) · 1.57 KB
1
2
3
4
5
6
7
⚡ Optimize NSRegularExpression match substring extraction
💡 **What:** Replaced the `Range(contentRange, in: string)` Swift `String.Index` translation with `NSString`'s `.substring(with:)` across all inline regex processors in `InlineProcessor.swift`.
🎯 **Why:** Translating an `NSRange` to a Swift `Range<String.Index>` requires traversing the String from the beginning since Swift Strings use UTF-8/UTF-16 encoding dynamically and count characters by grapheme clusters (O(N) operation for index translations). Doing this inside a loop proportional to the number of matches causes O(N*M) or O(N^2) complexity where N is the text length. Additionally, casting substring slices directly to `String(string[contentSwiftRange])` incurs extra memory allocations. By casting the base string to `NSString` once and using `.substring(with:)`, we utilize O(1) direct integer-offset memory reads, saving huge amounts of CPU cycles and memory allocations for documents with many format matches.
📊 **Measured Improvement:** Standard Swift build tools (`swiftc`, `xcodebuild`) were unavailable in the development environment to execute benchmarks. However, the theoretical algorithmic improvement is changing the inner loop extraction string lookup from O(N) index traversal to O(1) integer-based substring bounds lookup per match. For a text containing hundreds of formatting directives (e.g. `**bold**`), this saves N traversals, translating to a substantial performance increase during the RTF conversion process and significantly fewer ARC allocations due to substring memory bridging optimizations.