Skip to content

attributed string fast path equality performance improvements #1415

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vanvoorden
Copy link
Contributor

@vanvoorden vanvoorden commented Jul 9, 2025

Here is a small performance improvement to value-equality checking in AttributedString.

When two AttributedString instances are identical we can return true in constant-time: we do not need to check through all N characters.

Currently the check for identity equality lives inside Guts.123 This is still constant-time… but we have the potential to move this check up sooner: directly in the == operator on AttributedString. This will look similar to what we already have in place for AttributedSubstring.4

Benchmarks show improvements close to two orders of magnitude increased throughput when our AttributedString instances are identical.

SWIFTCI_USE_LOCAL_DEPS=1 swift package --package-path Benchmarks --disable-sandbox -c release benchmark run --target "AttributedStringBenchmarks" --filter "equality" --format markdown
SWIFTCI_USE_LOCAL_DEPS=1 swift package --package-path Benchmarks --disable-sandbox -c release benchmark run --target "AttributedStringBenchmarks" --filter "equalityShared" --format markdown

Control: Equality Unique

Metric p0 p25 p50 p75 p90 p99 p100 Samples
Throughput (# / s) (K) 81 79 78 78 77 77 77 79
Time (total CPU) (μs) * 12 13 13 13 13 13 13 79
Time (wall clock) (μs) * 12 13 13 13 13 13 13 79

Test: Equality Unique

Metric p0 p25 p50 p75 p90 p99 p100 Samples
Throughput (# / s) (K) 84 80 79 79 78 77 77 80
Time (total CPU) (μs) * 12 13 13 13 13 13 13 80
Time (wall clock) (μs) * 12 13 13 13 13 13 13 80

Control: Equality Shared

Metric p0 p25 p50 p75 p90 p99 p100 Samples
Throughput (# / s) (M) 421 381 381 375 369 296 12 10000
Time (total CPU) (ns) * 3 3 3 3 3 4 83 10000
Time (wall clock) (ns) * 2 3 3 3 3 3 81 10000

Test: Equality Shared

Metric p0 p25 p50 p75 p90 p99 p100 Samples
Throughput (# / s) (G) 24 24 24 24 24 12 0 9634
Time (total CPU) (ns) * 1 1 1 1 1 1 14 10000
Time (wall clock) (ns) * 0 0 0 0 0 0 5 9634

Footnotes

  1. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString%2BGuts.swift#L77

  2. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString%2BGuts.swift#L84-L86

  3. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedString%2BGuts.swift#L105

  4. https://github.com/swiftlang/swift-foundation/blob/swift-6.1.2-RELEASE/Sources/FoundationEssentials/AttributedString/AttributedSubstring.swift#L79-L81

@jmschonfeld
Copy link
Contributor

@swift-ci please test

Copy link
Contributor

@jmschonfeld jmschonfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this and for including the benchmark results! Indeed this change seems sensible especially since AttributedSubstring already does this. I'm curious to know exactly what work we're eliding here - I suspect it's the creation and comparison of a RangeSet representing the full range of the string when we create the AttributedString.Runs instances to compare. Out of curiosity, did you profile the call before/after to see what calls were previously taking up the most time in the identical case?

@vanvoorden
Copy link
Contributor Author

Out of curiosity, did you profile the call before/after to see what calls were previously taking up the most time in the identical case?

I did not do much profiling outside of running benchmarks. But skipping all the work in the AttributedString.Runs constructor looks like where we are seeing the wins from.

@vanvoorden
Copy link
Contributor Author

@jmschonfeld I think we fixed the Windows issue in #1416. Do I have to merge main here in this diff or will running the job again pick up the new changes?

@jmschonfeld
Copy link
Contributor

@jmschonfeld I think we fixed the Windows issue in #1416. Do I have to merge main here in this diff or will running the job again pick up the new changes?

I believe CI checks out the merge commit rather than your branch, so I think re-running should be sufficient - let me try re-running it and if it fails again we can rebase/merge main in

@jmschonfeld
Copy link
Contributor

@swift-ci please test Windows platform

@vanvoorden
Copy link
Contributor Author

@jmschonfeld I think this looks good to merge. Thanks!

@jmschonfeld
Copy link
Contributor

Thanks! Yep this looks good to me - I'll get this merged soon once we finish sorting out a few other action items for the 6.2 release

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