Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Initializing array from data or element segment #260

@askeksa-google

Description

@askeksa-google

In various discussions, we have touched upon the subject of having a version of the array.init instruction which initializes the array from a data or element segment rather than from the stack. Most recently, @rossberg suggested some variations of the operation in #250 (comment).

Of the various proposed instructions for this purpose, I think the ones that create a new array are the most important, as these provide new capabilities for global initializers. These would be (staying with the init nomenclature):

array.init_from_data $a (data $d) : [i32 i32 (rtt $a)] -> [(ref $a)]
array.init_from_elem $a (elem $e) : [i32 i32 (rtt $a)] -> [(ref $a)]

each taking an offset (byte resp. element) and the array length from the stack.

I did an experiment in dart2wasm, replacing the use of array.init for string constants with array.init_from_data as specified above.

For a module with 34k of string data, it reduced the module size by 48k uncompressed. Compressed (zopfli), the size reduction was less dramatic: 300 bytes if the instruction takes the offset first, then the length, or 1200 bytes if it takes the length followed by the offset.

But this is not merely a matter of module size reduction. Due to the limitation of array.init that there is a maximum length it can accept, these instructions are essential to enable arrays of any length to be initialized in global initializers. This is important for the implementation of Dart constant expressions, for instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions