lang: New function engine

This mega patch primarily introduces a new function engine. The main
reasons for this new engine are:

1) Massively improved performance with lock-contended graphs.

Certain large function graphs could have very high lock-contention which
turned out to be much slower than I would have liked. This new algorithm
happens to be basically lock-free, so that's another helpful
improvement.

2) Glitch-free function graphs.

The function graphs could "glitch" (an FRP term) which could be
undesirable in theory. In practice this was never really an issue, and
I've not explicitly guaranteed that the new graphs are provably
glitch-free, but in practice things are a lot more consistent.

3) Simpler graph shape.

The new graphs don't require the private channels. This makes
understanding the graphs a lot easier.

4) Branched graphs only run half.

Previously we would run two pure side of an if statement, and while this
was mostly meant as an early experiment, it stayed in for far too long
and now was the right time to remove this. This also means our graphs
are much smaller and more efficient too.

Note that this changed the function API slightly. Everything has been
ported. It's possible that we introduce a new API in the future, but it
is unexpected to cause removal of the two current APIs.

In addition, we finally split out the "schedule" aspect from
world.schedule(). The "pick me" aspects now happen in a separate
resource, rather than as a yucky side-effect in the function. This also
lets us more precisely choose when we're scheduled, and we can observe
without being chosen too.

As usual many thanks to Sam for helping through some of the algorithmic
graph shape issues!
This commit is contained in:
James Shubin
2025-09-09 02:46:59 -04:00
parent 1e2db5b8c5
commit 790b7199ca
109 changed files with 3632 additions and 6904 deletions

View File

@@ -100,10 +100,8 @@ type CollectFunc struct {
init *interfaces.Init
last types.Value // last value received to use for diff
args []types.Value
kind string
result types.Value // last calculated output
input chan string // stream of inputs
kind *string // the active kind
watchChan chan error
}
@@ -292,13 +290,13 @@ func (obj *CollectFunc) Info() *interfaces.Info {
// Init runs some startup code for this function.
func (obj *CollectFunc) Init(init *interfaces.Init) error {
obj.init = init
obj.input = make(chan string)
obj.watchChan = make(chan error) // XXX: sender should close this, but did I implement that part yet???
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *CollectFunc) Stream(ctx context.Context) error {
defer close(obj.init.Output) // the sender closes
ctx, cancel := context.WithCancel(ctx)
defer cancel() // important so that we cleanup the watch when exiting
for {
@@ -306,50 +304,35 @@ func (obj *CollectFunc) Stream(ctx context.Context) error {
// TODO: should this first chan be run as a priority channel to
// avoid some sort of glitch? is that even possible? can our
// hostname check with reality (below) fix that?
case input, ok := <-obj.init.Input:
case kind, ok := <-obj.input:
if !ok {
obj.init.Input = nil // don't infinite loop back
continue // no more inputs, but don't return!
obj.input = nil // don't infinite loop back
return fmt.Errorf("unexpected close")
}
//if err := input.Type().Cmp(obj.Info().Sig.Input); err != nil {
// return errwrap.Wrapf(err, "wrong function input")
//}
if obj.last != nil && input.Cmp(obj.last) == nil {
continue // value didn't change, skip it
}
obj.last = input // store for next
args, err := interfaces.StructToCallableArgs(input) // []types.Value, error)
if err != nil {
return err
}
obj.args = args
kind := args[0].Str()
if kind == "" {
return fmt.Errorf("can't use an empty kind")
}
if obj.init.Debug {
obj.init.Logf("kind: %s", kind)
if obj.kind != nil && *obj.kind == kind {
continue // nothing changed
}
// TODO: support changing the key over time?
if obj.kind == "" {
obj.kind = kind // store it
if obj.kind == nil {
obj.kind = &kind // store
var err error
// Don't send a value right away, wait for the
// first Watch startup event to get one!
obj.watchChan, err = obj.init.World.ResWatch(ctx, obj.kind) // watch for var changes
obj.watchChan, err = obj.init.World.ResWatch(ctx, kind) // watch for var changes
if err != nil {
return err
}
} else if obj.kind != kind {
return fmt.Errorf("can't change kind, previously: `%s`", obj.kind)
continue // we get values on the watch chan, not here!
}
continue // we get values on the watch chan, not here!
if *obj.kind == kind {
continue // skip duplicates
}
// *obj.kind != kind
return fmt.Errorf("can't change kind, previously: `%s`", *obj.kind)
case err, ok := <-obj.watchChan:
if !ok { // closed
@@ -360,27 +343,13 @@ func (obj *CollectFunc) Stream(ctx context.Context) error {
return nil
}
if err != nil {
return errwrap.Wrapf(err, "channel watch failed on `%s`", obj.kind)
return errwrap.Wrapf(err, "channel watch failed on `%s`", *obj.kind)
}
result, err := obj.Call(ctx, obj.args) // get the value...
if err != nil {
if err := obj.init.Event(ctx); err != nil { // send event
return err
}
// if the result is still the same, skip sending an update...
if obj.result != nil && result.Cmp(obj.result) == nil {
continue // result didn't change
}
obj.result = result // store new result
case <-ctx.Done():
return nil
}
select {
case obj.init.Output <- obj.result: // send
// pass
case <-ctx.Done():
return nil
}
@@ -402,6 +371,21 @@ func (obj *CollectFunc) Call(ctx context.Context, args []types.Value) (types.Val
return nil, fmt.Errorf("invalid resource kind: %s", kind)
}
// Check before we send to a chan where we'd need Stream to be running.
if obj.init == nil {
return nil, funcs.ErrCantSpeculate
}
if obj.init.Debug {
obj.init.Logf("kind: %s", kind)
}
select {
case obj.input <- kind:
case <-ctx.Done():
return nil, ctx.Err()
}
filters := []*engine.ResFilter{}
arg := args[1]
@@ -453,10 +437,6 @@ func (obj *CollectFunc) Call(ctx context.Context, args []types.Value) (types.Val
}
}
if obj.init == nil {
return nil, funcs.ErrCantSpeculate
}
list := types.NewList(obj.Info().Sig.Out) // collectFuncOutType
if len(filters) == 0 {