Question about large (big?)arrays (maybe of int) and garbage collection

Dear All,

I was wondering what the behaviour of GC is when encountering a large array (maybe bigarray is different?) of ints (say, since they look a lot like pointers at runtime). Does the array get scanned to check if there are pointers to other objects (in which case, what about efficiency…)? Or does the GC somehow know that the array doesn’t contain pointers?


Bigarrays cannot hold pointers to OCaml values, so they’re never scanned by the GC. On the other hand, regular arrays will be scanned by the GC, even if they only hold integers.
There are various tricks that can be used to create something that looks like an array of ints but will not be scanned, but I would recommend using bigarrays instead. It’s going to be more reliable, although the read/write costs are slightly higher.


Brilliant! Thank you so much.

Now you have intrigued me… why do int bigarray operations cost more than normal int arrays?

They’re not represented the same way.
The layout of OCaml arrays means that the value manipulated by the runtime can be used as a pointer to a C-style array of values. So reading field n from array a is roughly *(a + n) (if we forget about bound checks).
On the other hand, bigarrays are stored in a more complicated structure that can handle multi-dimensional arrays and cases where the data is not uniquely owned. So in practice, bound checks are more expensive even for one-dimensional bigarrays and the actual array is behind one indirection.
In the end reading field n of bigarray a is roughly *(*(a + 1) + n) (again, after bound checks).

1 Like

Makes sense. Thank you!