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

@@ -64,8 +64,8 @@ type ModinfoLoadedFunc struct {
init *interfaces.Init
last types.Value // last value received to use for diff
input chan string // stream of inputs
modulename *string // the active module name
result types.Value // last calculated output
}
// String returns a simple name for this function. This is needed so this struct
@@ -103,13 +103,12 @@ func (obj *ModinfoLoadedFunc) Info() *interfaces.Info {
// Init runs some startup code for this function.
func (obj *ModinfoLoadedFunc) Init(init *interfaces.Init) error {
obj.init = init
obj.input = make(chan string)
return nil
}
// Stream returns the changing values that this func has over time.
func (obj *ModinfoLoadedFunc) Stream(ctx context.Context) error {
defer close(obj.init.Output) // the sender closes
// create new watcher
// XXX: does this file produce inotify events?
recWatcher := &recwatch.RecWatcher{
@@ -127,22 +126,12 @@ func (obj *ModinfoLoadedFunc) Stream(ctx context.Context) error {
for {
select {
case input, ok := <-obj.init.Input:
case modulename, 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
modulename := input.Struct()[modinfoLoadedArgNameModule].Str()
// TODO: add check for empty string
if obj.modulename != nil && *obj.modulename == modulename {
continue // nothing changed
}
@@ -156,33 +145,13 @@ func (obj *ModinfoLoadedFunc) Stream(ctx context.Context) error {
return errwrap.Wrapf(err, "error event received")
}
if obj.last == nil {
if obj.modulename == nil {
continue // still waiting for input values
}
case <-ctx.Done():
return nil
}
args, err := interfaces.StructToCallableArgs(obj.last) // []types.Value, error)
if err != nil {
return err
}
result, err := obj.Call(ctx, args)
if err != nil {
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
select {
case obj.init.Output <- obj.result: // send
// pass
if err := obj.init.Event(ctx); err != nil { // send event
return err
}
case <-ctx.Done():
return nil
@@ -198,6 +167,20 @@ func (obj *ModinfoLoadedFunc) Call(ctx context.Context, args []types.Value) (typ
}
modulename := args[0].Str()
// Check before we send to a chan where we'd need Stream to be running.
if obj.init == nil {
return nil, funcs.ErrCantSpeculate
}
// Tell the Stream what we're watching now... This doesn't block because
// Stream should always be ready to consume unless it's closing down...
// If it dies, then a ctx closure should come soon.
select {
case obj.input <- modulename:
case <-ctx.Done():
return nil, ctx.Err()
}
m, err := lsmod.LsMod()
if err != nil {
return nil, errwrap.Wrapf(err, "error reading modules")