Skip to content

moved boxes and nullables to arena#1593

Merged
TomerStarkware merged 2 commits intomainfrom
tomer/box_arena
May 1, 2026
Merged

moved boxes and nullables to arena#1593
TomerStarkware merged 2 commits intomainfrom
tomer/box_arena

Conversation

@TomerStarkware
Copy link
Copy Markdown
Collaborator

@TomerStarkware TomerStarkware commented Apr 18, 2026

#Move Box and Nullable allocations to a per-invocation arena

Replace individual realloc/free calls for Box and Nullable allocations with a thread-local bumpalo arena that is reset once per program invocation.

  • New runtime function cairo_native__box_alloc(size, align) allocates out of a thread-local Bump arena, registered as the BoxAlloc runtime binding (src/runtime.rs,
    src/metadata/runtime_bindings.rs).
  • invoke_dynamic calls cairo_native__reset_box_arena() after the trampoline returns, freeing all box/nullable memory allocated during that invocation in one shot
    (src/executor.rs).
  • Libfuncs (box, const, enum::boxed_match, struct::boxed_deconstruct) now route all box allocations through into_boxRuntimeBindingsMeta::box_alloc, and no longer emit
    free on unbox / boxed_match / span_from_tuple.
  • types/box.rs and types/nullable.rs:
    • Dup now uses the arena instead of realloc and is only registered when the inner type actually has a dup override (the pure-memcpy path is gone).
    • Drop is only registered when the inner type has a drop override; the trailing free is removed since arena memory is reclaimed at invocation end.
  • Value::from_ptr no longer libc_frees the inner pointer for Box/Nullable payloads.

Introduces Breaking Changes?

No


This change is Reviewable

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 18, 2026

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 11.191 ± 0.091 11.109 11.423 5.75 ± 0.06
cairo-native (embedded AOT) 1.945 ± 0.011 1.925 1.961 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.031 ± 0.017 2.001 2.057 1.04 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 530.8 ± 5.5 524.1 540.2 1.00
cairo-native (embedded AOT) 1640.3 ± 12.5 1622.0 1662.2 3.09 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 1756.6 ± 18.0 1727.8 1779.4 3.31 ± 0.05

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.951 ± 0.068 4.898 5.061 2.36 ± 0.04
cairo-native (embedded AOT) 2.098 ± 0.012 2.079 2.120 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 2.145 ± 0.019 2.113 2.177 1.02 ± 0.01

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 4.821 ± 0.045 4.797 4.949 2.97 ± 0.04
cairo-native (embedded AOT) 1.625 ± 0.012 1.611 1.653 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.698 ± 0.017 1.673 1.727 1.04 ± 0.01

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 567.9 ± 2.1 565.0 571.3 1.00
cairo-native (embedded AOT) 1696.3 ± 18.0 1660.8 1720.7 2.99 ± 0.03
cairo-native (embedded JIT using LLVM's ORC Engine) 1823.0 ± 22.7 1784.6 1859.4 3.21 ± 0.04

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 483.4 ± 6.6 477.8 496.0 1.00
cairo-native (embedded AOT) 1800.3 ± 15.3 1780.9 1831.3 3.72 ± 0.06
cairo-native (embedded JIT using LLVM's ORC Engine) 1987.8 ± 18.8 1961.1 2016.5 4.11 ± 0.07

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 18, 2026

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.934 ± 0.031 1.899 2.001 1.03 ± 0.02
base dict_insert.cairo (AOT) 1.870 ± 0.023 1.829 1.905 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 2.139 ± 0.016 2.117 2.164 1.03 ± 0.01
head dict_insert.cairo (AOT) 2.075 ± 0.023 2.039 2.111 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.685 ± 0.019 1.658 1.714 1.07 ± 0.02
base dict_snapshot.cairo (AOT) 1.568 ± 0.016 1.549 1.601 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.828 ± 0.015 1.801 1.849 1.08 ± 0.02
head dict_snapshot.cairo (AOT) 1.688 ± 0.020 1.660 1.717 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 2.056 ± 0.015 2.038 2.089 1.03 ± 0.01
base factorial_2M.cairo (AOT) 1.999 ± 0.022 1.970 2.031 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 2.160 ± 0.017 2.127 2.188 1.03 ± 0.01
head factorial_2M.cairo (AOT) 2.096 ± 0.014 2.074 2.114 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.630 ± 0.021 1.591 1.657 1.03 ± 0.02
base fib_2M.cairo (AOT) 1.581 ± 0.021 1.557 1.624 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.706 ± 0.012 1.688 1.722 1.04 ± 0.01
head fib_2M.cairo (AOT) 1.644 ± 0.014 1.626 1.677 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.739 ± 0.014 1.721 1.763 1.08 ± 0.01
base linear_search.cairo (AOT) 1.609 ± 0.014 1.586 1.632 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.817 ± 0.014 1.792 1.837 1.08 ± 0.01
head linear_search.cairo (AOT) 1.681 ± 0.011 1.659 1.698 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.885 ± 0.019 1.848 1.913 1.10 ± 0.01
base logistic_map.cairo (AOT) 1.720 ± 0.012 1.702 1.736 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.987 ± 0.017 1.970 2.029 1.10 ± 0.01
head logistic_map.cairo (AOT) 1.811 ± 0.012 1.796 1.831 1.00

Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: 1 of 11 files reviewed, 1 unresolved discussion (waiting on TomerStarkware).


src/executor.rs line 355 at r1 (raw file):

    builtin_stats.blake = BLAKE_CALL_COUNT.with(|c| c.replace(0)) as usize;

    // Reset the box arena to free all allocated memory.

maybe this entire bump object be just stored in metadata or something of that sort?

@TomerStarkware TomerStarkware force-pushed the tomer/box_arena branch 5 times, most recently from 6d23fc6 to cad7b7f Compare April 28, 2026 11:22
Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 17 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on TomerStarkware).


src/libfuncs/array.rs line 206 at r3 (raw file):

    )?)?;

    // Move the data into the array and free the original tuple. Since the tuple and the array are

fix doc.

make sure next PR removes almost all memcopies.

Copy link
Copy Markdown
Collaborator Author

@TomerStarkware TomerStarkware left a comment

Choose a reason for hiding this comment

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

@TomerStarkware made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi).


src/libfuncs/array.rs line 206 at r3 (raw file):

Previously, orizi wrote…

fix doc.

make sure next PR removes almost all memcopies.

Done.

Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

:lgtm:

@orizi reviewed 1 file and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

:lgtm:

@orizi reviewed 3 files and all commit messages, and made 1 comment.
Reviewable status: 16 of 17 files reviewed, all discussions resolved (waiting on TomerStarkware).

Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 1 file.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@TomerStarkware TomerStarkware added this pull request to the merge queue May 1, 2026
Merged via the queue into main with commit ff7ce48 May 1, 2026
26 checks passed
@TomerStarkware TomerStarkware deleted the tomer/box_arena branch May 1, 2026 01:48
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.

2 participants