Skip to content

Conversation

@FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Oct 22, 2025

Optimize felt252_div compilation

Closes #None.

Similar to #1400, this PR tries to optimize felts division's compilation time. This issue seems to be the same as the one the mentioned PR attacks. Arithmetic operations tend to rise the execution of the pass TwoAddressInstructionPass, which tries to legalize the operations so as to generate properly formed instructions.
Particularly in x86, this pass tends to take a great amount of time during object generation phase of the compilation.

Benchmarks:

Compilation

I benchmarked one class hash which seems to be having this issue. This was performed with the following specs:

  • Native Profile: Default
  • OS: GNU/LinuxArch: x86_64
  • Memory (GiB): 125.7
  • CPU: AMD Ryzen 9 5950X 16-Core Processor
  • Native version (before): e27ad22
  • Native version (after): 8c56617
Class Before (s) After (s) Improvement
0x01ad981ba6707c4f518467704f33e415832588b5029ff9dc53118d3dff84b599 99 78 1.27x

Execution

I've run some block checking execution performance, each of them were run 10 times. The specs used are the same as the ones with compilation.

Block range Before (ns) After (ns) Improvement
630000-630010 16169074200 16306315021 0.991x
1478010-1478030 8723982882 8794435720 0.991x
1486830-1486835 2152507242 2192441702 0.981x
1492810-1492820 5673719876 5559305065 1.020x

Samply

I've also run the compilation of the same contract with samply, using the same specs as with benchmarks.

  • Without changes: link
  • With changes: link

Introduces Breaking Changes?

No.

These PRs should be merged after this one right away, in that order.

Checklist

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

@github-actions
Copy link

github-actions bot commented Oct 22, 2025

✅ Code is now correctly formatted.

@FrancoGiachetta FrancoGiachetta force-pushed the opt-felt252_div-compilation branch from 07ccfdd to 4bf1632 Compare October 22, 2025 19:39
@codecov-commenter
Copy link

codecov-commenter commented Oct 22, 2025

Codecov Report

❌ Patch coverage is 95.87629% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.44%. Comparing base (35361e2) to head (a93ff67).

Files with missing lines Patch % Lines
src/libfuncs/felt252.rs 90.47% 2 Missing ⚠️
src/libfuncs/circuit.rs 88.88% 1 Missing ⚠️
src/metadata/runtime_bindings.rs 98.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1437      +/-   ##
==========================================
- Coverage   81.45%   81.44%   -0.01%     
==========================================
  Files         105      105              
  Lines       25869    25845      -24     
==========================================
- Hits        21072    21050      -22     
+ Misses       4797     4795       -2     

☔ 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.

@github-actions
Copy link

github-actions bot commented Oct 22, 2025

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 2.483 ± 0.011 2.463 2.495 1.01 ± 0.03
base dict_insert.cairo (AOT) 2.456 ± 0.064 2.385 2.590 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 2.599 ± 0.025 2.559 2.634 1.04 ± 0.01
head dict_insert.cairo (AOT) 2.502 ± 0.023 2.477 2.543 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 2.310 ± 0.051 2.265 2.403 1.03 ± 0.03
base dict_snapshot.cairo (AOT) 2.248 ± 0.038 2.162 2.285 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 2.284 ± 0.012 2.270 2.303 1.05 ± 0.02
head dict_snapshot.cairo (AOT) 2.177 ± 0.039 2.153 2.284 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.677 ± 0.045 2.622 2.764 1.00
base factorial_2M.cairo (AOT) 2.727 ± 0.046 2.692 2.846 1.02 ± 0.02

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.661 ± 0.017 2.634 2.680 1.01 ± 0.01
head factorial_2M.cairo (AOT) 2.634 ± 0.032 2.591 2.686 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 2.154 ± 0.032 2.106 2.211 1.03 ± 0.02
base fib_2M.cairo (AOT) 2.092 ± 0.036 2.053 2.178 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 2.193 ± 0.016 2.176 2.226 1.02 ± 0.02
head fib_2M.cairo (AOT) 2.157 ± 0.032 2.120 2.211 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base heavy_circuit.cairo (JIT) 13.820 ± 0.073 13.688 13.897 1.02 ± 0.01
base heavy_circuit.cairo (AOT) 13.544 ± 0.124 13.367 13.832 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head heavy_circuit.cairo (JIT) 13.861 ± 0.095 13.682 13.993 1.02 ± 0.01
head heavy_circuit.cairo (AOT) 13.526 ± 0.083 13.360 13.623 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 2.296 ± 0.017 2.275 2.327 1.06 ± 0.01
base linear_search.cairo (AOT) 2.157 ± 0.015 2.129 2.179 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 2.369 ± 0.015 2.347 2.398 1.06 ± 0.01
head linear_search.cairo (AOT) 2.239 ± 0.020 2.206 2.268 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 2.396 ± 0.014 2.377 2.421 1.07 ± 0.01
base logistic_map.cairo (AOT) 2.246 ± 0.015 2.226 2.265 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.476 ± 0.022 2.457 2.520 1.06 ± 0.03
head logistic_map.cairo (AOT) 2.333 ± 0.052 2.288 2.449 1.00

@github-actions
Copy link

github-actions bot commented Oct 22, 2025

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.039 ± 0.299 10.893 11.876 4.38 ± 0.13
cairo-native (embedded AOT) 2.523 ± 0.027 2.485 2.574 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.635 ± 0.015 2.613 2.658 1.04 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 555.1 ± 13.5 542.0 587.0 1.00
cairo-native (embedded AOT) 2288.4 ± 21.3 2258.7 2320.1 4.12 ± 0.11
cairo-native (embedded JIT using LLVM's ORC Engine) 2419.8 ± 27.5 2362.3 2455.5 4.36 ± 0.12

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.818 ± 0.025 4.783 4.867 1.73 ± 0.02
cairo-native (embedded AOT) 2.803 ± 0.075 2.712 2.926 1.01 ± 0.03
cairo-native (embedded JIT using LLVM's ORC Engine) 2.778 ± 0.036 2.708 2.827 1.00

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.738 ± 0.022 4.714 4.782 2.11 ± 0.07
cairo-native (embedded AOT) 2.246 ± 0.070 2.159 2.400 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.289 ± 0.035 2.217 2.329 1.02 ± 0.04

Benchmark for program heavy_circuit

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 10.136 ± 0.146 9.858 10.308 1.00
cairo-native (embedded AOT) 15.237 ± 0.213 14.770 15.491 1.50 ± 0.03
cairo-native (embedded JIT using LLVM's ORC Engine) 15.458 ± 0.238 15.127 15.817 1.53 ± 0.03

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 595.5 ± 10.9 581.8 613.0 1.00
cairo-native (embedded AOT) 2332.8 ± 36.6 2289.9 2394.4 3.92 ± 0.09
cairo-native (embedded JIT using LLVM's ORC Engine) 2419.0 ± 56.5 2361.2 2520.5 4.06 ± 0.12

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 392.9 ± 8.9 379.2 409.9 1.00
cairo-native (embedded AOT) 2385.8 ± 49.4 2337.7 2492.1 6.07 ± 0.19
cairo-native (embedded JIT using LLVM's ORC Engine) 2581.3 ± 32.5 2526.2 2628.8 6.57 ± 0.17

@FrancoGiachetta FrancoGiachetta force-pushed the opt-felt252_div-compilation branch from 8af6772 to 8c56617 Compare October 23, 2025 15:15
@FrancoGiachetta FrancoGiachetta force-pushed the opt-felt252_div-compilation branch from 402fa82 to 0ce2bb5 Compare October 24, 2025 18:58
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.

3 participants