Skip to content

Commit

Permalink
Fixed some performance issues (#182)
Browse files Browse the repository at this point in the history
<!--- 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]>
  • Loading branch information
Wouter01 authored Apr 29, 2023
1 parent 2921e93 commit 045bd35
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
62 changes: 41 additions & 21 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
public init(
_ text: Binding<String>,
language: CodeLanguage,
theme: Binding<EditorTheme>,
font: Binding<NSFont>,
tabWidth: Binding<Int>,
indentOption: Binding<IndentOption> = .constant(.spaces(count: 4)),
lineHeight: Binding<Double>,
wrapLines: Binding<Bool>,
editorOverscroll: Binding<Double> = .constant(0.0),
theme: EditorTheme,
font: NSFont,
tabWidth: Int,
indentOption: IndentOption = .spaces(count: 4),
lineHeight: Double,
wrapLines: Bool,
editorOverscroll: Double = 0.0,
cursorPosition: Binding<(Int, Int)>,
useThemeBackground: Bool = true,
highlightProvider: HighlightProviding? = nil,
Expand All @@ -52,14 +52,14 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
) {
self._text = text
self.language = language
self._theme = theme
self.theme = theme
self.useThemeBackground = useThemeBackground
self._font = font
self._tabWidth = tabWidth
self._indentOption = indentOption
self._lineHeight = lineHeight
self._wrapLines = wrapLines
self._editorOverscroll = editorOverscroll
self.font = font
self.tabWidth = tabWidth
self.indentOption = indentOption
self.lineHeight = lineHeight
self.wrapLines = wrapLines
self.editorOverscroll = editorOverscroll
self._cursorPosition = cursorPosition
self.highlightProvider = highlightProvider
self.contentInsets = contentInsets
Expand All @@ -69,13 +69,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable {

@Binding private var text: String
private var language: CodeLanguage
@Binding private var theme: EditorTheme
@Binding private var font: NSFont
@Binding private var tabWidth: Int
@Binding private var indentOption: IndentOption
@Binding private var lineHeight: Double
@Binding private var wrapLines: Bool
@Binding private var editorOverscroll: Double
private var theme: EditorTheme
private var font: NSFont
private var tabWidth: Int
private var indentOption: IndentOption
private var lineHeight: Double
private var wrapLines: Bool
private var editorOverscroll: Double
@Binding private var cursorPosition: (Int, Int)
private var useThemeBackground: Bool
private var highlightProvider: HighlightProviding?
Expand Down Expand Up @@ -107,6 +107,12 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
}

public func updateNSViewController(_ controller: NSViewControllerType, context: Context) {
// Do manual diffing to reduce the amount of reloads.
// This helps a lot in view performance, as it otherwise gets triggered on each environment change.
guard !paramsAreEqual(controller: controller) else {
return
}

controller.font = font
controller.wrapLines = wrapLines
controller.useThemeBackground = useThemeBackground
Expand Down Expand Up @@ -134,4 +140,18 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
controller.reloadUI()
return
}

func paramsAreEqual(controller: NSViewControllerType) -> Bool {
controller.font == font &&
controller.wrapLines == wrapLines &&
controller.useThemeBackground == useThemeBackground &&
controller.lineHeightMultiple == lineHeight &&
controller.editorOverscroll == editorOverscroll &&
controller.contentInsets == contentInsets &&
controller.language.id == language.id &&
controller.theme == theme &&
controller.indentOption == indentOption &&
controller.tabWidth == tabWidth &&
controller.letterSpacing == letterSpacing
}
}
17 changes: 17 additions & 0 deletions Sources/CodeEditTextView/Extensions/NSEdgeInsets+Equatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSEdgeInsets+Equatable.swift
//
//
// Created by Wouter Hennen on 29/04/2023.
//

import Foundation

extension NSEdgeInsets: Equatable {
public static func == (lhs: NSEdgeInsets, rhs: NSEdgeInsets) -> Bool {
lhs.bottom == rhs.bottom &&
lhs.top == rhs.top &&
lhs.left == rhs.left &&
lhs.right == rhs.right
}
}

0 comments on commit 045bd35

Please sign in to comment.