feat(hoist): pre-compute loop hoisting metadata at compilation time - #483
Merged
Conversation
anakrish
force-pushed
the
ast-enhanced
branch
3 times, most recently
from
October 6, 2025 18:14
7393fc8 to
be57589
Compare
Introduce a compiler pass that analyzes and pre-computes loop hoisting information during policy compilation. This hoisted metadata is stored in lookup tables and made available to downstream consumers: - interpreter: use HoistedLoop entries during evaluation (replaces runtime scanning) - type inference: can leverage pre-computed loop structure for type propagation - RVM compiler: will consume hoisting metadata for optimized bytecode generation Changes: - populate loop hoisting tables during engine preparation and query snippet execution - refactor eval_stmts_in_loop and eval_output_expr_in_loop to consume HoistedLoop directly - add helper methods for accessing loop expressions, collections, and indices from HoistedLoop - extend Lookup with get_checked and into_slots for safe query context access and merging Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a compilation-time loop hoisting optimization that pre-computes loop metadata during policy preparation, storing it in lookup tables for efficient runtime evaluation. This replaces the previous runtime loop scanning approach with cached results that can be reused across multiple evaluations.
- Adds a new compiler module with loop hoisting and scope context functionality
- Refactors interpreter loop handling to consume pre-computed HoistedLoop structures
- Extends the Lookup utility with bounds checking and module management capabilities
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/compiler/hoist.rs | Implements the main LoopHoister that pre-computes loop metadata for statements and expressions |
| src/compiler/context.rs | Defines ScopeContext for tracking variable bindings during compilation |
| src/interpreter.rs | Updates loop evaluation to use pre-computed HoistedLoop data instead of runtime analysis |
| src/engine.rs | Integrates loop hoisting into policy preparation and query snippet execution |
| src/lookup.rs | Adds safe bounds checking and module management methods |
| src/compiled_policy.rs | Stores the loop hoisting table in compiled policy data |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
|
||
| #[inline] | ||
| fn loop_index_expr(loop_info: &HoistedLoop) -> Option<&ExprRef> { | ||
| loop_info.key.as_ref().or(None) |
There was a problem hiding this comment.
The .or(None) is redundant here. Option::as_ref() already returns an Option, so this can be simplified to just loop_info.key.as_ref().
Suggested change
| loop_info.key.as_ref().or(None) | |
| loop_info.key.as_ref() |
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.
Introduce a compiler pass that analyzes and pre-computes loop hoisting information during policy compilation. This hoisted metadata is stored in lookup tables and made available to downstream consumers:
Changes: