Memoize SafetyEvaluator type evaluations - #2976
Open
helenyugithub wants to merge 3 commits into
Open
Conversation
SafetyEvaluator re-evaluates the transitive safety of a referenced type at every reference site, so evaluation cost is proportional to the number of paths through the type graph rather than its size. On large, dense conjure definitions this dominates java codegen: a real-world 3.2MB IR spends ~350s in objects generation, with all sampled stacks inside SafetyEvaluator; the same IR generates in ~35s with this cache, with byte-identical output. Results are memoized per TypeName, respecting the recursive-type cycle guard: values computed after hitting the in-progress set are substituted with SAFE and therefore context-dependent, so only cycle-free subtree results and outermost (canonical) evaluations enter the cache.
Generate changelog in
|
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.
Before this PR
SafetyEvaluatorre-evaluates the transitive log-safety of a referenced type at every reference site:FieldSafetyVisitor#visitReferencedescends into the referenced type's full definition each time, so evaluation cost is proportional to the number of paths through the type graph rather than the number of types. On large, dense conjure definitions this dominates codegen wall-clock.Measured on real-world IRs (conjure-java 8.70.0, objects flavor, single run each):
During the slow runs, 20/20 thread-dump samples were inside the
SafetyEvaluatorrecursion. Generated output with the cache is byte-identical on both IRs (318/318 and 2371/2371 files).After this PR
Safety results are memoized per
TypeNamein a cache scoped to theSafetyEvaluatorinstance, shared across the visitor instances it creates. The cache respects the recursive-type cycle guard: values computed after hitting theinProgressset embed the SAFE cycle substitution and are context-dependent, so only results computed from a cycle-free subtree — or at the outermost evaluation of a type, which is canonical by definition — are cached.Two tests added: repeated evaluations on a shared evaluator agree with fresh evaluators, and a
Foo ↔ Barcycle where onlyFoocarries unsafe data verifies thatBar's within-cycle value (SAFE by substitution) is not reused for its standalone evaluation (UNSAFE).Possible downsides?
The cache retains one entry per named type for the lifetime of the
SafetyEvaluator— negligible next to theConjureDefinitionit already holds.SafetyEvaluatorwas not thread-safe before this change (callers construct per-generation instances) and remains so.