
John Meacham wrote:
GHC has 'pinned arrays' that have this behavior. however, you probably don't want to use them as they simply give the garbage collector less choices about what to do possibly decreasing its efficiency. The garbage collector already is free to not copy arrays if it feels it isn't worth it, by pinning them you simply take away its ability to choose to do so if it is needed.
To be a little more concrete, all arrays larger than ~3k are effectively pinned in GHC right now, as in they are never copied. If the array is unboxed, then it is never traversed by the GC either, so large unboxed arrays have basically zero GC cost. There's no need for any help from the programmer, it's done automatically by the GC. For smaller arrays, as John says there's a tradeoff in whether to pin them or not. Pinning avoids copying in the GC, but might lead to fragmentation. Pinning is necessary if you want to pass the address of the memory to an FFI call at any point, which is why bytestring pins its arrays. Cheers, Simon