Skip to content

Lazily compute whether attribute changed in _attributeModified(_:old:new:) #1237

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
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 @@ -186,12 +186,15 @@ extension AttributedString._AttributeStorage {
invalidatableKeys.remove(key)
}

guard old != new else { return }
// Lazy to ensure we only check if the value changed when we actually need to because we found a dependent attribute
// Unboxing the attribute value to call its == implementation can be expensive, so for text that doesn't contain dependent attributes avoid it when possible
lazy var valueChanged = { old != new }()

for k in invalidatableKeys {
guard k != key else { continue }
guard let value = contents[k] else { continue }
guard value.isInvalidatedOnChange(of: key) else { continue }
guard valueChanged else { return }
// FIXME: ☠️ This subscript assignment is recursively calling this same method.
// FIXME: Collect invalidated keys into a temporary set instead, and progressively
// FIXME: extend that set until all its keys are gone.
Expand Down