Skip to content

Commit

Permalink
removing graph reference from node metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
wcharczuk committed Feb 15, 2024
1 parent 325b092 commit 13e87c7
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion expert_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion freeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -76,20 +75,20 @@ 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)

testutil.Equal(t, 0, n.n.changedAt)
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) {
Expand Down
6 changes: 1 addition & 5 deletions observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions stabilize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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))
Expand Down Expand Up @@ -508,16 +510,16 @@ 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())

sw.Set(true)
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())
}
Expand Down
13 changes: 7 additions & 6 deletions var.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 13e87c7

Please sign in to comment.