Skip to content

Program plan optimizations - #1399

Merged
TristonianJones merged 3 commits into
cel-expr:masterfrom
TristonianJones:dispatcher-reuse
Aug 5, 2026
Merged

Program plan optimizations#1399
TristonianJones merged 3 commits into
cel-expr:masterfrom
TristonianJones:dispatcher-reuse

Conversation

@TristonianJones

@TristonianJones TristonianJones commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

A significant amount of overhead comes from the instantiation of the dispatcher
with an immutable set of functions. Instead of recreating the dispatcher, create
a shared one across program instances.

Additionally, check whether optimizations are needed for an expression before
applying them as decorators.

Benchmark Case Before (ns/op) After (ns/op) Δ Time Before (B/op) After (B/op) Δ Memory Before (allocs) After (allocs) Δ Allocs
BenchmarkProgramPlan/Default 8,153 930 -88.6% 8,320 1,416 -83.0% 36 26 -27.8%
BenchmarkProgramPlan/OptimizeUnneeded 7,344 1,150 -84.3% 8,784 1,512 -82.8% 50 32 -36.0%
BenchmarkProgramPlan/OptimizeNeeded 8,370 2,164 -74.1% 10,224 2,976 -70.9% 67 52 -22.4%

Addresses the dispatcher-sharing proposed in #1397, which is the first half of the optimizations suggested to minimize startup costs.

| Benchmark Case | Before (ns/op) | After (ns/op) | Δ Time | Before (B/op) | After (B/op) | Δ Memory | Before (allocs) | After (allocs) | Δ Allocs |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| BenchmarkProgramPlan/Default | 8,153 | 930 | **-88.6%** | 8,320 | 1,416 | **-83.0%** | 36 | 26 | **-27.8%** |
| BenchmarkProgramPlan/OptimizeUnneeded | 7,344 | 1,150 | **-84.3%** | 8,784 | 1,512 | **-82.8%** | 50 | 32 | **-36.0%** |
| BenchmarkProgramPlan/OptimizeNeeded | 8,370 | 2,164 | **-74.1%** | 10,224 | 2,976 | **-70.9%** | 67 | 52 | **-22.4%** |
@TristonianJones

Copy link
Copy Markdown
Collaborator Author

@jpbetz FYI

@jpbetz jpbetz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this looks like a cleaner/simpler approach than I had considered. Thanks!

@TristonianJones

TristonianJones commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator Author

@jpbetz I went ahead and folded in more of the changes from your proposed PR. The numbers I see are close to what you observed:

pkg: github.com/google/cel-go/cel
cpu: Apple M1 Pro
BenchmarkEnvExtendK8sShape-10             	   30596	     38461 ns/op	   23332 B/op	     278 allocs/op
BenchmarkProgramK8sShape-10               	 1579915	       755.6 ns/op	    1144 B/op	      20 allocs/op
BenchmarkProgramFirstPerEnvK8sShape-10    	 1000000	      1440 ns/op	    1144 B/op	      20 allocs/op

There's two more changes to make to reduce the EnvExtend cost further, but then I think we'll have captured everything.

@jpbetz

jpbetz commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Awesome. This LGTM

@TristonianJones
TristonianJones merged commit 9d5baaf into cel-expr:master Aug 5, 2026
8 checks passed
@TristonianJones
TristonianJones deleted the dispatcher-reuse branch August 5, 2026 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants