-
Notifications
You must be signed in to change notification settings - Fork 12
feat(expr)!: bounded range values, budgeted range equality, exact int/float comparison #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mwiebe
merged 3 commits into
OpenJobDescription:main
from
mwiebe:fix/expr-symbolic-ranges-equality
Jul 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // Copyright by contributors to this project. | ||
| // SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
|
||
| //! Memory-budgeted collection of expression values. | ||
|
|
||
| use crate::error::ExpressionError; | ||
| use crate::function_library::EvalContext; | ||
| use crate::value::ExprValue; | ||
|
|
||
| /// A `Vec<ExprValue>` that checks its projected heap footprint before growth. | ||
| pub(crate) struct BudgetedVec { | ||
| values: Vec<ExprValue>, | ||
| value_bytes: usize, | ||
| } | ||
|
|
||
| impl BudgetedVec { | ||
| /// Create an empty dynamically growing vector. | ||
| pub(crate) fn new() -> Self { | ||
| Self { | ||
| values: Vec::new(), | ||
| value_bytes: 0, | ||
| } | ||
| } | ||
|
|
||
| /// Create a vector with an exact, pre-checked capacity. | ||
| pub(crate) fn with_capacity( | ||
| ctx: &mut dyn EvalContext, | ||
| capacity: usize, | ||
| ) -> Result<Self, ExpressionError> { | ||
| ctx.check_memory(capacity.saturating_mul(size_of::<ExprValue>()))?; | ||
| Ok(Self { | ||
| values: Vec::with_capacity(capacity), | ||
| value_bytes: 0, | ||
| }) | ||
| } | ||
|
|
||
| /// Push a value after checking its bytes and the projected capacity slack. | ||
| pub(crate) fn push( | ||
| &mut self, | ||
| ctx: &mut dyn EvalContext, | ||
| value: ExprValue, | ||
| ) -> Result<(), ExpressionError> { | ||
| let value_bytes = self.value_bytes.saturating_add(value.memory_size()); | ||
| // Vec growth doubles a full allocation, with a minimum non-zero | ||
| // capacity of four for ExprValue-sized elements. | ||
| let projected_capacity = if self.values.len() == self.values.capacity() { | ||
| self.values.capacity().saturating_mul(2).max(4) | ||
| } else { | ||
| self.values.capacity() | ||
| }; | ||
| let slack = projected_capacity.saturating_sub(self.values.len() + 1); | ||
| let projected_bytes = | ||
| value_bytes.saturating_add(slack.saturating_mul(size_of::<ExprValue>())); | ||
| ctx.check_memory(projected_bytes)?; | ||
|
|
||
| self.values.push(value); | ||
| self.value_bytes = value_bytes; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Consume the wrapper and return the collected values. | ||
| pub(crate) fn into_vec(self) -> Vec<ExprValue> { | ||
| self.values | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::path_mapping::PathFormat; | ||
| use std::cell::RefCell; | ||
|
|
||
| #[derive(Default)] | ||
| struct TestContext { | ||
| checks: RefCell<Vec<usize>>, | ||
| } | ||
|
|
||
| impl EvalContext for TestContext { | ||
| fn path_format(&self) -> PathFormat { | ||
| PathFormat::host() | ||
| } | ||
|
|
||
| fn count_op(&mut self) -> Result<(), ExpressionError> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn count_ops(&mut self, _n: usize) -> Result<(), ExpressionError> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn count_string_ops(&mut self, _len: usize) -> Result<(), ExpressionError> { | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn check_memory(&self, bytes: usize) -> Result<(), ExpressionError> { | ||
| self.checks.borrow_mut().push(bytes); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn checks_exact_capacity_before_allocation() { | ||
| let mut ctx = TestContext::default(); | ||
| let values = BudgetedVec::with_capacity(&mut ctx, 3).unwrap(); | ||
|
|
||
| assert_eq!(values.values.capacity(), 3); | ||
| assert_eq!(*ctx.checks.borrow(), vec![3 * size_of::<ExprValue>()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn push_checks_projected_doubling_and_value_heap() { | ||
| let mut ctx = TestContext::default(); | ||
| let mut values = BudgetedVec::new(); | ||
| values | ||
| .push(&mut ctx, ExprValue::String("abc".to_owned())) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(*ctx.checks.borrow(), vec![4 * size_of::<ExprValue>() + 3]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn push_checks_doubled_capacity_before_fifth_value() { | ||
| let mut ctx = TestContext::default(); | ||
| let mut values = BudgetedVec::new(); | ||
| for value in 0..5 { | ||
| values.push(&mut ctx, ExprValue::Int(value)).unwrap(); | ||
| } | ||
|
|
||
| assert_eq!( | ||
| *ctx.checks.borrow(), | ||
| vec![ | ||
| 4 * size_of::<ExprValue>(), | ||
| 4 * size_of::<ExprValue>(), | ||
| 4 * size_of::<ExprValue>(), | ||
| 4 * size_of::<ExprValue>(), | ||
| 8 * size_of::<ExprValue>(), | ||
| ] | ||
| ); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maintainability data point: with this change,
eval_listcompis now 182 lines (145 code, 32 comment) with a cyclomatic complexity of ~40 (measured by decision-point count:if/while/for/match arms/&&/||/?). Even discounting the?early-returns as low-cognitive-load, that's well above clippy's cognitive-complexity default of 25 — this function is getting hard to maintain.The comments documenting the memory-accounting invariants help a lot, but the budget bookkeeping (baseline save/restore,
result_bytes, the capacity-slack projection beforepush) is interleaved with the iteration logic — and the same projected-capacity pre-check pattern now appears in three places (here,slice_rangein comparison.rs, andlist_from_rangein list.rs).Suggested follow-up (non-blocking): extract a small
BudgetedVec-style helper that owns the result-bytes tracking and post-doubling capacity projection. It would shrink this function by ~30 lines, deduplicate the three sites, and put the trickiest invariant behind one independently-testable abstraction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up 5bb85b7 created the suggested budgeted vec, centralizing some of this tricky accounting from a bunch of places.