Skip to content

Commit 201a45b

Browse files
authored
Lazily compute whether attribute changed in _attributeModified(_:old:new:) (#1237)
1 parent 03fe46f commit 201a45b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sources/FoundationEssentials/AttributedString/AttributedStringAttributeStorage.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,15 @@ extension AttributedString._AttributeStorage {
186186
invalidatableKeys.remove(key)
187187
}
188188

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

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

0 commit comments

Comments
 (0)