ocaml icon indicating copy to clipboard operation
ocaml copied to clipboard

runtime/array.c: expose {uniform_,float}array_concat, use it in Float.Array

Open gasche opened this issue 1 year ago • 0 comments

This is an immediate follow-up over #13361, sorry for the noise.

Yesterday I was intimidated by the length of the implementation of caml_array_concat (it is 43 lines of C code that traverses an OCaml list of arrays and accumulates them and their sizes into separate arrays to call caml_array_gather, with a cool optimization to avoid dynamic allocations below a certain input size). I gave up on the idea of finding out how to refactor it cleverly to avoid duplicating it twice into a uniform_array and a floatarray version.

Today I realized that I could use the power of Functional Programming, in this case, function pointers. I just abstract over the gather function, then define three one-liner instances for each usage.

Note: This is the last primitive function over arrays that is implemented in C in array.c and exposed as an external in array.ml. The present PR brings Float.Array at complete parity in terms of runtime support.

(cc @nojb)

gasche avatar Aug 05 '24 13:08 gasche