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:
@@ -1259,7 +1259,7 @@ func TestAstFunc2(t *testing.T) {
|
||||
t.Errorf("test #%d: init error with func engine: %+v", index, err)
|
||||
return
|
||||
}
|
||||
defer funcs.Cleanup()
|
||||
//defer funcs.Cleanup()
|
||||
|
||||
// XXX: can we type check things somehow?
|
||||
//logf("function engine validating...")
|
||||
@@ -1269,52 +1269,6 @@ func TestAstFunc2(t *testing.T) {
|
||||
// return
|
||||
//}
|
||||
|
||||
logf("function engine starting...")
|
||||
wg := &sync.WaitGroup{}
|
||||
defer wg.Wait()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := funcs.Run(ctx); err != nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: run error with func engine: %+v", index, err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
//wg.Add(1)
|
||||
//go func() { // XXX: debugging
|
||||
// defer wg.Done()
|
||||
// for {
|
||||
// select {
|
||||
// case <-time.After(100 * time.Millisecond): // blocked functions
|
||||
// t.Logf("test #%d: graphviz...", index)
|
||||
// funcs.Graphviz("") // log to /tmp/...
|
||||
//
|
||||
// case <-ctx.Done():
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//}()
|
||||
|
||||
<-funcs.Started() // wait for startup (will not block forever)
|
||||
|
||||
// Sanity checks for graph size.
|
||||
if count := funcs.NumVertices(); count != 0 {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: expected empty graph on start, got %d vertices", index, count)
|
||||
}
|
||||
defer func() {
|
||||
if count := funcs.NumVertices(); count != 0 {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: expected empty graph on exit, got %d vertices", index, count)
|
||||
}
|
||||
}()
|
||||
defer wg.Wait()
|
||||
defer cancel()
|
||||
|
||||
txn := funcs.Txn()
|
||||
defer txn.Free() // remember to call Free()
|
||||
txn.AddGraph(fgraph)
|
||||
@@ -1325,6 +1279,70 @@ func TestAstFunc2(t *testing.T) {
|
||||
}
|
||||
defer txn.Reverse() // should remove everything we added
|
||||
|
||||
logf("function engine starting...")
|
||||
wg := &sync.WaitGroup{}
|
||||
defer wg.Wait()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := funcs.Run(ctx); err != nil {
|
||||
if err == context.Canceled { // normal test shutdown
|
||||
return
|
||||
}
|
||||
// check in funcs.Err() instead.
|
||||
//t.Errorf("test #%d: FAIL", index)
|
||||
//t.Errorf("test #%d: run error with func engine: %+v", index, err)
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
err := errwrap.WithoutContext(funcs.Err())
|
||||
if err == context.Canceled { // normal test shutdown
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if (!fail || !failStream) && err != nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
return
|
||||
}
|
||||
if failStream && err != nil {
|
||||
t.Logf("test #%d: stream errored: %+v", index, err)
|
||||
// Stream errors often have pointers in them, so don't compare for now.
|
||||
//s := err.Error() // convert to string
|
||||
//if !foundErr(s) {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: expected different error", index)
|
||||
// t.Logf("test #%d: err: %s", index, s)
|
||||
// t.Logf("test #%d: exp: %s", index, expstr)
|
||||
//}
|
||||
return
|
||||
}
|
||||
if failStream && err == nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream passed, expected fail", index)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
// Sanity checks for graph size.
|
||||
//if count := funcs.NumVertices(); count != 0 {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: expected empty graph on start, got %d vertices", index, count)
|
||||
//}
|
||||
//defer func() {
|
||||
// if count := funcs.NumVertices(); count != 0 {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: expected empty graph on exit, got %d vertices", index, count)
|
||||
// }
|
||||
//}()
|
||||
defer wg.Wait()
|
||||
defer cancel()
|
||||
|
||||
isEmpty := make(chan struct{})
|
||||
if fgraph.NumVertices() == 0 { // no funcs to load!
|
||||
close(isEmpty)
|
||||
@@ -1332,64 +1350,24 @@ func TestAstFunc2(t *testing.T) {
|
||||
|
||||
// wait for some activity
|
||||
logf("stream...")
|
||||
stream := funcs.Stream()
|
||||
//select {
|
||||
//case err, ok := <-stream:
|
||||
// if !ok {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream closed", index)
|
||||
// return
|
||||
// }
|
||||
// if err != nil {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
//case <-time.After(60 * time.Second): // blocked functions
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream timeout", index)
|
||||
// return
|
||||
//}
|
||||
tableChan := funcs.Stream()
|
||||
|
||||
// sometimes the <-stream seems to constantly (or for a
|
||||
// long time?) win the races against the <-time.After(),
|
||||
// so add some limit to how many times we need to stream
|
||||
max := 1
|
||||
var table interfaces.Table
|
||||
Loop:
|
||||
for {
|
||||
var ok bool
|
||||
select {
|
||||
case err, ok := <-stream:
|
||||
case table, ok = <-tableChan:
|
||||
if !ok {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream closed", index)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
if (!fail || !failStream) && err != nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
return
|
||||
}
|
||||
if failStream && err != nil {
|
||||
t.Logf("test #%d: stream errored: %+v", index, err)
|
||||
// Stream errors often have pointers in them, so don't compare for now.
|
||||
//s := err.Error() // convert to string
|
||||
//if !foundErr(s) {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: expected different error", index)
|
||||
// t.Logf("test #%d: err: %s", index, s)
|
||||
// t.Logf("test #%d: exp: %s", index, expstr)
|
||||
//}
|
||||
return
|
||||
}
|
||||
if failStream && err == nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream passed, expected fail", index)
|
||||
return
|
||||
}
|
||||
//t.Errorf("test #%d: FAIL", index) // check in funcs.Err() instead.
|
||||
t.Logf("test #%d: stream closed", index)
|
||||
return
|
||||
}
|
||||
|
||||
t.Logf("test #%d: got stream event!", index)
|
||||
max--
|
||||
if max == 0 {
|
||||
@@ -1410,11 +1388,9 @@ func TestAstFunc2(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("test #%d: %s", index, funcs.Stats())
|
||||
//t.Logf("test #%d: %s", index, funcs.Stats())
|
||||
|
||||
// run interpret!
|
||||
table := funcs.Table() // map[interfaces.Func]types.Value
|
||||
|
||||
interpreter := &interpret.Interpreter{
|
||||
Debug: testing.Verbose(), // set via the -test.v flag to `go test`
|
||||
Logf: func(format string, v ...interface{}) {
|
||||
@@ -2138,7 +2114,7 @@ func TestAstFunc3(t *testing.T) {
|
||||
t.Errorf("test #%d: init error with func engine: %+v", index, err)
|
||||
return
|
||||
}
|
||||
defer funcs.Cleanup()
|
||||
//defer funcs.Cleanup()
|
||||
|
||||
// XXX: can we type check things somehow?
|
||||
//logf("function engine validating...")
|
||||
@@ -2148,52 +2124,6 @@ func TestAstFunc3(t *testing.T) {
|
||||
// return
|
||||
//}
|
||||
|
||||
logf("function engine starting...")
|
||||
wg := &sync.WaitGroup{}
|
||||
defer wg.Wait()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := funcs.Run(ctx); err != nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: run error with func engine: %+v", index, err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
//wg.Add(1)
|
||||
//go func() { // XXX: debugging
|
||||
// defer wg.Done()
|
||||
// for {
|
||||
// select {
|
||||
// case <-time.After(100 * time.Millisecond): // blocked functions
|
||||
// t.Logf("test #%d: graphviz...", index)
|
||||
// funcs.Graphviz("") // log to /tmp/...
|
||||
//
|
||||
// case <-ctx.Done():
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
//}()
|
||||
|
||||
<-funcs.Started() // wait for startup (will not block forever)
|
||||
|
||||
// Sanity checks for graph size.
|
||||
if count := funcs.NumVertices(); count != 0 {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: expected empty graph on start, got %d vertices", index, count)
|
||||
}
|
||||
defer func() {
|
||||
if count := funcs.NumVertices(); count != 0 {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: expected empty graph on exit, got %d vertices", index, count)
|
||||
}
|
||||
}()
|
||||
defer wg.Wait()
|
||||
defer cancel()
|
||||
|
||||
txn := funcs.Txn()
|
||||
defer txn.Free() // remember to call Free()
|
||||
txn.AddGraph(fgraph)
|
||||
@@ -2204,6 +2134,74 @@ func TestAstFunc3(t *testing.T) {
|
||||
}
|
||||
defer txn.Reverse() // should remove everything we added
|
||||
|
||||
logf("function engine starting...")
|
||||
wg := &sync.WaitGroup{}
|
||||
defer wg.Wait()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := funcs.Run(ctx); err != nil {
|
||||
if err == context.Canceled { // normal test shutdown
|
||||
return
|
||||
}
|
||||
// check in funcs.Err() instead.
|
||||
//t.Errorf("test #%d: FAIL", index)
|
||||
//t.Errorf("test #%d: run error with func engine: %+v", index, err)
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
err := errwrap.WithoutContext(funcs.Err())
|
||||
if err == context.Canceled { // normal test shutdown
|
||||
return
|
||||
}
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
return
|
||||
|
||||
//if (!fail || !failStream) && err != nil {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
// return
|
||||
//}
|
||||
//if failStream && err != nil {
|
||||
// t.Logf("test #%d: stream errored: %+v", index, err)
|
||||
// // Stream errors often have pointers in them, so don't compare for now.
|
||||
// //s := err.Error() // convert to string
|
||||
// //if !foundErr(s) {
|
||||
// // t.Errorf("test #%d: FAIL", index)
|
||||
// // t.Errorf("test #%d: expected different error", index)
|
||||
// // t.Logf("test #%d: err: %s", index, s)
|
||||
// // t.Logf("test #%d: exp: %s", index, expstr)
|
||||
// //}
|
||||
// return
|
||||
//}
|
||||
//if failStream && err == nil {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream passed, expected fail", index)
|
||||
// return
|
||||
//}
|
||||
}()
|
||||
|
||||
// Sanity checks for graph size.
|
||||
//if count := funcs.NumVertices(); count != 0 {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: expected empty graph on start, got %d vertices", index, count)
|
||||
//}
|
||||
//defer func() {
|
||||
// if count := funcs.NumVertices(); count != 0 {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: expected empty graph on exit, got %d vertices", index, count)
|
||||
// }
|
||||
//}()
|
||||
defer wg.Wait()
|
||||
defer cancel()
|
||||
|
||||
isEmpty := make(chan struct{})
|
||||
if fgraph.NumVertices() == 0 { // no funcs to load!
|
||||
close(isEmpty)
|
||||
@@ -2211,44 +2209,29 @@ func TestAstFunc3(t *testing.T) {
|
||||
|
||||
// wait for some activity
|
||||
logf("stream...")
|
||||
stream := funcs.Stream()
|
||||
//select {
|
||||
//case err, ok := <-stream:
|
||||
// if !ok {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream closed", index)
|
||||
// return
|
||||
// }
|
||||
// if err != nil {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
//case <-time.After(60 * time.Second): // blocked functions
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream timeout", index)
|
||||
// return
|
||||
//}
|
||||
tableChan := funcs.Stream()
|
||||
|
||||
// sometimes the <-stream seems to constantly (or for a
|
||||
// long time?) win the races against the <-time.After(),
|
||||
// so add some limit to how many times we need to stream
|
||||
max := 1
|
||||
var table interfaces.Table
|
||||
Loop:
|
||||
for {
|
||||
var ok bool
|
||||
select {
|
||||
case err, ok := <-stream:
|
||||
case table, ok = <-tableChan:
|
||||
if !ok {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream closed", index)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("test #%d: FAIL", index)
|
||||
t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
//t.Errorf("test #%d: FAIL", index) // check in funcs.Err() instead.
|
||||
t.Errorf("test #%d: FAIL", index) // check in funcs.Err() instead.
|
||||
t.Logf("test #%d: stream closed", index)
|
||||
return
|
||||
}
|
||||
//if err != nil {
|
||||
// t.Errorf("test #%d: FAIL", index)
|
||||
// t.Errorf("test #%d: stream errored: %+v", index, err)
|
||||
// return
|
||||
//}
|
||||
t.Logf("test #%d: got stream event!", index)
|
||||
max--
|
||||
if max == 0 {
|
||||
@@ -2269,10 +2252,9 @@ func TestAstFunc3(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("test #%d: %s", index, funcs.Stats())
|
||||
//t.Logf("test #%d: %s", index, funcs.Stats())
|
||||
|
||||
// run interpret!
|
||||
table := funcs.Table() // map[interfaces.Func]types.Value
|
||||
|
||||
interpreter := &interpret.Interpreter{
|
||||
Debug: testing.Verbose(), // set via the -test.v flag to `go test`
|
||||
|
||||
Reference in New Issue
Block a user