Skip to content

Improve AttributedString.CharacterView to Substring conversion in AttributedStringProtocol.range(of:) #1440

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

Merged

Conversation

jmschonfeld
Copy link
Contributor

@jmschonfeld jmschonfeld commented Jul 16, 2025

We don't currently have range(of:) implemented natively on BigString (or a UTF-8 collection) and instead we must first convert to a Substring to perform the searching operation. In the future, we should benchmark and improve this function by writing it natively on the BigString storage instead of bridging to String. However, today we're taking a very slow, linear path of converting the character view to a Substring iterating character by character. Instead, we can take a faster path by using our CharacterView to String conversion that copies chunk-by-chunk and create a Substring from that result. The benchmarks show that this has a significant performance improvement over the existing implementation (with much more room to improve with a native implementation)

AttributedStringBenchmarks

range(of:) metrics

Time (wall clock): results within specified thresholds, fold down for details.

Time (wall clock) (ns) * p0 p25 p50 p75 p90 p99 p100 Samples
main 3745 3963 4198 4321 4366 4444 4613 240
branch 375 403 439 449 458 486 554 2296
Δ -3370 -3560 -3759 -3872 -3908 -3958 -4059 2056
Improvement % 90 90 90 90 90 89 88 2056

Time (total CPU): results within specified thresholds, fold down for details.

Time (total CPU) (ns) * p0 p25 p50 p75 p90 p99 p100 Samples
main 3747 3965 4182 4325 4362 4432 4615 240
branch 376 404 440 450 458 481 534 2296
Δ -3371 -3561 -3742 -3875 -3904 -3951 -4081 2056
Improvement % 90 90 89 90 90 89 88 2056

Throughput (# / s): results within specified thresholds, fold down for details.

Throughput (# / s) (K) p0 p25 p50 p75 p90 p99 p100 Samples
main 267 252 238 231 229 225 217 240
branch 2665 2481 2277 2229 2185 2059 1806 2296
Δ 2398 2229 2039 1998 1956 1834 1589 2056
Improvement % 898 885 857 865 854 815 732 2056

Resolves rdar://155952423

@jmschonfeld jmschonfeld requested a review from lorentey July 16, 2025 21:33
@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit d1b39d1 into swiftlang:main Jul 17, 2025
16 checks passed
@jmschonfeld jmschonfeld deleted the attrstr/range-of-substring-init branch July 17, 2025 23:08
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.

2 participants