-
Notifications
You must be signed in to change notification settings - Fork 160
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
refactor: combine Program.hints and Program.hints_ranges into custom collection #1366
Conversation
vm/src/types/program.rs
Outdated
pub struct HintsCollection { | ||
pub hints: Vec<HintParams>, | ||
pub hints_ranges: Vec<HintRange>, | ||
} |
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.
I'd suggest the following:
- The fields to be private;
- The struct to be pub(crate);
- Use a constructor taking a HashMap;
- Use a getter taking a pc and returning a slice.
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.
Hi! Thanks for the suggestions! Just a short question.
The getter you are describing works fine for this case:
let hint_data = self
.program
.shared_program_data
.hints_ranges
.get(vm.run_context.pc.offset)
.and_then(|r| r.and_then(|(s, l)| hint_data.get(s..s + l.get())))
.unwrap_or(&[]);
But cases like this:
self.program
.shared_program_data
.hints_collection
.hints
.iter()
.map(|hint| {
hint_executor
.compile_hint(
&hint.code,
&hint.flow_tracking_data.ap_tracking,
&hint.flow_tracking_data.reference_ids,
references,
)
.map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into()))
})
.collect()
It seems a getter that returns all the hints is required. Let me know if I am wrong :)
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.
You can make a method that returns the iterator instead:
fn iter(&self) -> impl Iterator<Item = Option<(pc, &[HintData])>> {
self.hint_ranges.iter().enumerate().filter_map(|(pc, range)| {
let Some(range) = range else {
return None;
}
Some((pc, self.hint_data[range.start..range.start+range.len]))
})
}
Consider this pseudo-code, you'll probably need to make some changes to it.
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.
You could also implement some fn iter_hints(&self) -> impl Iterator<Item = &HintData>
if it makes this easier.
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.
Unfortunatelly I was forced to implement both iter_hints
and iter
. The iter
function is used only for testing in the helper get_hints_as_map
thus it has #[allow(dead_code)]
. Let me know your thoughts
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.
You can avoid the #[allow(dead_code)]
by using a separate impl
block that is gated by the #[cfg(test)]
annotation.
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.
Added the new impl block! Thanks!
…mments, add test impl for hints collection
Codecov Report
@@ Coverage Diff @@
## main #1366 +/- ##
==========================================
- Coverage 97.49% 97.48% -0.01%
==========================================
Files 93 93
Lines 37924 37960 +36
==========================================
+ Hits 36973 37007 +34
- Misses 951 953 +2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
LGTM
TITLE
refactor: combine Program.hints and Program.hints_ranges into custom collection
Description
Combines
Program.hints
andProgram.hints_ranges
into a new struct calledhints_collection
.Closes #1342
Description of the pull request changes and motivation.
Checklist