haskell / bytestring

An efficient compact, immutable byte string type (both strict and lazy) suitable for binary or 8-bit character data.
http://hackage.haskell.org/package/bytestring
Other
289 stars 139 forks source link

Store a `State# RealWorld` in the ByteString #684

Open DemiMarie opened 2 months ago

DemiMarie commented 2 months ago

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.

clyring commented 2 months ago

I briefly considered this as an alternative to the current deferForeignPtrAvailability stuff in 2022. But:


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.

DemiMarie commented 2 months ago

Could ByteString provide the needed types itself?

clyring commented 2 months ago

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.