Skip to content

Conversation

@ra1028
Copy link
Owner

@ra1028 ra1028 commented Aug 12, 2022

Pull Request Type

  • Bug fix
  • New feature
  • Refactoring
  • Documentation update
  • Chore

Issue for this PR

Link: close #20

Description

Implement Directed Acyclic Graph based dependency graph for better reliability, predictable result, and reproducibility of the internal state.

Motivation and Context

Currently, the core system of this library is implemented with ARC and reactive programing, but it's not 100% reliable and reproducible, it prevents restoration to a specific state.
Using DAG data structure and allows this library to manage a centralized state in a holistic approach.
That's the same approach with Recoil which that this library is inspired.

Impact on Existing Code

It contains a lot of braking changes on ABI, but the existing codes written with a proper usage of Atoms should work without changes.

Performance

  • Measure (average of 10 times run)
    • Main branch: 0.589 sec
    • DAG branch: 0.425 sec

= 139% better

func testPerformance() {
    struct Test1Atom: StateAtom, Hashable {
        func defaultValue(context: Context) -> Int {
            0
        }
    }

    struct Test2Atom: ValueAtom, Hashable {
        func value(context: Context) -> Int {
            context.watch(Test1Atom())
        }
    }

    struct Test3Atom: ValueAtom, Hashable {
        func value(context: Context) -> Int {
            context.watch(Test2Atom())
        }
    }

    struct Test4Atom: ValueAtom, Hashable {
        func value(context: Context) -> Int {
            context.watch(Test3Atom())
        }
    }

    struct Test5Atom: ValueAtom, Hashable {
        func value(context: Context) -> Int {
            context.watch(Test4Atom())
        }
    }

    measure {
        for _ in 0..<10000 {
            let context = AtomTestContext()

            let value0 = context.watch(Test5Atom())
            XCTAssertEqual(value0, 0)

            context[Test1Atom()] = 1

            let value1 = context.watch(Test5Atom())
            XCTAssertEqual(value1, 1)
        }
    }
}

@ra1028 ra1028 force-pushed the refactor/dag-based-state-management branch 2 times, most recently from 4e408e9 to f340e5f Compare August 15, 2022 16:35
@ra1028 ra1028 force-pushed the refactor/dag-based-state-management branch from f340e5f to 50e78cb Compare August 16, 2022 17:18
@ra1028 ra1028 force-pushed the refactor/dag-based-state-management branch from 54fb1e9 to f75b602 Compare August 21, 2022 18:30
@ra1028 ra1028 marked this pull request as ready for review August 22, 2022 13:46
@ra1028 ra1028 merged commit c9379e2 into main Aug 22, 2022
@ra1028 ra1028 deleted the refactor/dag-based-state-management branch August 22, 2022 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Refactoring]: DAG based state management

2 participants