Skip to content

Improve performance for comparing AttributedStrings with differing character counts #1224

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
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ let benchmarks = {
#endif

let manyAttributesString2 = createManyAttributesString()
let manyAttributesString3 = {
var str = createManyAttributesString()
str.characters.append("a")
return str
}()
let manyAttributesStringRange = manyAttributesString.characters.index(manyAttributesString.startIndex, offsetBy: manyAttributesString.characters.count / 2)...
let manyAttributesSubstring = manyAttributesString[manyAttributesStringRange]
let manyAttributes2Substring = manyAttributesString2[manyAttributesStringRange]
Expand All @@ -422,6 +427,10 @@ let benchmarks = {
blackHole(manyAttributesString == manyAttributesString2)
}

Benchmark("equalityDifferingCharacters") { benchmark in
blackHole(manyAttributesString == manyAttributesString3)
}

Benchmark("substringEquality") { benchmark in
blackHole(manyAttributesSubstring == manyAttributes2Substring)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ extension AttributedString.Guts {

guard left.count == right.count else { return false }
guard !left.isEmpty else { return true }

if !left._isPartial && !right._isPartial {
// For a full BigString, we can get the grapheme cluster count in constant time since
// the grapheme cluster count is cached at the node level in the tree. It is not
// possible for two AttributedStrings with differing character counts to be equal,
// so bail early if we detect that
//
// Note: we should not perform this check for cases where we are not knowingly working
// with the full string. Since character counts are only cached at the node level,
// to get the character count of a substring you would need to run the grapheme
// breaking algorithm over the partial first and last chunks. While this is
// technically done in constant time as chunks have a max of 255 UTF-8 scalars, grapheme
// breaking up to 510 UTF-8 scalars would not be cheap. In the future we can
// investigate best effort short cuts by comparing the counts of just the "middle"
// chunks that we can determine cheaply along with the knowledge that the partial
// first and last chunks have a character count no more than their UTF-8 counts.
guard left._guts.string.count == right._guts.string.count else {
return false
}
}


guard var leftIndex = left._strBounds.ranges.first?.lowerBound, var rightIndex = right._strBounds.ranges.first?.lowerBound else { return false }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ extension AttributedString {
internal let _strBounds: RangeSet<BigString.Index>
internal let _isDiscontiguous: Bool

internal var _isPartial: Bool {
guard !_isDiscontiguous else {
return true
}
guard let lower = _bounds.lowerBound._stringIndex, let upper = _bounds.upperBound._stringIndex else {
preconditionFailure("AttributedString.Runs created with bounds that have un-set string indices")
}
return _guts.string.startIndex != lower || _guts.string.endIndex != upper
}

internal init(_ guts: Guts, in bounds: Range<BigString.Index>) {
self.init(guts, in: RangeSet(bounds))
}
Expand Down