Skip to content

Share the standard declarations scope across type checks - #1512

Open
mpenet wants to merge 3 commits into
cel-expr:mainfrom
mpenet:checker-shared-standard-scope
Open

mpenet wants to merge 3 commits into
cel-expr:mainfrom
mpenet:checker-shared-standard-scope

Conversation

@mpenet

@mpenet mpenet commented Nov 25, 2026 •

Copy link
Copy Markdown

Every call to CelCheckerLegacyImpl.check() builds a new Env through Env.standard(...), which adds all the standard functions and identifiers again, then the checker's own declarations. addOverload compares each overload with the existing ones of the same function, so this ends up being most of the time spent checking a small expression. It adds up when compiling many expressions at runtime, like rules or policies, especially when each one gets its own variables (toCelBuilder().addVarDeclarations(...).build(), then compile).

Numbers on an M1 Pro with JDK 25 (criterium, allocations from ThreadMXBean), checking true, and deriving a checker with one variable then checking an expression with it:

before after
check, standardCelBuilder() 199 µs, 336 KB 0.29 µs, 2.5 KB
check, standardCelBuilder() with CelOptionalLibrary 212 µs, 369 KB 0.31 µs, 2.5 KB
check, standard environment disabled, for reference 0.32 µs, 2.5 KB 0.30 µs, 2.4 KB
derive a checker (toCelBuilder().addVar(...).build()), then check 203 µs, 341 KB 37 µs, 68 KB

benchmark: https://gist.github.com/mpenet/bb2dbb26df20ac1f01b4ec31aa73ee39

Changes:

  • CelStandardDeclarations indexes its function and identifier declarations by name when it's built. They come from the standard enums, so names are unique and there's no need to go through Env.add. Env.standard(...) sets up the standard scope from these maps directly, which also covers checkers derived with toCheckerBuilder() and subsets set with setStandardDeclarations.
  • DeclGroup is copy-on-write, so environments can share these immutable maps. tryLookupCelIdent still caches identifiers resolved through the type provider into the root scope, the first write copies the map for that Env only.
  • CelCheckerLegacyImpl adds its identifier and function declarations once, when built, and every check starts from the resulting declarations. If adding them reports errors (e.g. overlapping overloads), they're added on every check as before so the errors are still reported.

Scope depth, ROOT_SCOPE and what accept(EnvVisitor) sees don't change.

Tests cover the indexed declarations matching what Env.add produces (all, included functions, filtered overloads, excluded identifiers), root scope writes not being visible to other environments, types resolved by one checker not being visible from another one, overlapping user declarations being reported on every check, the timestamp epoch option still applying, overridden standard declarations only declaring what was included, and shadowing standard functions.

The checker, bundle, compiler, parser, runtime, optimizer, extensions, policy, validator and conformance tests pass locally (macOS, so I couldn't run the Android targets), and the checker targets build with --java_language_version=8.

@mpenet
mpenet force-pushed the checker-shared-standard-scope branch 2 times, most recently from 435e59b to c19cf84 Compare November 25, 2026 13:07
@mpenet
mpenet marked this pull request as ready for review November 25, 2026 13:07
* Standard declarations derived from {@link CelOptions}, keyed by {@link
* #standardDeclarationsKey}.
*/
private static final ConcurrentMap<Integer, CelStandardDeclarations> STANDARD_DECLARATIONS =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR. Type checker rebuilding all declarations on every check is definitely an undesired behavior that should be fixed.

I don't think we'd want to introduce a global cache, nor is it needed to solve this issue. I would experiment with a stateless approach where the same pre-built declarations from CelCheckerLegacyImpl is passed through the internal Env.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the review; that makes sense. I'll drop the static caches and build the declarations once per CelCheckerLegacyImpl, then pass them to the Env on each check.

One case I'd like to keep covered: deriving a checker per expression with toCheckerBuilder() (or toCelBuilder()) just to add a few variable declarations. With the declarations built per checker, every derived checker would build them again, which is the cost we're trying to get rid of in that case. Would you be ok with toCheckerBuilder() handing the prebuilt declarations to the new builder, and build() reusing them as long as the options and standard declarations weren't changed on it? Otherwise I can keep it strictly per instance and leave that part out.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I removed the static caches: the declarations are now built once per CelCheckerLegacyImpl, on first check, and passed to the Env on each check. I left out the checker deriving for now, waiting on your feedback for this part.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is better, though I would also avoid caching/invalidation in toCheckerBuilder(). The builders being fully mutable is the core concern here—invalidation conditions are easy to miss, and extension libraries can mutate the builder inside build(). Instead, I would experiment with an approach like the following:

  1. Index CelStandardDeclarations directly instead of running Env.standard / env::add (this is where the bulk of baseline cost comes from). CelStandardDeclarations is a closed immutable type built solely from validated standard enums. We can then declare a constant for it (e.g., static final CelStandardDeclarations ALL = newBuilder().build()). That makes standard declaration setup O(1) for both independently built checkers and toCheckerBuilder()—and subsetted CelStandardDeclarations are already pre-indexed too, so toCheckerBuilder() reuses them in O(1) as well.
  2. Replace DeclGroup layering with copy-on-write and prebuild user declarations. Concretely, you should be able to change Env's DeclGroup, CelStandardDeclarations and CelCheckerLegacyImpl to propagate the same ImmutableMap<String, Var/Function Decl>.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is a much better design than what I suggested earlier. Thanks for the hints.

CelCheckerLegacyImpl rebuilt the checker Env on every check, adding every
standard function and identifier declaration again, along with the overlap
checks this involves. This dominated the cost of checking small expressions.

Standard declarations are now added once into an immutable DeclGroup, cached
by the identity of the CelStandardDeclarations they hold. The declarations
derived from CelOptions are cached by the options they depend on, so every
checker with the same configuration shares the same scope. Each Env layers a
mutable root group on top of it, keeping identifiers resolved through the
type provider local to the Env as before.
Replace the static caches with a scope owned by each CelCheckerLegacyImpl,
built on first use and passed to the Env on every check. The scope is built
through Env.standard with the checker's type providers and options, then
copied into an immutable DeclGroup.
@mpenet
mpenet force-pushed the checker-shared-standard-scope branch from b6610a6 to 74bae63 Compare November 30, 2026 21:09

// Built on first use and only read afterwards, see Env.newStandardScope.
@SuppressWarnings("Immutable")
private final Supplier<Env.@Nullable DeclGroup> standardScope;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

CelImpl already wraps the compiler in Suppliers.memoize. I don't think we want to introduce a double memoization here.

* Standard declarations derived from {@link CelOptions}, keyed by {@link
* #standardDeclarationsKey}.
*/
private static final ConcurrentMap<Integer, CelStandardDeclarations> STANDARD_DECLARATIONS =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is better, though I would also avoid caching/invalidation in toCheckerBuilder(). The builders being fully mutable is the core concern here—invalidation conditions are easy to miss, and extension libraries can mutate the builder inside build(). Instead, I would experiment with an approach like the following:

  1. Index CelStandardDeclarations directly instead of running Env.standard / env::add (this is where the bulk of baseline cost comes from). CelStandardDeclarations is a closed immutable type built solely from validated standard enums. We can then declare a constant for it (e.g., static final CelStandardDeclarations ALL = newBuilder().build()). That makes standard declaration setup O(1) for both independently built checkers and toCheckerBuilder()—and subsetted CelStandardDeclarations are already pre-indexed too, so toCheckerBuilder() reuses them in O(1) as well.
  2. Replace DeclGroup layering with copy-on-write and prebuild user declarations. Concretely, you should be able to change Env's DeclGroup, CelStandardDeclarations and CelCheckerLegacyImpl to propagate the same ImmutableMap<String, Var/Function Decl>.

CelStandardDeclarations indexes its declarations by name when built, so
Env.standard sets up the standard scope from them without adding each
declaration again. DeclGroup is copy-on-write, letting environments share
these immutable maps.

CelCheckerLegacyImpl adds its identifier and function declarations once,
when built, and every check starts from the resulting declarations. If
adding them reports errors, they are added on every check as before so the
errors are reported.

This replaces the per-checker standard scope and the layered DeclGroup.
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.

2 participants