Skip to content

Conversation

FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Oct 15, 2025

Compile hints without unnecessary references

Description

Currently, when compiling a hint, all accessible references are taken into account. This means, there may be references which are computed end stored in HintProcessorData but actually are not used by the hint at all.
For every hint that gets compiled, the function get_ids_data gets called. This function's code is as follows:

fn get_ids_data(
    reference_ids: &HashMap<String, usize>,
    references: &[HintReference],
) -> Result<HashMap<String, HintReference>, VirtualMachineError> {
    let mut ids_data = HashMap::<String, HintReference>::new();
    for (path, ref_id) in reference_ids {
        let name = path
            .rsplit('.')
            .next()
            .ok_or(VirtualMachineError::Unexpected)?;
        ids_data.insert(
            name.to_string(),
            references
                .get(*ref_id)
                .ok_or(VirtualMachineError::Unexpected)?
                .clone(),
        );
    }
    Ok(ids_data)
}

Depending on the hint, and the context in which it gets called, there may be quite a lot of references which will be iterated in this function without any reason at all. To avoid having to compute unnecessary references, this PR changes how these references are computed for some hints. Basically, for those cases where the hint code does not make use of any reference, it will not call get_ids_data and simply assign an empty HashMap. This may avoid unnecessary overhead.

Benchmarks

Run some ranges of blocks comparing cairo vm 2.5.0 with this PR. These numbers correspond to the first run of each range. Running them a second time tends to lower the improvement, sometime to levels which are indistinguishable between the two versions. This could probably be due to caching.

20000-20010: 3% improvement.
2000000: 1% improvement.

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.

Copy link

github-actions bot commented Oct 15, 2025

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.122 ± 0.038 2.089 2.221 1.00 ± 0.02
head big_factorial 2.117 ± 0.020 2.101 2.168 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.050 ± 0.025 2.027 2.102 1.00 ± 0.01
head big_fibonacci 2.048 ± 0.007 2.038 2.059 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 7.656 ± 0.124 7.551 7.997 1.00 ± 0.02
head blake2s_integration_benchmark 7.650 ± 0.079 7.583 7.782 1.00
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.184 ± 0.020 2.157 2.210 1.00
head compare_arrays_200000 2.205 ± 0.020 2.188 2.256 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.433 ± 0.015 1.416 1.468 1.00
head dict_integration_benchmark 1.440 ± 0.012 1.426 1.471 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.233 ± 0.015 1.217 1.265 1.01 ± 0.01
head field_arithmetic_get_square_benchmark 1.226 ± 0.007 1.216 1.233 1.00
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 7.792 ± 0.037 7.732 7.851 1.00
head integration_builtins 7.806 ± 0.075 7.736 7.923 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 8.069 ± 0.122 7.931 8.236 1.00
head keccak_integration_benchmark 8.126 ± 0.192 7.922 8.483 1.01 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.179 ± 0.008 2.166 2.191 1.00
head linear_search 2.185 ± 0.005 2.174 2.192 1.00 ± 0.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.518 ± 0.019 1.503 1.568 1.00 ± 0.01
head math_cmp_and_pow_integration_benchmark 1.512 ± 0.004 1.507 1.522 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.466 ± 0.007 1.457 1.479 1.00 ± 0.01
head math_integration_benchmark 1.466 ± 0.007 1.458 1.478 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.233 ± 0.014 1.215 1.253 1.00 ± 0.02
head memory_integration_benchmark 1.229 ± 0.016 1.218 1.275 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.570 ± 0.007 1.560 1.582 1.00
head operations_with_data_structures_benchmarks 1.573 ± 0.011 1.560 1.594 1.00 ± 0.01
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 529.0 ± 2.6 526.3 535.5 1.00
head pedersen 530.8 ± 2.2 527.7 535.1 1.00 ± 0.01
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 635.4 ± 1.6 633.3 637.5 1.00
head poseidon_integration_benchmark 638.7 ± 3.6 630.5 643.9 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.856 ± 0.030 1.835 1.936 1.02 ± 0.02
head secp_integration_benchmark 1.825 ± 0.012 1.809 1.843 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 632.8 ± 1.8 629.8 635.0 1.03 ± 0.01
head set_integration_benchmark 611.7 ± 3.6 606.6 619.4 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.307 ± 0.119 4.211 4.589 1.01 ± 0.03
head uint256_integration_benchmark 4.250 ± 0.040 4.214 4.334 1.00

Copy link

codecov bot commented Oct 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.66%. Comparing base (065c8f4) to head (6be90a4).

Additional details and impacted files
@@           Coverage Diff           @@
##            2.x.y    #2231   +/-   ##
=======================================
  Coverage   96.66%   96.66%           
=======================================
  Files         103      103           
  Lines       43646    43688   +42     
=======================================
+ Hits        42191    42233   +42     
  Misses       1455     1455           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@FrancoGiachetta FrancoGiachetta force-pushed the compile-hints-without-unnecessary-references branch from 390be61 to accc076 Compare October 16, 2025 15:45
@FrancoGiachetta FrancoGiachetta force-pushed the compile-hints-without-unnecessary-references branch from 7fb5eb8 to cd709fd Compare October 16, 2025 17:56
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.

1 participant