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:
@@ -277,6 +277,11 @@ func (obj *opDeleteVertex) String() string {
|
||||
// functions in their Stream method to modify the function graph while it is
|
||||
// "running".
|
||||
type GraphTxn struct {
|
||||
|
||||
// Post runs some effects after the Commit has run succesfully, but
|
||||
// before it exits.
|
||||
Post func( /* deltaOps */ ) error
|
||||
|
||||
// Lock is a handle to the lock function to call before the operation.
|
||||
Lock func()
|
||||
|
||||
@@ -313,6 +318,16 @@ func (obj *GraphTxn) Init() interfaces.Txn {
|
||||
obj.rev = []opfn{}
|
||||
obj.mutex = &sync.Mutex{}
|
||||
|
||||
if obj.Post == nil {
|
||||
panic("the Post function is nil")
|
||||
}
|
||||
if obj.Lock == nil {
|
||||
panic("the Lock function is nil")
|
||||
}
|
||||
if obj.Unlock == nil {
|
||||
panic("the Unlock function is nil")
|
||||
}
|
||||
|
||||
return obj // return self so it can be called in a chain
|
||||
}
|
||||
|
||||
@@ -322,6 +337,7 @@ func (obj *GraphTxn) Init() interfaces.Txn {
|
||||
// TODO: FreeFunc isn't well supported here. Replace or remove this entirely?
|
||||
func (obj *GraphTxn) Copy() interfaces.Txn {
|
||||
txn := &GraphTxn{
|
||||
Post: obj.Post,
|
||||
Lock: obj.Lock,
|
||||
Unlock: obj.Unlock,
|
||||
GraphAPI: obj.GraphAPI,
|
||||
@@ -403,7 +419,7 @@ func (obj *GraphTxn) AddGraph(g *pgraph.Graph) interfaces.Txn {
|
||||
for _, v := range g.Vertices() {
|
||||
f, ok := v.(interfaces.Func)
|
||||
if !ok {
|
||||
panic("not a Func")
|
||||
panic("not a Func") // XXX: why does this panic not appear until we ^C ?
|
||||
}
|
||||
//obj.AddVertex(f) // easy
|
||||
opfn := &opAddVertex{ // replicate AddVertex
|
||||
@@ -505,6 +521,7 @@ func (obj *GraphTxn) commit() error {
|
||||
//obj.rev = append([]opfn{op}, obj.rev...) // add to front
|
||||
}
|
||||
}
|
||||
//deltaOps := obj.ops // copy for delta graph effects
|
||||
obj.ops = []opfn{} // clear it
|
||||
|
||||
// garbage collect anything that hit zero!
|
||||
@@ -514,6 +531,13 @@ func (obj *GraphTxn) commit() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// This runs after all the operations have been completely successfully.
|
||||
// XXX: We'd like to pass in the graph delta information, so that we can
|
||||
// more efficiently recompute the topological sort and so on...
|
||||
if err := obj.Post( /* deltaOps */ ); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// XXX: running this on each commit has a huge performance hit.
|
||||
// XXX: we could write out the .dot files and run graphviz afterwards
|
||||
if g, ok := obj.GraphAPI.(pgraph.Graphvizable); ok && GraphvizDebug {
|
||||
|
||||
Reference in New Issue
Block a user