Skip to content

perf: avoid heap allocs reading Blake2s opcode operands - #2397

Open
orizi wants to merge 1 commit into
claude/vm-execution-performance-32hi0cfrom
orizi/09-09-perf_avoid_heap_allocs_reading_blake2s_opcode_operands
Open

orizi wants to merge 1 commit into
claude/vm-execution-performance-32hi0cfrom
orizi/09-09-perf_avoid_heap_allocs_reading_blake2s_opcode_operands

Conversation

@orizi

@orizi orizi commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Read the Blake2s state and message via a new get_u32_array (const-generic stack array) instead of get_u32_range's Vec — two heap allocations per Blake instruction removed.

BREAKING: the Blake2sInvalidOperand error variant was unreachable (the vec always had the requested size on success) and is removed.

Based on #2391.

🤖 Generated with Claude Code

Performance

Expected: removes two heap allocations (a Vec<u32> of 8 and of 16) per Blake2s opcode instruction — relevant only to Stwo-style programs using the Blake opcode, roughly proportional to their Blake instruction density.

Measured (best-of-7 vs parent #2391 branch, local; noise ±2.4%): the standard benchmark suite contains no Blake-opcode programs, so the direct effect is not exercised. The suite nonetheless showed a consistent −1% to −9% (all 12 benchmarks negative, e.g. big_factorial −8.8%, big_fibonacci −7.3%); since the touched code is unreachable in these programs, that is most plausibly a code-layout/alignment side effect of the error-variant removal rather than an attributable algorithmic gain — treat the direct win as unmeasured pending a Blake-opcode workload.
Methodology: cairo_programs/benchmarks/*.cairo compiled with cairo-lang 0.14 --proof_mode, run via cairo-vm-cli <prog>.json --layout all_cairo --proof_mode, hyperfine best-of-7 per binary, local x86-64 Linux. Noise floor (identical-code control pair): ±2.4%.


This change is Reviewable

@orizi
orizi force-pushed the orizi/09-09-perf_avoid_heap_allocs_reading_blake2s_opcode_operands branch from ea80396 to 7f03670 Compare September 9, 2026 08:39
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 1.957 ± 0.018 1.944 2.000 1.05 ± 0.02
head big_factorial 1.867 ± 0.027 1.849 1.941 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 1.859 ± 0.021 1.848 1.917 1.05 ± 0.01
head big_fibonacci 1.765 ± 0.010 1.750 1.778 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 6.031 ± 0.236 5.857 6.611 1.05 ± 0.04
head blake2s_integration_benchmark 5.750 ± 0.085 5.682 5.973 1.00
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 1.921 ± 0.005 1.911 1.927 1.05 ± 0.01
head compare_arrays_200000 1.838 ± 0.009 1.827 1.850 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.352 ± 0.005 1.345 1.359 1.03 ± 0.01
head dict_integration_benchmark 1.317 ± 0.018 1.301 1.363 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.070 ± 0.002 1.067 1.075 1.02 ± 0.01
head field_arithmetic_get_square_benchmark 1.047 ± 0.005 1.041 1.058 1.00
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 6.049 ± 0.086 6.001 6.288 1.04 ± 0.02
head integration_builtins 5.844 ± 0.035 5.806 5.892 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 6.047 ± 0.028 6.017 6.120 1.02 ± 0.01
head keccak_integration_benchmark 5.926 ± 0.021 5.897 5.950 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 1.922 ± 0.012 1.914 1.953 1.04 ± 0.01
head linear_search 1.849 ± 0.011 1.833 1.875 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.418 ± 0.005 1.414 1.429 1.03 ± 0.01
head math_cmp_and_pow_integration_benchmark 1.371 ± 0.006 1.364 1.382 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.390 ± 0.013 1.384 1.426 1.04 ± 0.01
head math_integration_benchmark 1.339 ± 0.007 1.329 1.348 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.140 ± 0.002 1.137 1.143 1.04 ± 0.01
head memory_integration_benchmark 1.098 ± 0.006 1.089 1.107 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.443 ± 0.008 1.433 1.457 1.03 ± 0.01
head operations_with_data_structures_benchmarks 1.408 ± 0.009 1.395 1.425 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 500.6 ± 1.2 498.8 502.9 1.01 ± 0.01
head pedersen 493.3 ± 2.3 491.2 497.5 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 597.2 ± 2.2 593.1 599.7 1.03 ± 0.01
head poseidon_integration_benchmark 579.6 ± 3.6 575.6 589.1 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.718 ± 0.019 1.707 1.769 1.02 ± 0.01
head secp_integration_benchmark 1.681 ± 0.010 1.670 1.704 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 652.6 ± 1.1 650.8 653.9 1.02 ± 0.00
head set_integration_benchmark 638.4 ± 1.9 636.8 642.3 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 3.621 ± 0.044 3.590 3.720 1.03 ± 0.01
head uint256_integration_benchmark 3.500 ± 0.027 3.477 3.563 1.00

@orizi
orizi force-pushed the orizi/09-09-perf_avoid_heap_allocs_reading_blake2s_opcode_operands branch 2 times, most recently from 2ecfcfa to cbd96ff Compare September 9, 2026 10:06
Read the state and message via a new get_u32_array (const-generic stack
array) instead of get_u32_range's Vec. The Blake2sInvalidOperand error
was unreachable (the vec always had the requested size on success) and
is removed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@orizi
orizi force-pushed the orizi/09-09-perf_avoid_heap_allocs_reading_blake2s_opcode_operands branch from cbd96ff to 2c43ce0 Compare September 9, 2026 10:17
@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.23%. Comparing base (c610d41) to head (2c43ce0).

Additional details and impacted files
@@                           Coverage Diff                           @@
##           claude/vm-execution-performance-32hi0c    #2397   +/-   ##
=======================================================================
  Coverage                                   96.23%   96.23%           
=======================================================================
  Files                                         107      107           
  Lines                                       37985    37990    +5     
=======================================================================
+ Hits                                        36555    36560    +5     
  Misses                                       1430     1430           

☔ View full report in Codecov by Harness.
📢 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.

This branch has not been deployed

No deployments
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