Skip to content

Allocate memory for contract-call? args representation #6009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: feat/clarity-wasm-develop
Choose a base branch
from

Conversation

BowTiedWoo
Copy link

This PR changes how we allocate memory space when writing argument representations.

Previously, we used get_type_in_memory_size to calculate space for all arguments, which often over-allocated memory unnecessarily.

Now, we only allocate space for list arguments — the only type whose representation is written directly to the offset.

for item in &l.data {
let (len, in_mem_len) = write_to_wasm(
&mut store,
memory,
ltd.get_list_item_type(),
offset + written,
in_mem_offset + in_mem_written,
item,
true,
)?;

All other types are written to in_mem_offset inside the pass_argument_to_wasm function and do not require reserved space at offset.

Closes: stacks-network/clarity-wasm#633

@BowTiedWoo BowTiedWoo requested a review from a team as a code owner April 14, 2025 13:37
@BowTiedWoo BowTiedWoo requested a review from Acaccia April 14, 2025 13:37
Copy link
Contributor

@Acaccia Acaccia left a comment

Choose a reason for hiding this comment

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

This is incorrect globally.
The discussion we had that lead to this fix was flawed.

More than that, we have many types that would write at offset, the description is incorrect.

@BowTiedWoo
Copy link
Author

Thank you for the feedback!
Here’s the reasoning behind the PR description:

In pass_argument_to_wasm, the following types write directly to in_mem_offset using memory.write():

For List, each item uses write_to_wasm, which:

Writes to offset:

  • int, uint, bool (just value);
  • buffer, string, list, principal (offset + length);
  • optional, tuple, response (indicator bytes, possibly recursive)

Writes to in_mem_offset: buffer, string, principal (just the data)

So, only List reliably writes to both offset and in_mem_offset. Others either skip writing or only write at in_mem_offset.
(optional, tuple, and response can include nested types like List, but I haven’t fully traced that yet.)

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