NativeArray
NativeArray — flat numeric arrays for high-performance numeric loops.
Unlike Array (which uses a 32-way persistent trie), NativeArray is backed by a flat OCaml array that maps directly to sequential memory. This makes it cache-friendly for sequential scan patterns and enables tight inner loops for sum, map, and fold operations.
P10 vectorization target: native_float_arr_sum and native_float_arr_map emit tight for-loops that LLVM can auto-vectorize to AVX2 when the program is compiled with --compile and -mavx2. In the interpreter path these builtins bypass the generic evaluator entirely, giving 5-10x speedup over the PVec Array for numeric-only workloads.
Two opaque element types are supported: NativeIntArr — wraps int array NativeFloatArr — wraps float array
Note: these are interpreter-path builtins. The --compile (LLVM) path support is tracked in specs/optimizations.md P10.
Usage: let arr = NativeArray.make_float(1000, 0.0) let arr2 = NativeArray.map_float(arr, fn x -> x *. 2.0) let s = NativeArray.sum_float(arr2)
Functions
Create a new integer array of [n] elements, each initialised to [fill].
Return the number of elements in [arr].
Return the element at index [i] (0-based). Panics if out of bounds.
Return a new array with element [i] replaced by [v]. O(n).
Sum all elements. O(n) tight loop.
Apply [f] to every element, returning a new NativeIntArr. O(n).
Left fold over all elements. [f(acc, elem) = new_acc]. O(n).
Build a NativeIntArr from a List(Int). O(n).
Convert a NativeIntArr to a List(Int). O(n).
Create a new float array of [n] elements, each initialised to [fill].
Return the number of elements in [arr].
Return the element at index [i] (0-based). Panics if out of bounds.
Return a new array with element [i] replaced by [v]. O(n).
Sum all elements. O(n) tight loop.
Apply [f] to every element, returning a new NativeFloatArr. O(n).
Left fold over all elements. [f(acc, elem) = new_acc]. O(n).
Build a NativeFloatArr from a List(Float). O(n).
Convert a NativeFloatArr to a List(Float). O(n).