[Git][ghc/ghc][wip/binary-array-no-list] compiler: improve Binary instance of Array
Cheng Shao pushed to branch wip/binary-array-no-list at Glasgow Haskell Compiler / GHC Commits: 1a713d56 by Cheng Shao at 2026-04-08T11:11:39+00:00 compiler: improve Binary instance of Array This patch improves the `Binary` instance of `Array`: - We no longer allocate intermediate lists. When serializing an `Array`, we iterate over the elements directly; when deserializing it, we allocate the result `Array` and fill it in a loop. - Now we only serialize the array bounds tuple; the length field is not needed. Closes #27109. - - - - - 1 changed file: - compiler/GHC/Utils/Binary.hs Changes: ===================================== compiler/GHC/Utils/Binary.hs ===================================== @@ -142,6 +142,8 @@ import Control.DeepSeq import Control.Monad ( when, (<$!>), unless, forM_, void ) import Foreign hiding (bit, setBit, clearBit, shiftL, shiftR, void) import Data.Array +import Data.Array.Base (unsafeFreezeIOArray) +import Data.Array.IArray (traverseArray_) import Data.Array.IO import Data.Array.Unsafe import qualified Data.Binary as Binary @@ -970,11 +972,12 @@ instance Binary a => Binary (NonEmpty a) where instance (Ix a, Binary a, Binary b) => Binary (Array a b) where put_ bh arr = do put_ bh $ bounds arr - put_ bh $ elems arr + traverseArray_ (put_ bh) arr + get bh = do - bounds <- get bh - xs <- get bh - return $ listArray bounds xs + (l, u) <- get bh + marr <- newGenArray (l, u) $ \_ -> get bh + unsafeFreezeIOArray marr instance Binary a => Binary (SmallArray a) where put_ bh sa = do View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1a713d562961d5db909a97031962d860... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1a713d562961d5db909a97031962d860... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Cheng Shao (@TerrorJack)