Skip to content

Commit 045bd35

Browse files
authored
Fixed some performance issues (#182)
<!--- IMPORTANT: If this PR addresses multiple unrelated issues, it will be closed until separated. --> ### Description This PR fixes an issue where the view would be reloaded too often. This happened because `updateNSViewController` is called too often. This mostly happens because of a change in the environment, which gets passed to the NSViewControllerRepresentable in the `context` variable. Manual diffing is applied to check if any variables that matter have changed. If not, nothing is updated. The initializer has also changed, a few variables are now just values instead of bindings. These don't have to be bindings, as the textview won't update variables. ### Related Issues CodeEditApp/CodeEdit#1247 ### Checklist <!--- Add things that are not yet implemented above --> - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots <!--- REQUIRED: if issue is UI related --> <!--- IMPORTANT: Fill out all required fields. Otherwise we might close this PR temporarily --> Signed-off-by: Wouter01 <[email protected]>
1 parent 2921e93 commit 045bd35

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

Sources/CodeEditTextView/CodeEditTextView.swift

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
3636
public init(
3737
_ text: Binding<String>,
3838
language: CodeLanguage,
39-
theme: Binding<EditorTheme>,
40-
font: Binding<NSFont>,
41-
tabWidth: Binding<Int>,
42-
indentOption: Binding<IndentOption> = .constant(.spaces(count: 4)),
43-
lineHeight: Binding<Double>,
44-
wrapLines: Binding<Bool>,
45-
editorOverscroll: Binding<Double> = .constant(0.0),
39+
theme: EditorTheme,
40+
font: NSFont,
41+
tabWidth: Int,
42+
indentOption: IndentOption = .spaces(count: 4),
43+
lineHeight: Double,
44+
wrapLines: Bool,
45+
editorOverscroll: Double = 0.0,
4646
cursorPosition: Binding<(Int, Int)>,
4747
useThemeBackground: Bool = true,
4848
highlightProvider: HighlightProviding? = nil,
@@ -52,14 +52,14 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
5252
) {
5353
self._text = text
5454
self.language = language
55-
self._theme = theme
55+
self.theme = theme
5656
self.useThemeBackground = useThemeBackground
57-
self._font = font
58-
self._tabWidth = tabWidth
59-
self._indentOption = indentOption
60-
self._lineHeight = lineHeight
61-
self._wrapLines = wrapLines
62-
self._editorOverscroll = editorOverscroll
57+
self.font = font
58+
self.tabWidth = tabWidth
59+
self.indentOption = indentOption
60+
self.lineHeight = lineHeight
61+
self.wrapLines = wrapLines
62+
self.editorOverscroll = editorOverscroll
6363
self._cursorPosition = cursorPosition
6464
self.highlightProvider = highlightProvider
6565
self.contentInsets = contentInsets
@@ -69,13 +69,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
6969

7070
@Binding private var text: String
7171
private var language: CodeLanguage
72-
@Binding private var theme: EditorTheme
73-
@Binding private var font: NSFont
74-
@Binding private var tabWidth: Int
75-
@Binding private var indentOption: IndentOption
76-
@Binding private var lineHeight: Double
77-
@Binding private var wrapLines: Bool
78-
@Binding private var editorOverscroll: Double
72+
private var theme: EditorTheme
73+
private var font: NSFont
74+
private var tabWidth: Int
75+
private var indentOption: IndentOption
76+
private var lineHeight: Double
77+
private var wrapLines: Bool
78+
private var editorOverscroll: Double
7979
@Binding private var cursorPosition: (Int, Int)
8080
private var useThemeBackground: Bool
8181
private var highlightProvider: HighlightProviding?
@@ -107,6 +107,12 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
107107
}
108108

109109
public func updateNSViewController(_ controller: NSViewControllerType, context: Context) {
110+
// Do manual diffing to reduce the amount of reloads.
111+
// This helps a lot in view performance, as it otherwise gets triggered on each environment change.
112+
guard !paramsAreEqual(controller: controller) else {
113+
return
114+
}
115+
110116
controller.font = font
111117
controller.wrapLines = wrapLines
112118
controller.useThemeBackground = useThemeBackground
@@ -134,4 +140,18 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
134140
controller.reloadUI()
135141
return
136142
}
143+
144+
func paramsAreEqual(controller: NSViewControllerType) -> Bool {
145+
controller.font == font &&
146+
controller.wrapLines == wrapLines &&
147+
controller.useThemeBackground == useThemeBackground &&
148+
controller.lineHeightMultiple == lineHeight &&
149+
controller.editorOverscroll == editorOverscroll &&
150+
controller.contentInsets == contentInsets &&
151+
controller.language.id == language.id &&
152+
controller.theme == theme &&
153+
controller.indentOption == indentOption &&
154+
controller.tabWidth == tabWidth &&
155+
controller.letterSpacing == letterSpacing
156+
}
137157
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSEdgeInsets+Equatable.swift
3+
//
4+
//
5+
// Created by Wouter Hennen on 29/04/2023.
6+
//
7+
8+
import Foundation
9+
10+
extension NSEdgeInsets: Equatable {
11+
public static func == (lhs: NSEdgeInsets, rhs: NSEdgeInsets) -> Bool {
12+
lhs.bottom == rhs.bottom &&
13+
lhs.top == rhs.top &&
14+
lhs.left == rhs.left &&
15+
lhs.right == rhs.right
16+
}
17+
}

0 commit comments

Comments
 (0)