From 7748668f4bbcea42ce79eaaebc93b0e41e3e12ad Mon Sep 17 00:00:00 2001 From: david-swift Date: Sun, 21 Apr 2024 07:27:52 +0200 Subject: [PATCH] Slightly improve performance of State --- Sources/Adwaita/Model/Data Flow/Binding.swift | 2 +- Sources/Adwaita/Model/Data Flow/State.swift | 33 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Sources/Adwaita/Model/Data Flow/Binding.swift b/Sources/Adwaita/Model/Data Flow/Binding.swift index b19b32a..e05d1ac 100644 --- a/Sources/Adwaita/Model/Data Flow/Binding.swift +++ b/Sources/Adwaita/Model/Data Flow/Binding.swift @@ -122,7 +122,7 @@ extension Binding where Value: MutableCollection { public subscript(safe index: Value.Index?, default defaultValue: Value.Element) -> Binding { .init { if let index, wrappedValue.indices.contains(index) { - return wrappedValue[index] ?? defaultValue + return wrappedValue[index] } return defaultValue } set: { newValue in diff --git a/Sources/Adwaita/Model/Data Flow/State.swift b/Sources/Adwaita/Model/Data Flow/State.swift index 83d5ddb..c47f92b 100644 --- a/Sources/Adwaita/Model/Data Flow/State.swift +++ b/Sources/Adwaita/Model/Data Flow/State.swift @@ -72,7 +72,7 @@ public struct State: StateProtocol { /// - wrappedValue: The wrapped value. /// - forceUpdates: Whether to force update all available views when the property gets modified. public init(wrappedValue: Value, forceUpdates: Bool = false) { - content = .init(storage: .init(value: wrappedValue)) + content = .init(initialValue: wrappedValue) self.forceUpdates = forceUpdates } @@ -80,14 +80,41 @@ public struct State: StateProtocol { public class Content { /// The storage. - public var storage: Storage + public var storage: Storage { + get { + if let internalStorage { + return internalStorage + } + let storage = Storage(value: initialValue) + internalStorage = storage + return storage + } + set { + internalStorage = newValue + } + } + /// The internal storage. + var internalStorage: Storage? + /// The initial value. + private var initialValue: Value /// Initialize the content. /// - Parameter storage: The storage. + @available(*, deprecated, message: "Use a safer initializer instead") public init(storage: Storage) { + // swiftlint:disable force_cast + let initialValue = storage.value as! Value + // swiftlint:enable force_cast + self.initialValue = initialValue self.storage = storage } + /// Initialize the content. + /// - Parameter initialValue: The initial value. + public init(initialValue: Value) { + self.initialValue = initialValue + } + } /// A class storing the value. @@ -156,7 +183,7 @@ extension State where Value: Codable { /// /// The folder path will be appended to the XDG data home directory. public init(wrappedValue: Value, _ key: String, folder: String? = nil, forceUpdates: Bool = false) { - content = .init(storage: .init(value: wrappedValue)) + content = .init(initialValue: wrappedValue) self.forceUpdates = forceUpdates content.storage.key = key content.storage.folder = folder