stdio icon indicating copy to clipboard operation
stdio copied to clipboard

ByteString conversions

Open jac3km4 opened this issue 6 years ago • 1 comments

Added the ByteString conversions I've slightly changed bytesFromByteString, I think it should be better now Also, I've added it to Std.Foreign.PrimArray, because putting it in Std.Vector.Base would cause circular module dependencies, since Std.Foreign.PrimArray uses the vector modules.

jac3km4 avatar Apr 02 '19 18:04 jac3km4

It's OK to add these to Std.Foreign.PrimArray, but I still want to avoid copying in those functions if possible. You can do it by directly match on PrimVector or ByteString's contructor, get the ByteArray# (or MutableByteArray# if possible):

  • If it's ByteArray# coming from Bytes, depend on it's pinned or not we can save a copying.
  • If it's MutableByteArray# from ByteString, we can directly freeze it and construct a Bytes, if it's another type of ForeignPtr payload, we do a copy.

I know it sounds complicated, but It's probably better than doing copy and allocation everytime, since:

  • Now days most of ByteStrings are based on MutableByteArray#.
  • Most of time, Bytes coming from IO operations are pinned.

winterland1989 avatar Apr 03 '19 02:04 winterland1989