From 13e87c7087211b7956fc906581f005611e80e24f Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Thu, 15 Feb 2024 10:14:41 -0800 Subject: [PATCH] removing graph reference from node metadata --- bind.go | 6 +++--- expert_node.go | 2 +- freeze.go | 2 +- graph.go | 2 -- graph_test.go | 1 - node.go | 2 -- node_test.go | 9 ++++----- observe.go | 6 +----- scope.go | 7 +++++++ stabilize_test.go | 16 +++++++++------- var.go | 13 +++++++------ 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/bind.go b/bind.go index 86d4057..baef20a 100644 --- a/bind.go +++ b/bind.go @@ -156,7 +156,7 @@ func (b *bindMainIncr[A, B]) Stabilize(ctx context.Context) error { func (b *bindMainIncr[A, B]) Invalidate() { for _, n := range b.bind.rhsNodes { - b.n.graph.invalidateNode(n) + graphFromScope(b).invalidateNode(n) } } @@ -199,7 +199,7 @@ func (b *bindLeftChangeIncr[A, B]) Stabilize(ctx context.Context) (err error) { b.bind.main.parents = []INode{b} } - if err = b.n.graph.changeParent(b.bind.main, oldRhs, b.bind.rhs); err != nil { + if err = graphFromScope(b).changeParent(b.bind.main, oldRhs, b.bind.rhs); err != nil { return err } if oldRhs != nil { @@ -216,7 +216,7 @@ func (b *bindLeftChangeIncr[A, B]) Stabilize(ctx context.Context) (err error) { b.bind.addScopeNode(n) } } - b.n.graph.propagateInvalidity() + graphFromScope(b).propagateInvalidity() return nil } diff --git a/expert_node.go b/expert_node.go index 7388d5e..200c64b 100644 --- a/expert_node.go +++ b/expert_node.go @@ -56,7 +56,7 @@ type expertNode struct { node *Node } -func (en *expertNode) Graph() *Graph { return en.node.graph } +func (en *expertNode) Graph() *Graph { return graphFromScope(en.incr) } func (en *expertNode) SetID(id Identifier) { en.node.id = id diff --git a/freeze.go b/freeze.go index efe23ec..a061f72 100644 --- a/freeze.go +++ b/freeze.go @@ -42,7 +42,7 @@ func (f *freezeIncr[A]) Stabilize(_ context.Context) error { if f.freezeAt > 0 { return nil } - f.freezeAt = f.n.graph.stabilizationNum + f.freezeAt = graphFromScope(f).stabilizationNum f.v = f.i.Value() return nil } diff --git a/graph.go b/graph.go index c60636f..27deba1 100644 --- a/graph.go +++ b/graph.go @@ -400,7 +400,6 @@ func (graph *Graph) addNode(n INode) { if graphAlreadyHasNode { return } - gnn.graph = graph graph.numNodes++ gnn.initializeFrom(n) graph.nodes[gnn.id] = n @@ -415,7 +414,6 @@ func (graph *Graph) addObserver(on IObserver) { if graphAlreadyHasObserver { return } - onn.graph = graph graph.numNodes++ onn.initializeFrom(on) graph.observers[onn.id] = on diff --git a/graph_test.go b/graph_test.go index 978c9cb..87aded3 100644 --- a/graph_test.go +++ b/graph_test.go @@ -107,7 +107,6 @@ func Test_Graph_removeNodeFromGraph(t *testing.T) { testutil.Equal(t, 0, mn00.n.setAt) testutil.Equal(t, 0, mn00.n.recomputedAt) testutil.Nil(t, mn00.n.createdIn) - testutil.Nil(t, mn00.n.graph) testutil.Equal(t, heightUnset, mn00.n.height) testutil.Equal(t, heightUnset, mn00.n.heightInRecomputeHeap) testutil.Equal(t, heightUnset, mn00.n.heightInAdjustHeightsHeap) diff --git a/node.go b/node.go index e7b8b9e..750b97d 100644 --- a/node.go +++ b/node.go @@ -25,8 +25,6 @@ type Node struct { kind string // metadata is any additional metadata a user wants to attach to a node. metadata any - // graph is the graph this node is attached to currently. - graph *Graph // label is a descriptive string for the // node, and is set with `SetLabel` label string diff --git a/node_test.go b/node_test.go index a00e5ad..309358e 100644 --- a/node_test.go +++ b/node_test.go @@ -12,7 +12,6 @@ import ( func Test_NewNode(t *testing.T) { n := NewNode("test_node") testutil.NotNil(t, n.id) - testutil.Nil(t, n.graph) testutil.Equal(t, "test_node", n.kind) testutil.Equal(t, 0, len(n.parents)) testutil.Equal(t, 0, len(n.children)) @@ -76,10 +75,10 @@ func Test_SetStale(t *testing.T) { testutil.Equal(t, 0, n.n.recomputedAt) testutil.Equal(t, 1, n.n.setAt) - testutil.Equal(t, true, n.n.graph.recomputeHeap.has(n)) + testutil.Equal(t, true, graphFromScope(n).recomputeHeap.has(n)) // find the node in the recompute heap layer - testutil.Equal(t, 1, n.n.graph.recomputeHeap.heights[0].len()) + testutil.Equal(t, 1, graphFromScope(n).recomputeHeap.heights[0].len()) g.SetStale(n) @@ -87,9 +86,9 @@ func Test_SetStale(t *testing.T) { testutil.Equal(t, 0, n.n.recomputedAt) testutil.Equal(t, 1, n.n.setAt) - testutil.Equal(t, true, n.n.graph.recomputeHeap.has(n)) + testutil.Equal(t, true, graphFromScope(n).recomputeHeap.has(n)) - testutil.Equal(t, 1, n.n.graph.recomputeHeap.heights[0].len()) + testutil.Equal(t, 1, graphFromScope(n).recomputeHeap.heights[0].len()) } func Test_Node_OnUpdate(t *testing.T) { diff --git a/observe.go b/observe.go index d88579f..9843c6a 100644 --- a/observe.go +++ b/observe.go @@ -83,14 +83,10 @@ func (o *observeIncr[A]) Stabilize(_ context.Context) error { // // To observe parts of a graph again, use the `MustObserve(...)` helper. func (o *observeIncr[A]) Unobserve(ctx context.Context) { - g := o.n.graph - + g := graphFromScope(o) g.removeObserver(o) - o.input.Node().removeObserver(o.n.id) g.checkIfUnnecessary(o.input) - - // zero out the observed value var value A o.value = value o.input = nil diff --git a/scope.go b/scope.go index 397efd7..99c7ee7 100644 --- a/scope.go +++ b/scope.go @@ -36,6 +36,13 @@ type Scope interface { fmt.Stringer } +func graphFromScope(node INode) *Graph { + if node == nil { + return nil + } + return node.Node().createdIn.scopeGraph() +} + func maybeRemoveScopeNode(scope Scope, node INode) { if scope == nil || scope != nil && scope.isTopScope() { return diff --git a/stabilize_test.go b/stabilize_test.go index 370d54e..50058b5 100644 --- a/stabilize_test.go +++ b/stabilize_test.go @@ -465,8 +465,9 @@ func Test_Stabilize_Bind(t *testing.T) { Equal(t, true, g.Has(i1)) Equal(t, true, g.Has(m1)) - NotNil(t, i1.Node().graph, "i1 should be in the graph after the first stabilization") - NotNil(t, m1.Node().graph, "m1 should be in the graph after the first stabilization") + + Equal(t, true, i1.Node().isNecessary()) + Equal(t, true, m1.Node().isNecessary()) Equal(t, "bar-loo-baz", mb.Value()) @@ -478,8 +479,9 @@ func Test_Stabilize_Bind(t *testing.T) { Equal(t, true, g.Has(i0)) Equal(t, true, g.Has(m0)) - NotNil(t, i0.Node().graph, "i0 should be in the graph after the second stabilization") - NotNil(t, m0.Node().graph, "m0 should be in the graph after the second stabilization") + + Equal(t, true, i0.Node().isNecessary()) + Equal(t, true, m0.Node().isNecessary()) Equal(t, false, g.Has(i1)) Equal(t, false, g.Has(m1)) @@ -508,8 +510,8 @@ func Test_Stabilize_BindIf(t *testing.T) { err := g.Stabilize(ctx) Nil(t, err) - Nil(t, i0.Node().graph, "i0 should not be in the graph after the first stabilization") - NotNil(t, i1.Node().graph, "i1 should be in the graph after the first stabilization") + // Nil(t, i0.Node().graph, "i0 should not be in the graph after the first stabilization") + // NotNil(t, i1.Node().graph, "i1 should be in the graph after the first stabilization") Equal(t, "bar", b.Value()) @@ -517,7 +519,7 @@ func Test_Stabilize_BindIf(t *testing.T) { err = g.Stabilize(ctx) Nil(t, err) - NotNil(t, i0.Node().graph, "i1 should not be in the graph after the third stabilization") + // NotNil(t, i0.Node().graph, "i1 should not be in the graph after the third stabilization") Equal(t, "foo", b.Value()) } diff --git a/var.go b/var.go index 1974aba..a0474e8 100644 --- a/var.go +++ b/var.go @@ -62,19 +62,20 @@ func (vn *varIncr[T]) ShouldBeInvalidated() bool { } func (vn *varIncr[T]) Set(v T) { - if vn.n.graph != nil && atomic.LoadInt32(&vn.n.graph.status) == StatusStabilizing { + graph := graphFromScope(vn) + if graph != nil && atomic.LoadInt32(&graph.status) == StatusStabilizing { vn.setDuringStabilizationValue = v vn.setDuringStabilization = true - vn.n.graph.setDuringStabilizationMu.Lock() - vn.n.graph.setDuringStabilization[vn.Node().id] = vn - vn.n.graph.setDuringStabilizationMu.Unlock() + graph.setDuringStabilizationMu.Lock() + graph.setDuringStabilization[vn.Node().id] = vn + graph.setDuringStabilizationMu.Unlock() return } vn.value = v - if vn.n.graph != nil { - vn.n.graph.SetStale(vn) + if graph != nil { + graph.SetStale(vn) } }