Skip to content

Commit

Permalink
Cleanup & fix nil dep
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Apr 7, 2024
1 parent 046a8af commit 55fcd65
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 51 deletions.
2 changes: 0 additions & 2 deletions scheduler/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (wgm *WaitGroupMap) All() worker2.Dep {
defer wgm.mu.Unlock()

wg := worker2.NewGroup()
wg.LinkDeps()

for _, e := range wgm.m {
wg.AddDep(e)
Expand All @@ -80,7 +79,6 @@ func (wgm *WaitGroupMap) Get(s string) worker2.Dep {
}

wg := worker2.NewGroup()
wg.LinkDeps()
wgm.m[s] = wg

return wg
Expand Down
17 changes: 17 additions & 0 deletions utils/xtypes/xtypes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package xtypes

import "reflect"

// ForceCast force casting any type by going through any
// It is pretty somewhat unsafe, beware!
func ForceCast[B any](a any) B {
var b = a.(B)
return b
}

// IsNil allows to go around nil interfaces
// see https://stackoverflow.com/a/78104852/3212099
func IsNil(input interface{}) bool {
if input == nil {
return true
}
kind := reflect.ValueOf(input).Kind()
switch kind {
case reflect.Ptr, reflect.Map, reflect.Slice, reflect.Chan:
return reflect.ValueOf(input).IsNil()
default:
return false
}
}
20 changes: 0 additions & 20 deletions worker2/dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ func (a *Action) Exec(ctx context.Context, ins InStore, outs OutStore) error {
}

func (a *Action) GetDepsObj() *Deps {
if a.deps == nil {
a.deps = NewDeps()
}
a.deps.setOwner(a)
return a.deps
}

Expand All @@ -215,12 +211,6 @@ func (a *Action) DeepDo(f func(Dep)) {
deepDo(a, f)
}

func (a *Action) LinkDeps() {
for _, dep := range a.GetDepsObj().TransitiveDependencies() {
_ = dep.GetDepsObj()
}
}

type GroupConfig struct {
Name string
Deps []Dep
Expand All @@ -238,17 +228,7 @@ func (g *Group) GetName() string {
return g.name
}

func (g *Group) LinkDeps() {
for _, dep := range g.GetDepsObj().TransitiveDependencies() {
_ = dep.GetDepsObj()
}
}

func (g *Group) GetDepsObj() *Deps {
if g.deps == nil {
g.deps = NewDeps()
}
g.deps.setOwner(g)
return g.deps
}

Expand Down
4 changes: 2 additions & 2 deletions worker2/dep_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ type EventDeclared struct {
}

func NewAction(cfg ActionConfig) *Action {
a := &Action{}
_ = a.GetDepsObj()
a := &Action{deps: NewDeps()}
a.deps.setOwner(a)

a.name = cfg.Name
a.ctx = cfg.Ctx
Expand Down
4 changes: 2 additions & 2 deletions worker2/dep_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ func NewGroup(deps ...Dep) *Group {
}

func NewGroupWith(cfg GroupConfig) *Group {
g := &Group{}
_ = g.GetDepsObj()
g := &Group{deps: NewDeps()}
g.deps.setOwner(g)

g.name = cfg.Name
g.AddDep(cfg.Deps...)
Expand Down
4 changes: 2 additions & 2 deletions worker2/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/hephbuild/heph/utils/ads"
"github.com/hephbuild/heph/utils/sets"
"github.com/hephbuild/heph/utils/xtypes"
"strings"
"sync"
)
Expand Down Expand Up @@ -96,7 +97,6 @@ func (d *Deps) flattenNamed(deps []Dep) []Dep {
}
return fdeps
}

func (d *Deps) Add(deps ...Dep) {
d.m.Lock()
defer d.m.Unlock()
Expand All @@ -106,7 +106,7 @@ func (d *Deps) Add(deps ...Dep) {
}

for _, dep := range deps {
if dep == nil {
if xtypes.IsNil(dep) {
continue
}
if !d.has(dep) {
Expand Down
38 changes: 15 additions & 23 deletions worker2/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ func s(s string) string {
}

func TestLink(t *testing.T) {
d1 := &Action{name: "1", deps: NewDeps()}
d2 := &Action{name: "2", deps: NewDeps(d1)}
d1 := NewAction(ActionConfig{Name: "1"})
d2 := NewAction(ActionConfig{Name: "2", Deps: []Dep{d1}})

d3 := &Action{name: "3", deps: NewDeps()}
d4 := &Action{name: "4", deps: NewDeps(d3)}

for _, d := range []*Action{d1, d2, d3, d4} {
d.LinkDeps()
}
d3 := NewAction(ActionConfig{Name: "3"})
d4 := NewAction(ActionConfig{Name: "4", Deps: []Dep{d3}})

assertDetached := func() {
assert.Equal(t, s(`
Expand Down Expand Up @@ -98,21 +94,21 @@ func TestLink(t *testing.T) {
}

func TestCycle1(t *testing.T) {
d1 := &Action{name: "1", deps: NewDeps()}
d2 := &Action{name: "2", deps: NewDeps(d1)}
d3 := &Action{name: "3", deps: NewDeps(d2)}
d1 := NewAction(ActionConfig{Name: "1"})
d2 := NewAction(ActionConfig{Name: "2", Deps: []Dep{d1}})
d3 := NewAction(ActionConfig{Name: "3", Deps: []Dep{d2}})

assert.PanicsWithValue(t, "cycle", func() {
d2.AddDep(d3)
})
}

func TestCycle2(t *testing.T) {
d1 := &Action{name: "1", deps: NewDeps( /* d4 */ )}
d2 := &Action{name: "2", deps: NewDeps(d1)}
d1 := NewAction(ActionConfig{Name: "1", Deps: []Dep{ /* d4 */ }})
d2 := NewAction(ActionConfig{Name: "2", Deps: []Dep{d1}})

d3 := &Action{name: "3", deps: NewDeps()}
d4 := &Action{name: "4", deps: NewDeps(d3)}
d3 := NewAction(ActionConfig{Name: "3"})
d4 := NewAction(ActionConfig{Name: "4", Deps: []Dep{d3}})

d1.AddDep(d4)

Expand All @@ -122,23 +118,19 @@ func TestCycle2(t *testing.T) {
}

func TestRemoveStress(t *testing.T) {
root := &Action{name: "root", deps: NewDeps()}
root.LinkDeps()
root := NewAction(ActionConfig{Name: "root", Deps: []Dep{}})

for i := 0; i < 1000; i++ {
d := &Action{name: fmt.Sprint(i)}
d.LinkDeps()
d := NewAction(ActionConfig{Name: fmt.Sprint(i)})
root.AddDep(d)

for j := 0; j < 1000; j++ {
d1 := &Action{name: fmt.Sprintf("%v-%v", i, j)}
d1.LinkDeps()
d1 := NewAction(ActionConfig{Name: fmt.Sprintf("%v-%v", i, j)})
d.AddDep(d1)
}
}

group := &Action{name: "group", deps: NewDeps(root)}
group.LinkDeps()
group := NewAction(ActionConfig{Name: "group", Deps: []Dep{root}})

group.GetDepsObj().Remove(root)
}

0 comments on commit 55fcd65

Please sign in to comment.