Skip to content

Conversation

@DiegoCivi
Copy link
Contributor

@DiegoCivi DiegoCivi commented Aug 11, 2025

The euclidean algorithm was being built for every inverse gate in a circuit. The aim of this PR is to improve compilation times in circuit related contracts by reducing the MLIR size and this is done by declaring the euclidean algorithm only once and the just calling it.

Benchmark

The following are the compilation times it took with the no_inline attribute and with an O2 optimization level. The Before column contains the compilation times for the main branch (ref -> without the optimization done in #1348), while the After column contains the compilations times for this branch (ref -> again, without the optimization done in #1348 ). These were ran on a MacBook Air M1 with 16 GB.

Class Hash Before (ms) After (ms) Improvement
0x1b5fbe104c033025dbb7fb37011781cc9344e881b4828cdaa023a80fecafde4 130730 110260 1.19x
0x3100defca27214e5f78f25e48a5b05e45899c6834cb4d34f48384c18e14dff7 125509 107107 1.17x
0x79666cdb4fc3cbcafbd74f4ea4e2855bf455c5a7c70915f5679325c54032771 123684 112580 1.10x
0xa40ecaa08eaef629d433b15f6b017461df8bea9b4e7299eb8ba3632e32b5a5 132966 118622 1.12x
0x4ffeed293927cd56686a9038a10026a2d3b9602f789d1f163c1c4ac9a822a82 177028 155397 1.14x
0x2269858a40ea0535cb373b0c981c91b907466edf6d65bcaf669760bbee0ae4d 169722 149323 1.14x
0x5ff378cb2f16804539ecb92e84f273aafbab57d450530e9fe8e87771705a673 138254 120414 1.15x
0x4edde37ca59d9dff8f4ac8945b1c4860b606abd61d74727904ad7494fccdfa9 138312 120037 1.15x

Correctness

Blocks 1560000 to 1580000 were executed and no diffs were found when comparing with the VM

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 Aug 11, 2025

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.023 ± 0.052 10.961 11.123 4.54 ± 0.03
cairo-native (embedded AOT) 2.428 ± 0.013 2.408 2.449 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.519 ± 0.027 2.491 2.571 1.04 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 522.0 ± 4.4 515.1 530.1 1.00
cairo-native (embedded AOT) 2094.0 ± 9.8 2072.7 2105.1 4.01 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 2221.0 ± 14.0 2197.1 2240.9 4.25 ± 0.05

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.792 ± 0.014 4.779 4.820 1.86 ± 0.03
cairo-native (embedded AOT) 2.576 ± 0.047 2.537 2.700 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.593 ± 0.014 2.571 2.615 1.01 ± 0.02

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.741 ± 0.021 4.717 4.782 2.28 ± 0.02
cairo-native (embedded AOT) 2.083 ± 0.019 2.060 2.120 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.127 ± 0.011 2.114 2.143 1.02 ± 0.01

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 559.7 ± 7.9 546.6 570.0 1.00
cairo-native (embedded AOT) 2152.3 ± 9.3 2138.9 2166.7 3.85 ± 0.06
cairo-native (embedded JIT using LLVM's ORC Engine) 2304.7 ± 23.2 2259.3 2338.0 4.12 ± 0.07

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 377.1 ± 6.8 371.6 390.1 1.00
cairo-native (embedded AOT) 2238.2 ± 7.0 2224.7 2244.4 5.94 ± 0.11
cairo-native (embedded JIT using LLVM's ORC Engine) 2419.1 ± 31.7 2387.0 2480.0 6.42 ± 0.14

@github-actions
Copy link

github-actions bot commented Aug 11, 2025

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 2.638 ± 0.048 2.592 2.760 1.04 ± 0.02
base dict_insert.cairo (AOT) 2.542 ± 0.025 2.495 2.571 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 2.606 ± 0.022 2.574 2.639 1.03 ± 0.01
head dict_insert.cairo (AOT) 2.523 ± 0.022 2.483 2.554 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 2.360 ± 0.030 2.311 2.404 1.06 ± 0.02
base dict_snapshot.cairo (AOT) 2.224 ± 0.022 2.193 2.275 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 2.328 ± 0.017 2.301 2.354 1.06 ± 0.01
head dict_snapshot.cairo (AOT) 2.194 ± 0.017 2.170 2.218 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.709 ± 0.028 2.667 2.748 1.02 ± 0.01
base factorial_2M.cairo (AOT) 2.661 ± 0.023 2.624 2.695 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.659 ± 0.030 2.611 2.711 1.01 ± 0.01
head factorial_2M.cairo (AOT) 2.629 ± 0.020 2.601 2.660 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 2.253 ± 0.015 2.234 2.283 1.02 ± 0.01
base fib_2M.cairo (AOT) 2.219 ± 0.021 2.185 2.255 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 2.197 ± 0.016 2.175 2.227 1.02 ± 0.01
head fib_2M.cairo (AOT) 2.148 ± 0.012 2.131 2.171 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 2.414 ± 0.024 2.387 2.465 1.05 ± 0.01
base linear_search.cairo (AOT) 2.291 ± 0.021 2.265 2.320 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 2.365 ± 0.017 2.339 2.401 1.06 ± 0.01
head linear_search.cairo (AOT) 2.241 ± 0.016 2.226 2.281 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 2.528 ± 0.039 2.483 2.598 1.07 ± 0.02
base logistic_map.cairo (AOT) 2.352 ± 0.010 2.337 2.368 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.487 ± 0.018 2.464 2.519 1.07 ± 0.01
head logistic_map.cairo (AOT) 2.318 ± 0.016 2.297 2.348 1.00

@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2025

Codecov Report

❌ Patch coverage is 96.47887% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.27%. Comparing base (bef8133) to head (a78c669).

Files with missing lines Patch % Lines
src/libfuncs/circuit.rs 85.71% 3 Missing ⚠️
src/metadata/runtime_bindings.rs 98.34% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1357      +/-   ##
==========================================
+ Coverage   76.22%   76.27%   +0.05%     
==========================================
  Files         111      111              
  Lines       26520    26602      +82     
==========================================
+ Hits        20214    20291      +77     
- Misses       6306     6311       +5     

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

@DiegoCivi DiegoCivi marked this pull request as ready for review August 27, 2025 19:24
Comment on lines 186 to 187
rhs_value: Value<'c, '_>,
circuit_modulus: Value<'c, '_>,
Copy link
Contributor

@JulianGCalderon JulianGCalderon Sep 5, 2025

Choose a reason for hiding this comment

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

Given that this is a generic implementation of egcd, we could rename arguments to a and b. (no need to keep the circuit-related names)

        a: Value<'c, '_>,
        b: Value<'c, '_>,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, done in 7335dd8

@DiegoCivi
Copy link
Contributor Author

Does it include the optimization done in #1384?

Nope, i'll change the description to clarify that

JulianGCalderon
JulianGCalderon previously approved these changes Sep 5, 2025
Copy link
Contributor

@JulianGCalderon JulianGCalderon left a comment

Choose a reason for hiding this comment

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

Looks good!

DiegoCivi and others added 3 commits September 8, 2025 18:37
Co-authored-by: Gabriel Bosio <[email protected]>
Co-authored-by: Gabriel Bosio <[email protected]>
@gabrielbosio gabrielbosio merged commit ac3e1a4 into main Sep 15, 2025
30 checks passed
@gabrielbosio gabrielbosio deleted the euclidean-mlir-func branch September 15, 2025 18:46
@github-project-automation github-project-automation bot moved this from In Review to Done in Starknet Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

6 participants