Fix: suppress didSet observer during incremental token merge

In mergeTokensByRowId, each tokensByRowId[rowId] = tokens assignment
triggered the didSet observer (Swift Dictionary is a value type), causing
tokenAttributedStringsByRowId.removeAll(), clampHorizontalOffsets(), and
setNeedsDisplayForVisibleBounds() to fire N times — once per patched row.

This defeated the incremental cache invalidation the method intended and
multiplied layout work for large token patches.

Add a _suppressTokensByRowIdDidSet flag so the didSet observer is skipped
during the merge loop. The method already performs its own targeted cache
invalidation (removeValue per row) and calls clampHorizontalOffsets() and
setNeedsDisplayForVisibleBounds() once at the end.
This commit is contained in:
Cursor Agent committed 2026-05-01 05:37:54 +00:00
1 parent 9b9fe0d64b
commit e72d364ef8
1 file changed
+4
@@ -689,8 +689,10 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
setNeedsDisplayForVisibleBounds()
}
}
private var _suppressTokensByRowIdDidSet = false
var tokensByRowId: [String: [ReviewDiffNativeToken]] = [:] {
didSet {
guard !_suppressTokensByRowIdDidSet else { return }
tokenAttributedStringsByRowId.removeAll()
clampHorizontalOffsets()
setNeedsDisplayForVisibleBounds()
@@ -698,10 +700,12 @@ private final class ReviewDiffContentView: UIView, UIGestureRecognizerDelegate {
}
func mergeTokensByRowId(_ tokensPatch: [String: [ReviewDiffNativeToken]]) {
_suppressTokensByRowIdDidSet = true
tokensPatch.forEach { rowId, tokens in
tokensByRowId[rowId] = tokens
tokenAttributedStringsByRowId.removeValue(forKey: rowId)
}
_suppressTokensByRowIdDidSet = false
clampHorizontalOffsets()
setNeedsDisplayForVisibleBounds()
}