Skip to content

JS Array.to_list copies array slots at every tree level #1661

Closed as not planned

Description

@oxura

Array.to_list walks the Base array tree. JS arrays are flat, so each ANode match slices the backing array in two before the walk continues. This copies O(n log n) slots to produce an O(n) list.

I profiled the actual generated .mjs export with 4,096 elements on Bun 1.4.3:

Work per conversion Main Flat walk
Array.slice calls 8,190 0
Copied array slots 49,152 0

The fix is in oxura@de9bf2f8 . It walks the flat array backward and prepends list nodes without mutating the input. Array.to_list is a template: its checked specializations have generated names and no Base flag, so JS dispatch recognizes those instances through the Base template’s metadata. Arbitrary user definitions do not become intrinsics, and C keeps the existing implementation.

The generated exports preserve order for six valid power-of-two sizes from 1 to 4,096, accept frozen input arrays, and preserve record-element identity. A focused regression covers U32 and record elements plus a first-class function reference; it returns True{} on both JS and native C. The repository gate passes 54/54. I did not run the macOS/Metal gate cluster.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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