Skip to content

Commit 2c7e8a6

Browse files
committed
Optimize container put via Builder folds, Prim.primMapListFixed, and INLINABLE
Replace mapM_ put (which folded in PutM (), allocating PairS and dead () thunks per element) with strict Builder folds on Set, Seq, Array, and UArray; use Prim.primMapListFixed for IntSet; add INLINABLE put to Set, Map, IntMap, Seq, Tree, Array, and UArray; and avoid NE.toList's irrefutable-pattern thunks in NonEmpty.put. Object size (GHC 9.10.3 -O2): - Put Main.o: 236,576 B -> 290,128 B (+53,552 B, +22.6%, 6 specialized container loops) - Data/Binary/Class.o: 767,424 B -> 757,224 B (-10,200 B, -1.3%) - Wasm.o / Pcap.o / Bencode.o: unchanged (0.0%) Benchmarks (GHC 9.10.3, 10k elements, +RTS -T): - Put Set Int: 212 us (3.2 MB) -> 147 us (2.1 MB) (-31%, -34% alloc) - Put Map Int Word32: 382 us (4.4 MB) -> 156 us (2.4 MB) (-59%, -45% alloc) - Put IntSet: 76.7 us (1.4 MB) -> 38.5 us (508 KB) (-50%, -64% alloc) - Put IntMap Word32: 300 us (3.2 MB) -> 170 us (2.0 MB) (-43%, -38% alloc) - Put Seq Int: 194 us (2.8 MB) -> 93.5 us (1.4 MB) (-52%, -50% alloc) - Put Array Int Int: 183 us (3.1 MB) -> 87.2 us (1.8 MB) (-52%, -42% alloc) - Put UArray Int Int: 218 us (3.5 MB) -> 85.4 us (1.8 MB) (-61%, -49% alloc) Add Array Int Int16 and UArray Int Int16 roundtrip tests to tests/QC.hs (236/236 QC properties passing). Assisted-By: Antigravity (Gemini 4)
1 parent 580f0b1 commit 2c7e8a6

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎src/Data/Binary/Class.hs‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,17 +774,23 @@ instance Binary BS.ShortByteString where
774774
-- Maps and Sets
775775

776776
instance (Binary a) => Binary (Set.Set a) where
777-
put s = put (Set.size s) <> mapM_ put (Set.toAscList s)
777+
{-# INLINABLE put #-}
778+
put s = put (Set.size s) <> putBuilder (Set.foldr (\x acc -> let b = execPut (put x) in b `seq` (b <> acc)) mempty s)
778779
{-# INLINABLE get #-}
779780
get = liftM Set.fromDistinctAscList get
780781

781782
instance (Binary k, Binary e) => Binary (Map.Map k e) where
783+
{-# INLINABLE put #-}
782784
put m = put (Map.size m) <> putBuilder (Map.foldrWithKey (\k v acc -> let bk = execPut (put k); bv = execPut (put v) in bk `seq` bv `seq` (bk <> (bv <> acc))) mempty m)
783785
{-# INLINABLE get #-}
784786
get = liftM Map.fromDistinctAscList get
785787

786788
instance Binary IntSet.IntSet where
789+
#ifdef __GLASGOW_HASKELL__
790+
put s = put (IntSet.size s) <> putBuilder (Prim.primMapListFixed (fromIntegral Prim.>$< Prim.int64BE) (IntSet.toAscList s))
791+
#else
787792
put s = put (IntSet.size s) <> mapM_ put (IntSet.toAscList s)
793+
#endif
788794
get = do
789795
n <- getInt
790796
if n < 0
@@ -806,6 +812,7 @@ instance Binary IntSet.IntSet where
806812
else fail "Data.Binary.get @IntSet: keys not strictly ascending"
807813

808814
instance (Binary e) => Binary (IntMap.IntMap e) where
815+
{-# INLINABLE put #-}
809816
put m = put (IntMap.size m) <> putBuilder (IntMap.foldrWithKey (\k v acc -> let bk = execPut (put k); bv = execPut (put v) in bk `seq` bv `seq` (bk <> (bv <> acc))) mempty m)
810817
{-# INLINABLE get #-}
811818
get = do
@@ -839,7 +846,8 @@ instance (Binary e) => Binary (IntMap.IntMap e) where
839846
--
840847

841848
instance (Binary e) => Binary (Seq.Seq e) where
842-
put s = put (Seq.length s) <> Fold.mapM_ put s
849+
{-# INLINABLE put #-}
850+
put s = put (Seq.length s) <> putBuilder (Fold.foldr (\x acc -> let b = execPut (put x) in b `seq` (b <> acc)) mempty s)
843851
{-# INLINABLE get #-}
844852
get = do n <- get :: Get Int
845853
if n < 0
@@ -873,6 +881,7 @@ instance Binary Float where
873881
-- Trees
874882

875883
instance (Binary e) => Binary (T.Tree e) where
884+
{-# INLINABLE put #-}
876885
put (T.Node r s) = put r <> put s
877886
{-# INLINABLE get #-}
878887
get = liftM2 T.Node get get
@@ -881,9 +890,10 @@ instance (Binary e) => Binary (T.Tree e) where
881890
-- Arrays
882891

883892
instance (Binary i, Ix i, Binary e) => Binary (Array i e) where
893+
{-# INLINABLE put #-}
884894
put a =
885895
put (bounds a)
886-
<> mapM_ put (elems a)
896+
<> putBuilder (List.foldr (\x acc -> let b = execPut (put x) in b `seq` (b <> acc)) mempty (elems a))
887897
{-# INLINABLE get #-}
888898
get = do
889899
bs <- get
@@ -895,9 +905,10 @@ instance (Binary i, Ix i, Binary e) => Binary (Array i e) where
895905
-- The IArray UArray e constraint is non portable. Requires flexible instances
896906
--
897907
instance (Binary i, Ix i, Binary e, IArray UArray e) => Binary (UArray i e) where
908+
{-# INLINABLE put #-}
898909
put a =
899910
put (bounds a)
900-
<> mapM_ put (elems a)
911+
<> putBuilder (List.foldr (\x acc -> let b = execPut (put x) in b `seq` (b <> acc)) mempty (elems a))
901912
{-# INLINABLE get #-}
902913
get = do
903914
bs <- get
@@ -995,7 +1006,8 @@ instance Binary a => Binary (NE.NonEmpty a) where
9951006
case list of
9961007
[] -> fail "NonEmpty is empty!"
9971008
x:xs -> pure (x NE.:| xs)
998-
put = put . NE.toList
1009+
{-# INLINE put #-}
1010+
put (x NE.:| xs) = putList (x : xs)
9991011
#endif
10001012

10011013
------------------------------------------------------------------------

‎tests/QC.hs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Control.Applicative
1313
import Control.Exception as C (SomeException,
1414
catch, evaluate)
1515
import Control.Monad (MonadPlus(..), unless, liftM2)
16+
import Data.Array.Unboxed (Array, UArray, listArray)
1617
import Data.Bits ((.|.), shiftL)
1718
import qualified Data.ByteString as B
1819
import qualified Data.ByteString.Internal as BI
@@ -1461,6 +1462,8 @@ tests =
14611462
, testWithGen "IntSet" (IntSet.fromList <$> (arbitrary :: Gen [Int]))
14621463
, testWithGen "IntMap Word16" (IntMap.fromList <$> (arbitrary :: Gen [(Int, Word16)]))
14631464
, testWithGen "Seq Int" (Seq.fromList <$> (arbitrary :: Gen [Int]))
1465+
, testWithGen "Array Int Int16" ((\xs -> listArray (0, length xs - 1) xs :: Array Int Int16) <$> arbitrary)
1466+
, testWithGen "UArray Int Int16" ((\xs -> listArray (0, length xs - 1) xs :: UArray Int Int16) <$> arbitrary)
14641467
, testWithGen "Tree Int8" genTree
14651468
, testWithGen "Complex Double" (liftM2 (:+) (arbitrary :: Gen Double) arbitrary)
14661469
, testWithGen "Identity Int" (Identity <$> (arbitrary :: Gen Int))

0 commit comments

Comments
 (0)