perf: optimized validString in CommentedString#1067
perf: optimized validString in CommentedString#1067ChrisBenua wants to merge 4 commits intotuist:mainfrom
Conversation
|
@dosubot could you review this PR? It optimized |
|
Thanks for working on this performance optimization! The 5-20x improvement benchmarks are impressive, especially the 20x boost for long strings (119µs → 5.5µs). Here's my review: ✅ What Looks Good
|
fortmarek
left a comment
There was a problem hiding this comment.
🤯
Thanks a lot for this PR, the improvements are impressive. I have a couple minor nits, but otherwise, looks good!
|
@fortmarek Marek, could you please re-run the workflows and re-approve PR? I apologize for not checking the linting rules before opening the PR. I've run mise run lint locally, and while it flagged several existing Swift files in XcodeProj for not conforming to current linting standards, I’ve verified that the changes in this PR adhere to the linting rules. Thank you for your understanding! |
Resolves #1066
Short description 📝
Solution 📦
StringProtocol.containswith checking that oneUInt8array (specificallyUnsafeBufferPointer) is subarray of another. Why this approach is faster? BecauseStringProtocol.containsspends most of its time on string indexing (which requires much logic to preserve grapheme clusters).rangeOfCharacterwith checking thatUInt8array (specificallyUnsafeBufferPointer) contains specific value.appendfor each string character inreducefunction - calculate needed capacity upfront and createStringusingString(unsafeUninitializedCapacity:)initImplementation 👩💻👨💻
StringProtocol.containsandrangeOfCharacterswithUnsafeBufferPointer<UInt8>operationsreduce+append. It required too much memory allocation. Instead - calculate needed capacity upfront and createStringusingString(unsafeUninitializedCapacity:)initPerformance
Performance Comparison
Measured with
M2 Max 32 GB RAM, MacOS 26.2, release build. If I run these benchmarks many times, average deviation is about 3-4%Previous
validStringimplementation performance:Introduced
validStringimplementation performance: