bytestring
bytestring copied to clipboard
Store a `State# RealWorld` in the ByteString
This allows passing the state thread used to initialize the ByteString’s pointer to the functions that access that pointer. As a result, deferForeignPtrAvailability can be removed, and most or all uses of accursedUnutterablePerformIO can be replaced with a safer function that takes a State# RealWorld argument.
I briefly considered this as an alternative to the current deferForeignPtrAvailability stuff in 2022. But:
- It's a rather invasive change, requiring basically all code that reads the contents of
ByteStrings unsafely usingByteStringinternals to be rewritten.- I think there is probably much more code out there that performs such unsafe reads than code that unsafely creates
ByteStrings directly using the constructor.
- I think there is probably much more code out there that performs such unsafe reads than code that unsafely creates
- The necessary function
unsafeReadFromByteString :: ByteString -> (ForeignPtr Word8 -> Int -> IO a) -> ais not really any safer thanaccursedUnutterablePerformIO, although at least the conditions under which it is safe are probably more obvious to the uninitiated. - Another function
unsafeReadFromByteStringIO :: ByteString -> (ForeignPtr Word8 -> Int -> IO a) -> IO ais needed to allow reading from more than oneByteStringin a singleIOstate thread.
From my perspective, the most principled solution would be for base to provide both MutableForeignPtr and ImmutableForeignPtr types, and just store an ImmutableForeignPtr Word8 in each ByteString. Then, read operations don't need to pretend to do IO, and initializing a ByteString ultimately involves calling an unsafeFreezeMutableForeignPtr primitive at the right time.
But I don't know how to realistically get there from the current status quo.
Could ByteString provide the needed types itself?
Could
ByteStringprovide the needed types itself?
There is some argument for doing so. After all, the only difference between a ByteString and an ImmutableForeignPtr Word8 is that the former stores the length of (the relevant part of) its buffer.
But the main obstacles are impact assessment and migration, rather than finding the best possible home for the relevant types.