Repository navigation
Conversation
mpenet
force-pushed
the
checker-shared-standard-scope
branch
from
November 25, 2026 13:05
7816ef1 to
435e59b
Compare
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.
mpenet
force-pushed
the
checker-shared-standard-scope
branch
from
November 25, 2026 13:07
435e59b to
c19cf84
Compare
mpenet
marked this pull request as ready for review
November 25, 2026 13:07
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every call to
CelCheckerLegacyImpl.check()builds a newEnvthroughEnv.standard(...), which adds all the standard functions and identifiers again.addOverloadcompares 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(), thencompile).Numbers for checking
true, on an M1 Pro with JDK 25 (criterium, allocations fromThreadMXBean):standardCelBuilder()standardCelBuilder()withCelOptionalLibrarybenchmark: https://gist.github.com/mpenet/bb2dbb26df20ac1f01b4ec31aa73ee39
Building the
CelStandardDeclarationsis cheap (around 2.5 µs), the time goes into adding them to theEnv, so passing prebuilt declarations withsetStandardDeclarationsdoesn't help.The standard declarations already sit in their own bottom scope, so this change builds that scope once into an immutable
DeclGroupand reuses it:Env.standardScope(CelStandardDeclarations)returns the scope for a set of declarations, cached by the identity of the declarations object (weak keys, so it goes away with them). If adding the declarations reports errors it returns null, and the checker falls back to the current per-check path so errors are reported the same way.Env.standardDeclarations(CelOptions)caches the declarations derived from the options, keyed by the two optionsnewStandardDeclarationsreads, so every checker with the same configuration gets the same declarations and therefore the same scope, however it was built.Env.fromStandardScopecreates anEnvon top of the scope. Its root group is layered over the shared one, sincetryLookupCelIdentcaches identifiers resolved through the type provider into the root scope. Those stay local to theEnv, otherwise two checkers sharing the scope with different type providers would see each other's types.CelCheckerLegacyImplonly changes ingetEnv, plus a package-private accessor used by the tests. Scope depth,ROOT_SCOPEand whataccept(EnvVisitor)sees don't change.The new tests in
CelCheckerLegacyImplTestcover sharing between independently built and derived checkers, sharing when unrelated options change, the timestamp epoch option still applying, overridden declarations (sharing and only declaring what was included), immutability of the shared scope, declarations not leaking between checkers, shadowing standard functions, and types resolved by one checker not being visible from another one sharing the scope.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.A couple of things that could be follow-ups if you think they're worth it:
CelEnvironmentbuilds newCelStandardDeclarationsfor a standard library subset on everyextend, so those don't share the scope. Sharing them would need equality onCelStandardDeclarations.CelOptionalLibrary.