Skip to content

Commit

Permalink
Cache deepDo
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed May 11, 2024
1 parent 081076e commit 7c1c3a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
7 changes: 7 additions & 0 deletions utils/sets/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ func (ts *Set[K, T]) Slice() []T {
return nil
}

ts.mu.RLock()
defer ts.mu.RUnlock()

return ts.a[:]
}

Expand All @@ -118,6 +121,9 @@ func (ts *Set[K, T]) Len() int {
return 0
}

ts.mu.RLock()
defer ts.mu.RUnlock()

return len(ts.a)
}

Expand All @@ -143,6 +149,7 @@ func (ts *Set[K, T]) Remove(v T) {
k := ts.f(v)

delete(ts.m, k)

ts.a = slices.DeleteFunc(ts.a, func(t T) bool {
return ts.f(t) == k
})
Expand Down
49 changes: 33 additions & 16 deletions worker2/dep_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import (
"sync/atomic"
)

func deepDo(a Dep, f func(Dep)) {
if a.GetNode().IsFrozen() {
// This approach sounds good on paper, but in reality very CPU intensive since it requires
// read & write to the deps set at every change, at every level...
deepDoPrecomputed(a, f)
} else {
deepDoRecursive(a, f)
}
}

func deepDoPrecomputed(a Dep, f func(Dep)) {
f(a)
for _, dep := range a.GetNode().Dependencies.TransitiveValues() {
Expand All @@ -17,32 +27,39 @@ var deepDoMapPool = xsync.Pool[map[Dep]struct{}]{New: func() map[Dep]struct{} {
return map[Dep]struct{}{}
}}

func deepDo(a Dep, f func(Dep)) {
if false {
// This approach sounds good on paper, but in reality very CPU intensive since it requires
// read & write to the deps set at every change, at every level...
deepDoPrecomputed(a, f)
} else {
deepDoRecursive(a, f)
}
}

func deepDoRecursive(a Dep, f func(Dep)) {
m := deepDoMapPool.Get()
maps.Clear(m)
defer deepDoMapPool.Put(m)
defer func() {
maps.Clear(m)
deepDoMapPool.Put(m)
}()
deepDoRecursiveInner(a, f, m)
}

func deepDoRecursiveInner(a Dep, f func(Dep), m map[Dep]struct{}) {
func deepDoInner(a Dep, f func(Dep), m map[Dep]struct{}) bool {
if _, ok := m[a]; ok {
return
return false
}
m[a] = struct{}{}

f(a)
for _, dep := range a.GetNode().Dependencies.Values() {
deepDoRecursiveInner(dep, f, m)

return true
}

func deepDoRecursiveInner(a Dep, f func(Dep), m map[Dep]struct{}) {
if !deepDoInner(a, f, m) {
return
}

if a.GetNode().IsFrozen() {
for _, dep := range a.GetNode().Dependencies.TransitiveValues() {
deepDoInner(dep, f, m)
}
} else {
for _, dep := range a.GetNode().Dependencies.Values() {
deepDoRecursiveInner(dep, f, m)
}
}
}

Expand Down

0 comments on commit 7c1c3a6

Please sign in to comment.