Skip to content

fix(values): support Box and Nullable nested inside aggregates in to_ptr - #1656

Merged
TomerStarkware merged 1 commit into
mainfrom
tomer/values_nested_box_nullable
Aug 23, 2026
Merged

fix(values): support Box and Nullable nested inside aggregates in to_ptr#1656
TomerStarkware merged 1 commit into
mainfrom
tomer/values_nested_box_nullable

Conversation

@TomerStarkware

@TomerStarkware TomerStarkware commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #1655.

Problem

Value::to_ptr dispatched only on the Value variant, and Box<T> / Nullable<T> have no dedicated variant — so a box nested inside a struct, array, enum, or dict fell through to the payload's own arm and wrote the payload inline where the layout expects a single pointer, corrupting every following member. Only top-level boxes worked, via a dedicated AbiArgument arm.

Separately, that arm allocated the payload with libc_malloc, which leaked: boxes are arena-owned and never individually freed (see crate::libfuncs::box), so nothing ever released the block.

Fix

Handle both types by type before dispatching on the value: marshal the payload recursively, move it into the execution arena, and return a slot holding the payload pointer — the type's inline representation, matching from_ptr's Box/Nullable arms. Value::Null produces a slot holding a null pointer.

The AbiArgument Box/Nullable arms now delegate to this path and pass the pointer by value, removing the malloc leak and the Box/Nullable type_id inconsistency.

Tests

  • Native execution: Nullable<bool> argument (null and both variants — a memory-allocated payload), and a Box<felt252> nested inside a struct argument.
  • Round-trip unit tests: boxed bool, nullable bool (incl. null), struct with a boxed member.

🤖 Generated with Claude Code


This change is Reviewable

@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown

✅ Code is now correctly formatted.

@TomerStarkware
TomerStarkware force-pushed the tomer/values_nested_box_nullable branch from 9bed290 to c09a4f1 Compare August 18, 2026 09:43
@TomerStarkware
TomerStarkware force-pushed the tomer/values_inline_to_ptr branch from d046557 to ab01fbe Compare August 18, 2026 09:43
@TomerStarkware
TomerStarkware force-pushed the tomer/values_nested_box_nullable branch from c09a4f1 to b7dd582 Compare August 18, 2026 15:39
@TomerStarkware
TomerStarkware force-pushed the tomer/values_inline_to_ptr branch from ab01fbe to c534003 Compare August 19, 2026 16:07
@TomerStarkware
TomerStarkware force-pushed the tomer/values_nested_box_nullable branch from b7dd582 to 3d5c68c Compare August 19, 2026 16:07
@TomerStarkware
TomerStarkware requested a review from orizi August 20, 2026 12:28

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

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

@TomerStarkware
TomerStarkware force-pushed the tomer/values_inline_to_ptr branch from c534003 to 5d7a5d3 Compare August 23, 2026 08:31
Base automatically changed from tomer/values_inline_to_ptr to main August 23, 2026 09:47
`Value::to_ptr` dispatched only on the `Value` variant, and `Box<T>` /
`Nullable<T>` have no dedicated variant — so a box nested inside a
struct, array, enum, or dict fell through to the payload's own arm and
wrote the payload inline where the layout expects a single pointer,
corrupting every following member. Only top-level boxes worked, via a
dedicated `AbiArgument` arm.

Handle both types by type before dispatching on the value: marshal the
payload recursively, move it into the execution arena, and return a
slot holding the payload pointer — the type's inline representation,
matching what `from_ptr` reads.

The `AbiArgument` Box/Nullable arms now delegate to this path and pass
the pointer by value. This also replaces their `libc_malloc`, which
leaked: boxes are arena-owned and never individually freed (see
`crate::libfuncs::box`), so nothing ever released the malloc'd payload.

Adds native execution tests for `Nullable<bool>` (null and both
variants, exercising a memory-allocated payload) and a `Box<felt252>`
nested in a struct argument, plus `to_ptr`->`from_ptr` round-trip unit
tests for boxed bool, nullable bool, and a struct with a boxed member.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@TomerStarkware
TomerStarkware force-pushed the tomer/values_nested_box_nullable branch from 3d5c68c to e42ae5e Compare August 23, 2026 10:33
@github-actions

Copy link
Copy Markdown

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 8.600 ± 0.065 8.483 8.713 5.41 ± 0.07
cairo-native (embedded AOT) 1.589 ± 0.016 1.565 1.613 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.593 ± 0.015 1.568 1.615 1.00 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 449.2 ± 4.8 443.0 455.9 1.00
cairo-native (embedded AOT) 1385.1 ± 15.0 1365.5 1408.0 3.08 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 1420.0 ± 21.9 1393.4 1450.5 3.16 ± 0.06

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 3.682 ± 0.079 3.626 3.880 2.55 ± 0.06
cairo-native (embedded AOT) 1.444 ± 0.008 1.430 1.456 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.460 ± 0.013 1.434 1.478 1.01 ± 0.01

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 3.581 ± 0.011 3.555 3.595 2.55 ± 0.02
cairo-native (embedded AOT) 1.412 ± 0.011 1.387 1.425 1.00 ± 0.01
cairo-native (embedded JIT using LLVM's ORC Engine) 1.406 ± 0.008 1.397 1.420 1.00

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 481.1 ± 3.9 477.3 488.6 1.00
cairo-native (embedded AOT) 1412.1 ± 14.4 1390.1 1429.6 2.94 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 1438.2 ± 16.6 1418.1 1467.4 2.99 ± 0.04

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 412.7 ± 3.3 407.8 417.2 1.00
cairo-native (embedded AOT) 1401.9 ± 15.2 1376.8 1427.6 3.40 ± 0.05
cairo-native (embedded JIT using LLVM's ORC Engine) 1428.8 ± 15.3 1408.8 1456.2 3.46 ± 0.05

@github-actions

Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.821 ± 0.014 1.805 1.842 1.01 ± 0.01
base dict_insert.cairo (AOT) 1.803 ± 0.008 1.792 1.817 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.921 ± 0.031 1.888 1.991 1.00
head dict_insert.cairo (AOT) 1.922 ± 0.023 1.888 1.964 1.00 ± 0.02

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.639 ± 0.011 1.626 1.664 1.01 ± 0.01
base dict_snapshot.cairo (AOT) 1.615 ± 0.015 1.602 1.656 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.754 ± 0.026 1.705 1.788 1.01 ± 0.02
head dict_snapshot.cairo (AOT) 1.736 ± 0.022 1.691 1.768 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 1.697 ± 0.008 1.683 1.711 1.01 ± 0.01
base factorial_2M.cairo (AOT) 1.681 ± 0.009 1.668 1.697 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 1.801 ± 0.013 1.785 1.822 1.01 ± 0.01
head factorial_2M.cairo (AOT) 1.782 ± 0.020 1.754 1.808 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.648 ± 0.007 1.634 1.660 1.02 ± 0.01
base fib_2M.cairo (AOT) 1.610 ± 0.011 1.599 1.634 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.767 ± 0.021 1.740 1.809 1.02 ± 0.03
head fib_2M.cairo (AOT) 1.726 ± 0.044 1.629 1.778 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.663 ± 0.008 1.651 1.681 1.02 ± 0.01
base linear_search.cairo (AOT) 1.636 ± 0.009 1.616 1.647 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.794 ± 0.027 1.763 1.834 1.03 ± 0.02
head linear_search.cairo (AOT) 1.740 ± 0.018 1.716 1.780 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.642 ± 0.008 1.630 1.659 1.01 ± 0.01
base logistic_map.cairo (AOT) 1.626 ± 0.018 1.610 1.666 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.747 ± 0.018 1.717 1.769 1.01 ± 0.02
head logistic_map.cairo (AOT) 1.722 ± 0.022 1.690 1.755 1.00

@TomerStarkware
TomerStarkware added this pull request to the merge queue Aug 23, 2026
Merged via the queue into main with commit 164a316 Aug 23, 2026
15 checks passed
@TomerStarkware
TomerStarkware deleted the tomer/values_nested_box_nullable branch August 23, 2026 11:54
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