From 0e5a47fd70a4e57c13814f2e7650c54acc1b144f Mon Sep 17 00:00:00 2001 From: Muukii Date: Tue, 10 Oct 2023 12:49:26 +0900 Subject: [PATCH] Patch --- ..._COWFragment.swift => ReferenceEdge.swift} | 21 +++++++------------ Tests/VergeORMTests/PerformanceTests.swift | 2 +- ...ntTests.swift => ReferenceEdgeTests.swift} | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) rename Sources/Verge/Utility/{_COWFragment.swift => ReferenceEdge.swift} (80%) rename Tests/VergeTests/{_COWFragmentTests.swift => ReferenceEdgeTests.swift} (92%) diff --git a/Sources/Verge/Utility/_COWFragment.swift b/Sources/Verge/Utility/ReferenceEdge.swift similarity index 80% rename from Sources/Verge/Utility/_COWFragment.swift rename to Sources/Verge/Utility/ReferenceEdge.swift index ee515b39ca..a9181bf8a7 100644 --- a/Sources/Verge/Utility/_COWFragment.swift +++ b/Sources/Verge/Utility/ReferenceEdge.swift @@ -22,7 +22,7 @@ import Foundation @_implementationOnly import Atomics -private let _edge_global_counter = ManagedAtomic.init(0) +private let _edge_global_counter = ManagedAtomic.init(0) /** A structure that manages sub-state-tree from root-state-tree. @@ -35,27 +35,22 @@ private let _edge_global_counter = ManagedAtomic.init(0) Fragment is a wrapper structure and manages version number inside. It increments the version number each wrapped value updated. - - Memoization can use that version if it should pass new input. - - To activate this feature, you can check this method. - `MemoizeMap.map(_ map: @escaping (Changes) -> Fragment) -> MemoizeMap` */ @propertyWrapper -public struct _COWFragment: EdgeType { +public struct ReferenceEdge: EdgeType { private final class Storage { var value: State - init(_ value: State) { + init(_ value: consuming State) { self.value = value } } public static func == (lhs: Self, rhs: Self) -> Bool { - lhs.storage === rhs.storage || lhs.version == rhs.version + lhs.storage === rhs.storage || (lhs.globalID == rhs.globalID && lhs.version == rhs.version) } public let globalID: UInt64 @@ -68,9 +63,9 @@ public struct _COWFragment: EdgeType { private(set) public var counter: NonAtomicCounter = .init() - public init(wrappedValue: State) { + public init(wrappedValue: consuming State) { self.globalID = _edge_global_counter.loadThenWrappingIncrement(ordering: .relaxed) - self.storage = Storage(wrappedValue) + self.storage = Storage(consume wrappedValue) } private var storage: Storage @@ -97,9 +92,9 @@ public struct _COWFragment: EdgeType { } -extension _COWFragment where State : Equatable { +extension ReferenceEdge where State : Equatable { public static func == (lhs: Self, rhs: Self) -> Bool { - lhs.storage === rhs.storage || lhs.version == rhs.version || lhs.wrappedValue == rhs.wrappedValue + lhs.storage === rhs.storage || (lhs.globalID == rhs.globalID && lhs.version == rhs.version) || lhs.wrappedValue == rhs.wrappedValue } } diff --git a/Tests/VergeORMTests/PerformanceTests.swift b/Tests/VergeORMTests/PerformanceTests.swift index 27e2e674ea..883096fe80 100644 --- a/Tests/VergeORMTests/PerformanceTests.swift +++ b/Tests/VergeORMTests/PerformanceTests.swift @@ -409,7 +409,7 @@ final class DictionaryPerformanceTests: XCTestCase { @available(iOS 13, *) func testModify_withCOWBox() { - let base = (0..<100000).reduce(into: [Int: _COWFragment]()) { partialResult, i in + let base = (0..<100000).reduce(into: [Int: ReferenceEdge]()) { partialResult, i in partialResult[i] = .init(wrappedValue: Concrete(id: i)) } diff --git a/Tests/VergeTests/_COWFragmentTests.swift b/Tests/VergeTests/ReferenceEdgeTests.swift similarity index 92% rename from Tests/VergeTests/_COWFragmentTests.swift rename to Tests/VergeTests/ReferenceEdgeTests.swift index b3be165c75..ae30625e3c 100644 --- a/Tests/VergeTests/_COWFragmentTests.swift +++ b/Tests/VergeTests/ReferenceEdgeTests.swift @@ -5,7 +5,7 @@ import Verge final class FragmentTests: XCTestCase { struct State { - @_COWFragment var number = 0 + @ReferenceEdge var number = 0 } func testFragment() {