-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add list ops with array length accessor and any/all/none quantifiers #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,14 @@ func BenchmarkOptimizedEngineSimple(b *testing.B) { | |
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
@@ -37,6 +45,14 @@ func BenchmarkOptimizedEngineComplex(b *testing.B) { | |
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
@@ -55,6 +71,14 @@ func BenchmarkOptimizedEngineStringOps(b *testing.B) { | |
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
@@ -76,6 +100,15 @@ func BenchmarkOptimizedEngineInOperator(b *testing.B) { | |
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| // 1 alloc (24 B) from []any interface boxing in Go runtime. | ||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs > 1 { | ||
| b.Fatalf("expected <= 1 allocs/op, got %f", allocs) | ||
| } | ||
|
Comment on lines
+103
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enforce 0 allocs/op for the This currently tolerates allocations, which violates the benchmark requirement. Proposed fix- // 1 alloc (24 B) from []any interface boxing in Go runtime.
if allocs := testing.AllocsPerRun(1, func() {
_, _ = engine.Evaluate(rule, ctx)
- }); allocs > 1 {
- b.Fatalf("expected <= 1 allocs/op, got %f", allocs)
+ }); allocs != 0 {
+ b.Fatalf("expected 0 allocs/op, got %f", allocs)
}As per coding guidelines, "Allocation Verification: All benchmarks must show 0 allocs/op". 🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The in operator benchmark correctly allows 1 allocation (24 B). This is a pre-existing behavior — not introduced by this PR. The allocation comes from Go's runtime boxing of a []any slice header (24 bytes) into an any interface field (EvalResult.OriginalValue). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since this is a known limitation and not a regression, the 🔥 Failed to add learningsLearnings cannot be added due to server error. Please try again. |
||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
@@ -102,6 +135,14 @@ func BenchmarkOptimizedEngineNestedProps(b *testing.B) { | |
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
@@ -117,6 +158,234 @@ func BenchmarkOptimizedStandalone(b *testing.B) { | |
| ctx := D{"x": 10} | ||
| rule := "x eq 10" | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| result, err := engine.Evaluate(rule, ctx) | ||
| if err != nil || !result { | ||
| b.Fatalf("Expected true result, got %v, %v", result, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark array length evaluation for zero allocations. | ||
| func BenchmarkOptimizedEngineArrayLength(b *testing.B) { | ||
| engine := NewEngine() | ||
| ctx := D{ | ||
| "selections": []any{ | ||
| D{"id": "1"}, | ||
| D{"id": "2"}, | ||
| D{"id": "3"}, | ||
| }, | ||
| } | ||
| rule := "selections.length gt 2" | ||
|
|
||
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| result, err := engine.Evaluate(rule, ctx) | ||
| if err != nil || !result { | ||
| b.Fatalf("Expected true result, got %v, %v", result, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark any quantifier evaluation (1 alloc, 24 B from []any interface boxing). | ||
| func BenchmarkOptimizedEngineQuantifierAny(b *testing.B) { | ||
| engine := NewEngine() | ||
| ctx := D{ | ||
| "selections": []any{ | ||
| D{"event_status": "EVENT_STATUS_IN_PROGRESS", "is_live": true}, | ||
| D{"event_status": "EVENT_STATUS_CANCELLED", "is_live": false}, | ||
| D{"event_status": "EVENT_STATUS_FINISHED", "is_live": false}, | ||
| }, | ||
| } | ||
| rule := `selections any (event_status eq "EVENT_STATUS_CANCELLED")` | ||
|
|
||
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| // 1 alloc (24 B) from []any interface boxing in Go runtime. | ||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs > 1 { | ||
| b.Fatalf("expected <= 1 allocs/op, got %f", allocs) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| result, err := engine.Evaluate(rule, ctx) | ||
| if err != nil || !result { | ||
| b.Fatalf("Expected true result, got %v, %v", result, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark all quantifier evaluation (1 alloc, 24 B from []any interface boxing). | ||
| func BenchmarkOptimizedEngineQuantifierAll(b *testing.B) { | ||
| engine := NewEngine() | ||
| ctx := D{ | ||
| "items": []any{ | ||
| D{"is_valid": true}, | ||
| D{"is_valid": true}, | ||
| D{"is_valid": true}, | ||
| }, | ||
| } | ||
| rule := "items all (is_valid eq true)" | ||
|
|
||
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| // 1 alloc (24 B) from []any interface boxing in Go runtime. | ||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs > 1 { | ||
| b.Fatalf("expected <= 1 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| result, err := engine.Evaluate(rule, ctx) | ||
| if err != nil || !result { | ||
| b.Fatalf("Expected true result, got %v, %v", result, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark none quantifier evaluation (1 alloc, 24 B from []any interface boxing). | ||
| func BenchmarkOptimizedEngineQuantifierNone(b *testing.B) { | ||
| engine := NewEngine() | ||
| ctx := D{ | ||
| "items": []any{ | ||
| D{"status": "active"}, | ||
| D{"status": "pending"}, | ||
| D{"status": "completed"}, | ||
| }, | ||
| } | ||
| rule := `items none (status eq "error")` | ||
|
|
||
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| // 1 alloc (24 B) from []any interface boxing in Go runtime. | ||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs > 1 { | ||
| b.Fatalf("expected <= 1 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| result, err := engine.Evaluate(rule, ctx) | ||
| if err != nil || !result { | ||
| b.Fatalf("Expected true result, got %v, %v", result, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark complex real-world betting rule with length + quantifiers. | ||
| func BenchmarkOptimizedEngineComplexBetting(b *testing.B) { | ||
| engine := NewEngine() | ||
| ctx := D{ | ||
| "bet_type": "BET_TYPE_MULTIPLE", | ||
| "customer_data": D{ | ||
| "is_vip": true, | ||
| }, | ||
| "selections": []any{ | ||
| D{ | ||
| "event_status": "EVENT_STATUS_IN_PROGRESS", | ||
| "is_live": true, | ||
| "odd": 2.5, | ||
| "provider": "PROVIDER_SPORTRADAR", | ||
| }, | ||
| D{"event_status": "EVENT_STATUS_NOT_STARTED", "is_live": false, "odd": 1.8, "provider": "PROVIDER_RAMP"}, | ||
| D{ | ||
| "event_status": "EVENT_STATUS_IN_PROGRESS", | ||
| "is_live": true, | ||
| "odd": 3.1, | ||
| "provider": "PROVIDER_SPORTRADAR", | ||
| }, | ||
| }, | ||
| } | ||
| rule := `bet_type eq "BET_TYPE_MULTIPLE" and selections.length ge 2 and selections none (event_status eq "EVENT_STATUS_CANCELLED") and selections any (is_live eq true and odd gt 2.0)` | ||
|
|
||
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| // Multiple allocs expected from []any interface boxing (one per quantifier). | ||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs > 2 { | ||
| b.Fatalf("expected <= 2 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
| result, err := engine.Evaluate(rule, ctx) | ||
| if err != nil || !result { | ||
| b.Fatalf("Expected true result, got %v, %v", result, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Benchmark quantifier with short-circuit (first element matches). | ||
| func BenchmarkOptimizedEngineQuantifierAnyShortCircuit(b *testing.B) { | ||
| engine := NewEngine() | ||
|
|
||
| // Create array with 100 elements, first one matches | ||
| items := make([]any, 100) | ||
|
|
||
| items[0] = D{"status": "target"} | ||
| for i := 1; i < 100; i++ { | ||
| items[i] = D{"status": "other"} | ||
| } | ||
|
|
||
| ctx := D{"items": items} | ||
| rule := `items any (status eq "target")` | ||
|
|
||
| // Pre-compile rule | ||
| engine.AddQuery(rule) | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| // 1 alloc (24 B) from []any interface boxing in Go runtime. | ||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = engine.Evaluate(rule, ctx) | ||
| }); allocs > 1 { | ||
| b.Fatalf("expected <= 1 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
@@ -135,6 +404,14 @@ func BenchmarkZeroAllocEvaluatorDirect(b *testing.B) { | |
| NewNumberLiteralNode(10)) | ||
| ctx := D{"x": 10} | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| if allocs := testing.AllocsPerRun(1, func() { | ||
| _, _ = evaluator.Evaluate(ast, ctx) | ||
| }); allocs != 0 { | ||
| b.Fatalf("expected 0 allocs/op, got %f", allocs) | ||
| } | ||
|
|
||
| b.ResetTimer() | ||
|
|
||
| for range b.N { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.