streamly icon indicating copy to clipboard operation
streamly copied to clipboard

Expose `Streamly.Data.Unboxed`

Open adithyaov opened this issue 3 years ago • 7 comments

adithyaov avatar Jul 25 '22 11:07 adithyaov

  1. Rename the Unboxed APIs properly.
  2. Rename ArrayContents properly

readByteArray -> peekWith writeByteArray -> pokeWith ArrayContents -> Memory or Storage or OAM (Offset Adressable Memory)

adithyaov avatar Jul 25 '22 12:07 adithyaov

To be able to support different memory types, we can split the Unboxed type class in two classes:

  • Unboxed: Specifying size and alignment for the type, this will be the same for all memory types. That way we can ensure that the size and alignment remain the same whichever method we use to read or write the type.
  • Unbox: Specifying the read and write operations, this will be different for different memory types

We can then possibly have multiple Unbox type classes for different memory types:

  • Array.Unbox operating on MutableByteArray#
  • Foreign.Unbox operating on Ptr akin to Foreign.Storable
  • ForeignPtr.Unbox operating on ForeignPtr (requires touching during read and write). This can be implemented in terms of the previous two.

We can then use the same common array code and pass the relevant type class dictionary to use a particular memory type. We could use a type class with two parameters one for memory type and one for the storable type, but that would make constraint types unnecessarily complex. And we would mostly need only the byte-array type class, others would not be used often. We only need a provision for those, just in case.

harendra-kumar avatar Aug 03 '22 16:08 harendra-kumar

Alternatively, instead of creating a new type class for size and alignment, we can just reuse Storable and put a Storable constraint on Unbox. And make sure all instances are compatible with Storable. This will make the use of Storable and Unbox consistent. The downside is that to make an instance of Unbox you will also have to make an instance of Storable. However, if you are never going to use peek/poke you can leave those undefined and just specify size/alignment.

harendra-kumar avatar Aug 06 '22 06:08 harendra-kumar

Common API naming:

  • newAligned
  • getInternal
  • cast
  • reallocAligned
  • asPtrUnsafe
  • putSliceUnsafe
  • cmp

We should not expose internal details. Any common internal manipulating APIs should be given a place in the Unbox type class.

adithyaov avatar Aug 14 '22 09:08 adithyaov

The module should be renamed to Streamly.Internal.Data.MutableByteArray

adithyaov avatar Aug 14 '22 09:08 adithyaov

Closed in https://github.com/composewell/streamly/pull/1772

adithyaov avatar Sep 07 '22 06:09 adithyaov

We can keep this open as there is more discussion on this. Pushing this to 0.10.0

adithyaov avatar Sep 07 '22 06:09 adithyaov

See #1910

adithyaov avatar Oct 12 '22 11:10 adithyaov