RFC: unsafeShrinkMutableByteArray#

Herbert Valerio Riedel hvriedel at gmail.com
Sat Jul 12 07:51:54 UTC 2014


Hello Simon (et al.)

While experimenting with refactoring/improving integer-gmp, I'd like to
represent a GMP number just by a ByteArrays# (and thus save a redundant
limb-count field). However, for that I'd need an efficient way to resize
a MutableByteArray# for the result value in case its initial size over-
(or under-)allocated.

Right now I'd re-allocate via newByteArray# with the final size and
copyMutableByteArray#, and now I was wondering if we couldn't simply
have an

  unsafeShrinkMutableByteArray#
    :: MutableByteArray# s# -> Int# -> State# s -> State# s

operation, which would allow for zero-copying. (the 'unsafe' denotes
this wouldn't check if the new size is less-or-equal to the current
size, and that one has to be careful when subsequently using
sizeofMutableByteArray# which is currently a pure function)

Is such an operation feasible, or is there something in the RTS/GC that
would trip over when a ByteArray has suddenly a smaller byte-count than
its originally newByteArray#'ed amount?

PS: maybe unsafeShrinkMutableByteArray# could unsafe-freeze the
    ByteArray# while at it (thus be called something like
    unsafeShrinkAndFreezeMutableByteArray#), as once I know the final
    smaller size I would freeze it anyway right after shrinking.

Cheers,
  hvr


More information about the ghc-devs mailing list