Skip to content

Conversation

FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Sep 22, 2025

Add reference counting to circuit_outputs dynamic array.

Closes #1234

We currently duplicate the circuit_outputs array for every time the get_output libfunc gets called. This is because this libfunc consumes the array. To avoid having to duplicate it, this PRs implements a reference counter to the circuit outputs array. This way, we don't need to duplicate anything, and the array will get freed only when the last reference gets dropped.

Benchmarks:

Programs

Programs were executed with release mode and optimization level 2 (in x86):

Program base (s) head (s) Improvement
heavy_output_circuit 35.839 35.914 1x

Transactions

Tx times executed base (ms) head (ms) Improvement
0x77c686aebdada96749942f1c39d09345ed3e6db51172c467104f698ddda20bb 5000 44.24 44.30 1x
0x624b433291451e0e7b4dfed16a20a7a5c3e3a345f99261f403c8c6232787fc4 100 297.69 292.46 1.017x

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.

Copy link

github-actions bot commented Sep 22, 2025

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 2.870 ± 0.020 2.840 2.905 1.03 ± 0.02
base dict_insert.cairo (AOT) 2.797 ± 0.040 2.736 2.853 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 2.758 ± 0.047 2.681 2.820 1.07 ± 0.02
head dict_insert.cairo (AOT) 2.585 ± 0.035 2.535 2.647 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 2.568 ± 0.036 2.515 2.624 1.16 ± 0.05
base dict_snapshot.cairo (AOT) 2.206 ± 0.085 2.156 2.442 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 2.395 ± 0.024 2.357 2.431 1.04 ± 0.03
head dict_snapshot.cairo (AOT) 2.295 ± 0.057 2.215 2.369 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.642 ± 0.033 2.584 2.681 1.02 ± 0.02
base factorial_2M.cairo (AOT) 2.590 ± 0.027 2.547 2.633 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.765 ± 0.045 2.689 2.831 1.00 ± 0.02
head factorial_2M.cairo (AOT) 2.761 ± 0.045 2.700 2.817 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 2.180 ± 0.024 2.144 2.223 1.02 ± 0.01
base fib_2M.cairo (AOT) 2.138 ± 0.019 2.102 2.174 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 2.294 ± 0.026 2.257 2.340 1.03 ± 0.02
head fib_2M.cairo (AOT) 2.230 ± 0.026 2.184 2.269 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 2.370 ± 0.028 2.322 2.411 1.06 ± 0.02
base linear_search.cairo (AOT) 2.244 ± 0.023 2.222 2.293 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 2.392 ± 0.026 2.353 2.439 1.05 ± 0.02
head linear_search.cairo (AOT) 2.275 ± 0.029 2.238 2.322 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 2.475 ± 0.038 2.442 2.573 1.09 ± 0.02
base logistic_map.cairo (AOT) 2.268 ± 0.023 2.247 2.329 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 2.516 ± 0.031 2.457 2.550 1.06 ± 0.02
head logistic_map.cairo (AOT) 2.375 ± 0.038 2.306 2.425 1.00

Copy link

github-actions bot commented Sep 22, 2025

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 10.933 ± 0.104 10.815 11.141 4.51 ± 0.05
cairo-native (embedded AOT) 2.426 ± 0.014 2.412 2.447 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.513 ± 0.014 2.488 2.538 1.04 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 544.8 ± 7.6 536.1 560.1 1.00
cairo-native (embedded AOT) 2252.3 ± 25.5 2217.7 2303.4 4.13 ± 0.07
cairo-native (embedded JIT using LLVM's ORC Engine) 2375.8 ± 36.1 2337.9 2428.7 4.36 ± 0.09

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.826 ± 0.024 4.796 4.860 1.75 ± 0.03
cairo-native (embedded AOT) 2.754 ± 0.039 2.674 2.826 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.794 ± 0.053 2.687 2.876 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.752 ± 0.022 4.724 4.781 2.07 ± 0.03
cairo-native (embedded AOT) 2.295 ± 0.031 2.228 2.339 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.322 ± 0.043 2.252 2.396 1.01 ± 0.02

Benchmark for program heavy_output_circuit

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 36.766 ± 0.451 36.012 37.524 1.00
cairo-native (embedded AOT) 51.694 ± 0.583 50.911 52.595 1.41 ± 0.02
cairo-native (embedded JIT using LLVM's ORC Engine) 50.422 ± 0.315 49.925 50.906 1.37 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 577.6 ± 16.3 561.1 603.3 1.00
cairo-native (embedded AOT) 2316.9 ± 41.3 2240.6 2411.5 4.01 ± 0.13
cairo-native (embedded JIT using LLVM's ORC Engine) 2461.6 ± 25.6 2429.7 2504.7 4.26 ± 0.13

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 407.7 ± 5.8 399.5 415.1 1.00
cairo-native (embedded AOT) 2505.1 ± 23.2 2465.0 2538.3 6.14 ± 0.10
cairo-native (embedded JIT using LLVM's ORC Engine) 2676.7 ± 53.0 2570.9 2727.5 6.57 ± 0.16

Copy link

github-actions bot commented Sep 22, 2025

✅ Code is now correctly formatted.

@FrancoGiachetta FrancoGiachetta force-pushed the circuit-outputs-ref-counting branch from cda88ba to aa46b9a Compare September 22, 2025 18:30
@codecov-commenter
Copy link

codecov-commenter commented Sep 22, 2025

Codecov Report

❌ Patch coverage is 96.72131% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.41%. Comparing base (f27f7fd) to head (fa134dd).

Files with missing lines Patch % Lines
src/libfuncs/circuit.rs 91.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1419      +/-   ##
==========================================
+ Coverage   81.38%   81.41%   +0.03%     
==========================================
  Files         105      105              
  Lines       25805    25846      +41     
==========================================
+ Hits        21001    21043      +42     
+ Misses       4804     4803       -1     

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

@FrancoGiachetta FrancoGiachetta force-pushed the circuit-outputs-ref-counting branch from 91188f9 to 7ba95ec Compare September 23, 2025 16:18
@FrancoGiachetta FrancoGiachetta force-pushed the circuit-outputs-ref-counting branch from 348e424 to 5dadf39 Compare September 23, 2025 16:23
@FrancoGiachetta FrancoGiachetta force-pushed the circuit-outputs-ref-counting branch from 0cc685a to ac9f14e Compare September 23, 2025 21:00
@FrancoGiachetta FrancoGiachetta marked this pull request as ready for review September 24, 2025 17:26
@FrancoGiachetta FrancoGiachetta changed the title Add reference counting for circuit_outputs Add reference counting for circuit_outputs dynamic array Sep 24, 2025
@JulianGCalderon
Copy link
Contributor

If we see no performance improvement with this PR, I don't think we should merge for know it as it makes the code more complicated. Also, not seeing a performance increase might mean that there is something wrong with the implementation but we just don't know it yet.

@FrancoGiachetta
Copy link
Contributor Author

I agree. I'll put it back to draft until we are sure what to do.

@FrancoGiachetta FrancoGiachetta marked this pull request as draft September 29, 2025 13:23
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.

Reference Counting for Circuit Arrays

3 participants