Skip to content

Commit

Permalink
no more locking in nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
wcharczuk committed Feb 15, 2024
1 parent 56a799a commit 6015072
Showing 1 changed file with 6 additions and 62 deletions.
68 changes: 6 additions & 62 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package incr
import (
"context"
"fmt"
"sync"
)

// NewNode returns a new node.
Expand All @@ -15,9 +14,6 @@ func NewNode(kind string) *Node {
height: heightUnset,
heightInRecomputeHeap: heightUnset,
heightInAdjustHeightsHeap: heightUnset,
parentLookup: make(set[Identifier]),
childLookup: make(set[Identifier]),
observerLookup: make(set[Identifier]),
}
}

Expand All @@ -36,20 +32,13 @@ type Node struct {
label string
// parents are the nodes that this node depends on, that is
// parents are nodes that this node takes as inputs
parents []INode
parentLookupMu sync.Mutex
parentLookup set[Identifier]
parents []INode
// children are the nodes that depend on this node, that is
// children take this node as an input
children []INode
childLookupMu sync.Mutex
childLookup set[Identifier]

children []INode
// observers are observer nodes that are attached to this
// node or its children.
observers []IObserver
observerLookupMu sync.Mutex
observerLookup set[Identifier]
observers []IObserver
// height is the topological sort pseudo-height of the
// node and is used to order recomputation
// it is established when the graph is initialized but
Expand Down Expand Up @@ -206,78 +195,33 @@ func (n *Node) initializeFrom(in INode) {
}

func (n *Node) addChildren(children ...INode) {
n.childLookupMu.Lock()
for _, c := range children {
if !n.childLookup.has(c.Node().id) {
n.children = append(n.children, c)
n.childLookup.add(c.Node().id)
}
n.children = append(n.children, c)
}
n.childLookupMu.Unlock()
}

func (n *Node) addParents(parents ...INode) {
n.parentLookupMu.Lock()
for _, p := range parents {
if !n.parentLookup.has(p.Node().id) {
n.parents = append(n.parents, p)
n.parentLookup.add(p.Node().id)
}
n.parents = append(n.parents, p)
}
n.parentLookupMu.Unlock()
}

func (n *Node) addObservers(observers ...IObserver) {
n.observerLookupMu.Lock()
for _, o := range observers {
if !n.observerLookup.has(o.Node().id) {
n.observers = append(n.observers, o)
n.observerLookup.add(o.Node().id)
}
n.observers = append(n.observers, o)
}
n.observerLookupMu.Unlock()
}

func (n *Node) hasChild(in INode) (ok bool) {
n.childLookupMu.Lock()
_, ok = n.childLookup[in.Node().id]
n.childLookupMu.Unlock()
return
}

func (n *Node) hasParent(in INode) (ok bool) {
n.parentLookupMu.Lock()
_, ok = n.parentLookup[in.Node().id]
n.parentLookupMu.Unlock()
return
}

func (n *Node) hasObserver(o IObserver) (ok bool) {
n.observerLookupMu.Lock()
_, ok = n.observerLookup[o.Node().id]
n.observerLookupMu.Unlock()
return
}

func (n *Node) removeChild(id Identifier) {
n.childLookupMu.Lock()
n.children = remove(n.children, id)
delete(n.childLookup, id)
n.childLookupMu.Unlock()
}

func (n *Node) removeParent(id Identifier) {
n.parentLookupMu.Lock()
n.parents = remove(n.parents, id)
delete(n.parentLookup, id)
n.parentLookupMu.Unlock()
}

func (n *Node) removeObserver(id Identifier) {
n.observerLookupMu.Lock()
n.observers = remove(n.observers, id)
delete(n.observerLookup, id)
n.observerLookupMu.Unlock()
}

// maybeCutoff calls the cutoff delegate if it's set, otherwise
Expand Down

0 comments on commit 6015072

Please sign in to comment.