bytestring icon indicating copy to clipboard operation
bytestring copied to clipboard

Store a `State# RealWorld` in the ByteString

Open DemiMarie opened this issue 1 year ago • 3 comments

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.

DemiMarie avatar Jun 22 '24 03:06 DemiMarie

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 using ByteString internals 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.
  • The necessary function unsafeReadFromByteString :: ByteString -> (ForeignPtr Word8 -> Int -> IO a) -> a is not really any safer than accursedUnutterablePerformIO, 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 a is needed to allow reading from more than one ByteString in a single IO state 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.

clyring avatar Jun 26 '24 18:06 clyring

Could ByteString provide the needed types itself?

DemiMarie avatar Jun 27 '24 00:06 DemiMarie

Could ByteString provide 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.

clyring avatar Jul 06 '24 14:07 clyring