Skip to content

Optimize MoonBit canonical list arguments with typed borrows - #1672

Draft
peter-jerry-ye wants to merge 2 commits into
bytecodealliance:mainfrom
peter-jerry-ye:codex/moonbit-canonical-list-borrows
Draft

Optimize MoonBit canonical list arguments with typed borrows#1672
peter-jerry-ye wants to merge 2 commits into
bytecodealliance:mainfrom
peter-jerry-ye:codex/moonbit-canonical-list-borrows

Conversation

@peter-jerry-ye

Copy link
Copy Markdown
Contributor

Why

MoonBit imports currently lower canonical lists through untyped integer pointers. This obscures the call-scoped lifetime of argument storage and causes list<u16> and list<s16> to allocate staging memory and lower every element because their public element types are widened.

What changed

  • Preserve u16 and s16 as UInt16 and Int16.
  • Represent canonical-layout primitive lists, including bool, as FixedArray[T].
  • Pass compatible list arguments directly to synchronous flat imports through typed #borrow FFI parameters, with the paired length defaulting to array.length().
  • Keep non-compatible lists on the existing generic lowering path.
  • Adopt compatible list results allocated through the caller's cabi_realloc; returned lists are not treated as borrows.

Why this is correct

The optimized path is limited to element types whose MoonBit array storage matches the Canonical ABI element size and alignment. It is only selected for synchronous imports with direct parameters, so the backing array remains alive for the complete host call. Async, indirect, nested resource-bearing, and non-compatible payloads continue to use their existing lowering behavior.

Regression coverage checks direct and record-flattened parameters, Boolean byte layout, compatible result lifting, and preservation of generic list lowering.

Scope

This change does not alter stream or future runtime behavior, resource ownership, FixedArray semantics, or canonical return allocation. Stream-specific bulk-copy work remains separate.

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.

1 participant