Skip to content

Draft: PoC - Empty preallocated arrays.

Andreas Klebinger requested to merge wip/andreask/empty_arrays into master

I looked into #22249 today. Sadly it's not trivial to define a emptyByteArray# value.

As far as I can think of right now there are no arity 0 unlifted values that aren't literals in GHC. This means trying to add such a value runs into odd corners like GHCi wanting a lifted wrapper and I'm sure it would run into more later on.

So I tried to make those functions retuning an empty array instead. But it really doesn't offer much value over just using the existing primitives at that point. Anyway here is the current state:

Add new primitives that return empty preallocated arrays.

* newEmptyByteArray#  :: (# #) -> ByteArray#
* newEmptySmallArray# :: forall {l :: Levity} (a :: TYPE (BoxedRep l)).
                         (# #) -> SmallArray# a
* newEmptyArray#      :: forall {l :: Levity} (a :: TYPE (BoxedRep l)).
                         (# #) -> Array# a

This allows users to avoid allocating a new array when a empty Array is
required.

But one could get basically the same via:

{-# NOINLINE empty #-}
empty = unsafePerformIO $ newByteArray 0

newEmptyByteArray# _ = case empty of ByteArray ba -> ba

So I don't see the point of merging this as-is. I guess it would avoid making things caffy but that seems like a small win.

Merge request reports