Skip to content

Commit 92f0841

Browse files
committed
Use sumSize instead of size shiftR 1 in GSumGet and GSumPut
Previously GSumGet and GSumPut split (a :+: b) via sizeL = size `shiftR` 1. That matches GHC's balanced deriving Generic trees, but causes silent tag collisions on manually written unbalanced sum trees (e.g. C1 :+: (C2 :+: (C3 :+: C4)), where both C3 and C4 encoded to tag 3 and C3 decoded as C4). Split using sizeL = unTagged (sumSize :: Tagged a Word64) instead and drop the now-redundant size parameter from getSumWith and putSum. Wire format for all GHC-derived Generic instances is bit-for-bit identical. Add prop_Generic_unbalanced_sums (right- and left-nested 4-ctor sums) to tests/GenericLargeSum.hs and tests/QC.hs. Benchmarks (GHC 9.10.3 -O2, WasmBench, +RTS -T): - wasm/generic/encode: 1.82 ms (11 MB) -> 1.81 ms (11 MB) (unchanged) - wasm/generic/decode: 2.88 ms (15 MB) -> 2.86 ms (15 MB) (unchanged) Object sizes (GHC 9.10.3 -O2): - Data/Binary/Generic.o: 39,440 -> 38,064 B (-1,376 B / -3.49%) - Wasm.o: 2,557,488 -> 2,557,488 B (0 B / identical) Compiles with zero warnings under both default and -DBINARY_PORTABLE. Assisted-By: Antigravity (Gemini 4)
1 parent d1642a7 commit 92f0841

3 files changed

Lines changed: 43 additions & 22 deletions

File tree

‎src/Data/Binary/Generic.hs‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import Control.Applicative
2424
import Data.Binary.Class
2525
import Data.Binary.Get
2626
import Data.Binary.Put
27-
import Data.Bits
2827
import Data.Word
2928
import Data.Proxy
3029
#if !MIN_VERSION_base(4,11,0)
@@ -84,7 +83,7 @@ instance Binary a => GBinaryGet (K1 i a) where
8483
-- use two bytes, and so on till 2^64-1.
8584

8685
#define GUARD(WORD) (size - 1) <= fromIntegral (maxBound :: WORD)
87-
#define PUTSUM(WORD) GUARD(WORD) = putSum (Proxy :: Proxy WORD) 0 size
86+
#define PUTSUM(WORD) GUARD(WORD) = putSum (Proxy :: Proxy WORD) 0
8887
#define GETSUM(WORD) GUARD(WORD) = (get :: Get WORD) >>= checkGetSum size . fromIntegral
8988

9089
instance ( GSumPut a, GSumPut b
@@ -111,36 +110,34 @@ sizeError s size =
111110

112111
checkGetSum :: (GSumGet f) => Word64 -> Word64 -> Get (f a)
113112
checkGetSum size code
114-
| code < size = getSumWith (size > 8) id code size
113+
| code < size = getSumWith (size > 8) id code
115114
| otherwise = fail "Unknown encoding for constructor"
116115
{-# INLINE checkGetSum #-}
117116

118117
class GSumGet f where
119-
getSumWith :: Bool -> (f a -> r) -> Word64 -> Word64 -> Get r
118+
getSumWith :: Bool -> (f a -> r) -> Word64 -> Get r
120119

121120
class GSumPut f where
122-
putSum :: (Binary word, Num word) => Proxy word -> Word64 -> Word64 -> f a -> Put
121+
putSum :: (Binary word, Num word) => Proxy word -> Word64 -> f a -> Put
123122

124-
instance (GSumGet a, GSumGet b) => GSumGet (a :+: b) where
125-
getSumWith large k !code !size
126-
| code < sizeL = getSumWith large (k . L1) code sizeL
127-
| otherwise = getSumWith large (k . R1) (code - sizeL) sizeR
123+
instance (GSumGet a, GSumGet b, SumSize a) => GSumGet (a :+: b) where
124+
getSumWith large k !code
125+
| code < sizeL = getSumWith large (k . L1) code
126+
| otherwise = getSumWith large (k . R1) (code - sizeL)
128127
where
129-
sizeL = size `shiftR` 1
130-
sizeR = size - sizeL
128+
sizeL = unTagged (sumSize :: Tagged a Word64)
131129
{-# INLINE getSumWith #-}
132130

133-
instance (GSumPut a, GSumPut b) => GSumPut (a :+: b) where
134-
putSum p !code !size s = case s of
135-
L1 x -> putSum p code sizeL x
136-
R1 x -> putSum p (code + sizeL) sizeR x
131+
instance (GSumPut a, GSumPut b, SumSize a) => GSumPut (a :+: b) where
132+
putSum p !code s = case s of
133+
L1 x -> putSum p code x
134+
R1 x -> putSum p (code + sizeL) x
137135
where
138-
sizeL = size `shiftR` 1
139-
sizeR = size - sizeL
136+
sizeL = unTagged (sumSize :: Tagged a Word64)
140137
{-# INLINE putSum #-}
141138

142139
instance GBinaryGet a => GSumGet (C1 c a) where
143-
getSumWith large k _ _
140+
getSumWith large k _
144141
| large = fmapSum k gget
145142
| otherwise = k <$> gget
146143
{-# INLINE getSumWith #-}
@@ -150,7 +147,7 @@ fmapSum f g = f <$> g
150147
{-# NOINLINE fmapSum #-}
151148

152149
instance GBinaryPut a => GSumPut (C1 c a) where
153-
putSum (_ :: Proxy word) !code _ x = put (fromIntegral code :: word) <> gput x
150+
putSum (_ :: Proxy word) !code x = put (fromIntegral code :: word) <> gput x
154151
{-# INLINE putSum #-}
155152

156153
------------------------------------------------------------------------

‎tests/GenericLargeSum.hs‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
-- tree, consuming ~30 GiB of RAM and OOM-killing CI runners (both 64-bit and
44
-- 32-bit i386). The library under test (Data.Binary.Generic) is still compiled
55
-- with full optimization.
6-
{-# OPTIONS_GHC -O0 #-}
6+
{-# OPTIONS_GHC -O0 -fmax-simplifier-iterations=0 #-}
77
{-# LANGUAGE DataKinds, TypeOperators #-}
88
module GenericLargeSum
99
( prop_Generic_Word16_and_Word32_sums
10+
, prop_Generic_unbalanced_sums
1011
) where
1112

1213
import qualified Data.ByteString.Lazy as L
13-
import Data.Word (Word16, Word32)
14+
import Data.Word (Word8, Word16, Word32)
1415
import GHC.Generics ((:+:)(..), C1, M1(..), U1(..), Meta(..), FixityI(..))
1516

1617
import Data.Binary
@@ -69,3 +70,24 @@ prop_Generic_Word16_and_Word32_sums =
6970
&& runGet gget bsFirst131072 == first131072
7071
&& runGet gget bsLast131072 == last131072
7172
&& isFail (runGetOrFail (gget :: Get (GSum131072 ())) (encode (131072 :: Word32)))
73+
74+
type UnbalR4 = GSum1 :+: (GSum1 :+: (GSum1 :+: GSum1))
75+
type UnbalL4 = ((GSum1 :+: GSum1) :+: GSum1) :+: GSum1
76+
77+
prop_Generic_unbalanced_sums :: Bool
78+
prop_Generic_unbalanced_sums =
79+
let c = M1 U1 :: GSum1 ()
80+
rVals :: [UnbalR4 ()]
81+
rVals = [L1 c, R1 (L1 c), R1 (R1 (L1 c)), R1 (R1 (R1 c))]
82+
lVals :: [UnbalL4 ()]
83+
lVals = [L1 (L1 (L1 c)), L1 (L1 (R1 c)), L1 (R1 c), R1 c]
84+
check vals =
85+
let encs = map (runPut . gput) vals
86+
expected = map (encode :: Word8 -> L.ByteString) [0, 1, 2, 3]
87+
in encs == expected && map (runGet gget) encs == vals
88+
isFail (Left _) = True
89+
isFail (Right _) = False
90+
in check rVals
91+
&& check lVals
92+
&& isFail (runGetOrFail (gget :: Get (UnbalR4 ())) (encode (4 :: Word8)))
93+
&& isFail (runGetOrFail (gget :: Get (UnbalL4 ())) (encode (4 :: Word8)))

‎tests/QC.hs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ import Test.QuickCheck hiding (total)
6666

6767
import qualified Action (tests)
6868
import Arbitrary ()
69-
import GenericLargeSum (prop_Generic_Word16_and_Word32_sums)
69+
import GenericLargeSum (prop_Generic_Word16_and_Word32_sums,
70+
prop_Generic_unbalanced_sums)
7071
import Data.Binary
7172
import qualified Data.Binary.Builder as Builder
7273
import Data.Binary.Get
@@ -1582,6 +1583,7 @@ tests =
15821583
, testProperty "GenericSmall (small sum + products)" prop_GenericSmall
15831584
, testProperty "GenericSmall invalid tag fails" prop_GenericSmall_invalidTag
15841585
, testProperty "Generic Word16 (512) and Word32 (131072) sums" prop_Generic_Word16_and_Word32_sums
1586+
, testProperty "Generic unbalanced sum trees" prop_Generic_unbalanced_sums
15851587
, testProperty "GenericVoid get fails" prop_GenericVoid_get
15861588
, testProperty "GenericVoid put is strict" prop_GenericVoid_put
15871589
, testProperty "GenericVoid empty list" prop_GenericVoid_list

0 commit comments

Comments
 (0)