Open anka-213 opened 2 years ago
It's not safe to copy the pinned bytearray backing a ByteString
. Look at the definition of ByteString
and ForeignPtr
:
data ByteString = BS {-# UNPACK #-} !(ForeignPtr Word8) -- payload
{-# UNPACK #-} !Int -- length
data ForeignPtr a = ForeignPtr Addr# ForeignPtrContents
That Addr#
will point to the wrong byte array if you do a deep copy that includes the ForeignPtrContents (most likely the PlainPtr data constructor).
I am working with a project that creates a large immutable long-living structure that unfortunately contains a bunch of
Data.ByteString
s. I was hoping to be able to easily reduce GC load by wrapping the creation of this structure with abut that of course gives the "Pinned ByteArray# objects cannot be compacted" exception.
I can think of two possible solutions (other than trying to eliminate all bytestrings from the code), but both might add way too much complexity:
I guess another option would be to make an unsafe version of the
compact
function, where you say "trust me, it's safe to copy these pinned ByteArray#".