-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AST: Allow children of commutative nodes to be reordered cost-wise.
This adds several things: * Commutative functions are marked as such. * AST functions now have an associated cost depending on their computational complexity. * Evaluation will now reorder the children of a commutative node to execute the cheapest first. This would have the benefit, thanks to the lazy evaluation of AST nodes (circuit breaking), to improve overall performance for those scenarii.
- Loading branch information
Antoine Popineau
committed
Jan 16, 2025
1 parent
d0d3c04
commit 47f3847
Showing
11 changed files
with
173 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ast_eval | ||
|
||
import ( | ||
"cmp" | ||
"slices" | ||
|
||
"github.com/checkmarble/marble-backend/models/ast" | ||
) | ||
|
||
// Weighted nodes manages a flat list of nodes and offers an interface to process | ||
// them sorted by node cost. A lower-cost node will be executed earlier when the | ||
// parent is commutative. | ||
// | ||
// The parent is passed to the constructor, so that if it is not commutative, this | ||
// is basically a no-op. | ||
type WeightedNodes struct { | ||
enabled bool | ||
original []ast.Node | ||
} | ||
|
||
func NewWeightedNodes(env AstEvaluationEnvironment, parent ast.Node, nodes []ast.Node) WeightedNodes { | ||
enabled := false | ||
|
||
if !env.disableCostOptimizations { | ||
if fattrs, err := parent.Function.Attributes(); err == nil { | ||
enabled = fattrs.Commutative | ||
} | ||
} | ||
|
||
if enabled { | ||
for idx := range nodes { | ||
nodes[idx].Index = idx | ||
} | ||
} | ||
|
||
return WeightedNodes{ | ||
enabled: enabled, | ||
original: nodes, | ||
} | ||
} | ||
|
||
func (wn WeightedNodes) Sorted() []ast.Node { | ||
if !wn.enabled { | ||
return wn.original | ||
} | ||
|
||
return slices.SortedFunc(slices.Values(wn.original), func(lhs, rhs ast.Node) int { | ||
return cmp.Compare(lhs.Cost(), rhs.Cost()) | ||
}) | ||
} | ||
|
||
func (wn WeightedNodes) Reorder(results []ast.NodeEvaluation) []ast.NodeEvaluation { | ||
if !wn.enabled { | ||
return results | ||
} | ||
|
||
output := make([]ast.NodeEvaluation, len(wn.original)) | ||
|
||
for idx := range wn.original { | ||
output[idx] = ast.NodeEvaluation{ | ||
Index: idx, | ||
Skipped: true, | ||
ReturnValue: nil, | ||
} | ||
} | ||
|
||
for _, result := range results { | ||
output[result.Index] = result | ||
output[result.Index].Skipped = false | ||
} | ||
|
||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters