March Docs

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

fnmake_intmake_int(n : Int, fill : Int)#

Create a new integer array of [n] elements, each initialised to [fill].

fnlength_intlength_int(arr)#

Return the number of elements in [arr].

fnget_intget_int(arr, i : Int)#

Return the element at index [i] (0-based). Panics if out of bounds.

fnset_intset_int(arr, i : Int, v : Int)#

Return a new array with element [i] replaced by [v]. O(n).

fnsum_intsum_int(arr) : Int#

Sum all elements. O(n) tight loop.

fnmap_intmap_int(arr, f)#

Apply [f] to every element, returning a new NativeIntArr. O(n).

fnfold_intfold_int(acc, arr, f)#

Left fold over all elements. [f(acc, elem) = new_acc]. O(n).

fnfrom_list_intfrom_list_int(lst)#

Build a NativeIntArr from a List(Int). O(n).

fnto_list_intto_list_int(arr)#

Convert a NativeIntArr to a List(Int). O(n).

fnmake_floatmake_float(n : Int, fill : Float)#

Create a new float array of [n] elements, each initialised to [fill].

fnlength_floatlength_float(arr)#

Return the number of elements in [arr].

fnget_floatget_float(arr, i : Int)#

Return the element at index [i] (0-based). Panics if out of bounds.

fnset_floatset_float(arr, i : Int, v : Float)#

Return a new array with element [i] replaced by [v]. O(n).

fnsum_floatsum_float(arr) : Float#

Sum all elements. O(n) tight loop.

fnmap_floatmap_float(arr, f)#

Apply [f] to every element, returning a new NativeFloatArr. O(n).

fnfold_floatfold_float(acc, arr, f)#

Left fold over all elements. [f(acc, elem) = new_acc]. O(n).

fnfrom_list_floatfrom_list_float(lst)#

Build a NativeFloatArr from a List(Float). O(n).

fnto_list_floatto_list_float(arr)#

Convert a NativeFloatArr to a List(Float). O(n).