Skip to content

Commit ee7024d

Browse files
committed
Very important Performance Improvements
1 parent 94065e7 commit ee7024d

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Sources/CodeEditTextView/TextLayoutManager/TextLayoutManager+Layout.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,19 @@ extension TextLayoutManager {
9292
#if DEBUG
9393
isInLayout = false
9494
#endif
95-
CATransaction.commit()
96-
9795
// Enqueue any lines not used in this layout pass.
9896
viewReuseQueue.enqueueViews(notInSet: usedFragmentIDs)
9997

10098
// Update the visible lines with the new set.
10199
visibleLineIds = newVisibleLines
102100

101+
// The delegate methods below may call another layout pass, make sure we don't send it into a loop of forced
102+
// layout.
103+
needsLayout = false
104+
105+
// Commit the view tree changes we just made.
106+
CATransaction.commit()
107+
103108
// These are fine to update outside of `isInLayout` as our internal data structures are finalized at this point
104109
// so laying out again won't break our line storage or visible line.
105110

@@ -114,8 +119,6 @@ extension TextLayoutManager {
114119
if originalHeight != lineStorage.height || layoutView?.frame.size.height != lineStorage.height {
115120
delegate?.layoutManagerHeightDidUpdate(newHeight: lineStorage.height)
116121
}
117-
118-
needsLayout = false
119122
}
120123

121124
// MARK: - Layout Single Line

Sources/CodeEditTextView/TextLine/TextLine.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ public final class TextLine: Identifiable, Equatable {
3131
/// - Returns: True, if this line has been marked as needing layout using ``TextLine/setNeedsLayout()`` or if the
3232
/// line needs to find new line breaks due to a new constraining width.
3333
func needsLayout(maxWidth: CGFloat) -> Bool {
34-
needsLayout || maxWidth != self.maxWidth
34+
needsLayout // Force layout
35+
|| (
36+
// Both max widths we're comparing are finite
37+
maxWidth.isFinite
38+
&& (self.maxWidth ?? 0.0).isFinite
39+
// The new max width is less than the required space, so we need to layout again.
40+
&& maxWidth < (self.maxWidth ?? 0.0)
41+
)
3542
}
3643

3744
/// Prepares the line for display, generating all potential line breaks and calculating the real height of the line.

0 commit comments

Comments
 (0)