From 21d72b2f67fb065d9071907c5c3307434aad3795 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Thu, 19 Jun 2025 01:22:19 -0400 Subject: Add multiple clause and tree level optimizations Implement optimizations that can be called in parallel or serial. Optimizations occur mostly in place and result in a logically equivalent tree when used correctly. Optimizations ============= * Sort - sort all statements within a clause tree * Simplify - apply negation rules to all statements * Flatten - merge child clauses with parents * Compact - merge equivalent statements * Tidy^ - remove zero statements * Contradictions - zero contradicting statements and clauses * StrictEquality - zero fuzzy statements when exact statements are within clause * Tighten - combine multiple fuzzy statements to the least (AND) or most (OR) restrictive bounds ^: when used incorrectly can turn an invalid clause tree into a valid one --- pkg/query/optimizer_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'pkg/query/optimizer_test.go') diff --git a/pkg/query/optimizer_test.go b/pkg/query/optimizer_test.go index 4de3a12..00710f6 100644 --- a/pkg/query/optimizer_test.go +++ b/pkg/query/optimizer_test.go @@ -1,6 +1,7 @@ package query_test import ( + "runtime" "slices" "testing" @@ -163,9 +164,10 @@ func TestClause_Flatten(t *testing.T) { }, } for _, tt := range tests { - o := query.Optimizer{} + workers := uint(runtime.NumCPU()) t.Run(tt.name, func(t *testing.T) { - o.Flatten(tt.root) + o := query.NewOptimizer(tt.root, workers) + o.Flatten() slices.SortFunc(tt.root.Statements, query.StatementCmp) slices.SortFunc(tt.expected.Statements, query.StatementCmp) @@ -256,9 +258,10 @@ func TestOptimizer_Compact(t *testing.T) { }, } for _, tt := range tests { - o := query.Optimizer{} + workers := uint(runtime.NumCPU()) t.Run(tt.name, func(t *testing.T) { - o.Compact(tt.c) + o := query.NewOptimizer(tt.c, workers) + o.Compact() got := slices.Collect(tt.c.DFS()) want := slices.Collect(tt.want.DFS()) -- cgit v1.2.3