Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ReferenceEdge #427

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import Foundation
@_implementationOnly import Atomics

private let _edge_global_counter = ManagedAtomic<UInt64>.init(0)
private let _edge_global_counter = ManagedAtomic<UInt64>.init(0)

/**
A structure that manages sub-state-tree from root-state-tree.
Expand All @@ -35,27 +35,22 @@ private let _edge_global_counter = ManagedAtomic<UInt64>.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<Input.Value>) -> Fragment<Output>) -> MemoizeMap<Input, Output>`
*/
@propertyWrapper
public struct _COWFragment<State>: EdgeType {
public struct ReferenceEdge<State>: 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
Expand All @@ -68,9 +63,9 @@ public struct _COWFragment<State>: 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
Expand All @@ -97,9 +92,9 @@ public struct _COWFragment<State>: 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
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/VergeORMTests/PerformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ final class DictionaryPerformanceTests: XCTestCase {
@available(iOS 13, *)
func testModify_withCOWBox() {

let base = (0..<100000).reduce(into: [Int: _COWFragment<Any>]()) { partialResult, i in
let base = (0..<100000).reduce(into: [Int: ReferenceEdge<Any>]()) { partialResult, i in
partialResult[i] = .init(wrappedValue: Concrete(id: i))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Verge
final class FragmentTests: XCTestCase {

struct State {
@_COWFragment var number = 0
@ReferenceEdge var number = 0
}

func testFragment() {
Expand Down
Loading