Currently, if evalNode() would encounter the same node twice, it would perform all (in-circuit) computations that make up that node twice.
This could become a huge waste if we want to add expensive operations as possible nodes that should be used in several places.
There's an easy fix, we just have to add a store which contains the result of each previously evaluated node, and to reuse that when it already exists.
The only question is how to define "same" node. A first step is to just look for object identity and store intermediate results in a Map<Node, any>.
A more refined strategy would also recognize nodes that were defined multiple times but are equivalent.
Currently, if
evalNode()would encounter the same node twice, it would perform all (in-circuit) computations that make up that node twice.This could become a huge waste if we want to add expensive operations as possible nodes that should be used in several places.
There's an easy fix, we just have to add a store which contains the result of each previously evaluated node, and to reuse that when it already exists.
The only question is how to define "same" node. A first step is to just look for object identity and store intermediate results in a
Map<Node, any>.A more refined strategy would also recognize nodes that were defined multiple times but are equivalent.