Skip to content
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

Merged
merged 6 commits into from
Aug 24, 2023

Conversation

PanGan21
Copy link
Contributor

@PanGan21 PanGan21 commented Aug 9, 2023

TITLE

refactor: combine Program.hints and Program.hints_ranges into custom collection

Description

Combines Program.hints and Program.hints_ranges into a new struct called hints_collection.
Closes #1342

Description of the pull request changes and motivation.

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

Comment on lines 99 to 102
pub struct HintsCollection {
pub hints: Vec<HintParams>,
pub hints_ranges: Vec<HintRange>,
}
Copy link
Member

@Oppen Oppen Aug 9, 2023

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.

Copy link
Contributor Author

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 :)

Copy link
Member

@Oppen Oppen Aug 11, 2023

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.

Copy link
Member

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.

Copy link
Contributor Author

@PanGan21 PanGan21 Aug 12, 2023

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

Copy link
Member

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.

Copy link
Contributor Author

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!

@PanGan21 PanGan21 marked this pull request as draft August 10, 2023 17:56
@PanGan21 PanGan21 marked this pull request as ready for review August 14, 2023 22:49
@codecov
Copy link

codecov bot commented Aug 16, 2023

Codecov Report

Merging #1366 (9b716c5) into main (4d36f96) will decrease coverage by 0.01%.
The diff coverage is 98.01%.

@@            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     
Files Changed Coverage Δ
vm/src/vm/runners/cairo_runner.rs 97.91% <92.30%> (-0.03%) ⬇️
vm/src/types/program.rs 99.44% <98.66%> (-0.10%) ⬇️
vm/src/serde/deserialize_program.rs 97.32% <100.00%> (-0.02%) ⬇️
vm/src/utils.rs 99.49% <100.00%> (-0.01%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@MegaRedHand MegaRedHand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Oppen Oppen added this pull request to the merge queue Aug 24, 2023
Merged via the queue into lambdaclass:main with commit 16abcb4 Aug 24, 2023
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor: combine Program.hints and Program.hints_ranges into a custom collection
3 participants