Skip to content

Commit b3e72c2

Browse files
committed
Add INLINE/INLINABLE pragmas to primitive put, Maybe/Either put, Arg, and Ratio
Add INLINE put to (), Proxy, Bool, Ordering, Word8..64, Int8..64, Word, Int, Maybe, Either, and Semigroup.Arg (plus INLINE get on Arg), and INLINABLE put/get with strict (pure $!) on Ratio. Object size (GHC 9.10.3 -O2): - Pcap.o: 517,432 B -> 526,792 B (+9,360 B, +1.8%) - Wasm.o: 2,470,592 B -> 2,466,440 B (-4,152 B, -0.2%) - Get Main.o: 515,976 B -> 520,264 B (+4,288 B, +0.8%, Ratio specialization) - Put Main.o: 228,008 B -> 228,008 B ( 0 B, 0.0%) - Data/Binary/Class.o: 764,888 B -> 767,424 B (+2,536 B, +0.3%) Benchmarks (GHC 9.10.3, 10k elements, +RTS -T): - Put [Either Int Word32]: 342 us (3.4 MB) -> 251 us (2.7 MB) (-27%, -21% alloc) - Get [Ratio Int]: 1.60 ms (9.2 MB) -> 1.14 ms (6.4 MB) (-29%, -30% alloc) - Put [Ratio Int]: 512 us (4.9 MB) -> 491 us (4.9 MB) (-4%) - Put [Maybe Word32]: 145 us (1.7 MB) -> 145 us (1.7 MB) (same) - pcap/generic/encode: 358 us (3.8 MB) -> 170 us (2.1 MB) (-53%, 2.1x faster, -45% alloc) Add Semigroup.Arg Int Word16 roundtrip tests to tests/QC.hs (232/232 QC properties passing). Assisted-By: Antigravity (Gemini 4)
1 parent 463ade0 commit b3e72c2

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎src/Data/Binary/Class.hs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,20 @@ instance Binary Void where
216216
-- The () type need never be written to disk: values of singleton type
217217
-- can be reconstructed from the type alone
218218
instance Binary () where
219+
{-# INLINE put #-}
219220
put () = mempty
220221
{-# INLINE get #-}
221222
get = return ()
222223

223224
instance Binary (Proxy a) where
225+
{-# INLINE put #-}
224226
put Proxy = pure ()
225227
{-# INLINE get #-}
226228
get = pure Proxy
227229

228230
-- Bools are encoded as a byte in the range 0 .. 1
229231
instance Binary Bool where
232+
{-# INLINE put #-}
230233
put = putWord8 . fromIntegral . fromEnum
231234
{-# INLINE get #-}
232235
get = getWord8 >>= toBool
@@ -237,6 +240,7 @@ instance Binary Bool where
237240

238241
-- Values of type 'Ordering' are encoded as a byte in the range 0 .. 2
239242
instance Binary Ordering where
243+
{-# INLINE put #-}
240244
put = putWord8 . fromIntegral . fromEnum
241245
{-# INLINE get #-}
242246
get = getWord8 >>= toOrd
@@ -251,6 +255,7 @@ instance Binary Ordering where
251255

252256
-- Words8s are written as bytes
253257
instance Binary Word8 where
258+
{-# INLINE put #-}
254259
put = putWord8
255260
#ifdef __GLASGOW_HASKELL__
256261
{-# INLINE putList #-}
@@ -263,6 +268,7 @@ instance Binary Word8 where
263268

264269
-- Words16s are written as 2 bytes in big-endian (network) order
265270
instance Binary Word16 where
271+
{-# INLINE put #-}
266272
put = putWord16be
267273
#ifdef __GLASGOW_HASKELL__
268274
{-# INLINE putList #-}
@@ -275,6 +281,7 @@ instance Binary Word16 where
275281

276282
-- Words32s are written as 4 bytes in big-endian (network) order
277283
instance Binary Word32 where
284+
{-# INLINE put #-}
278285
put = putWord32be
279286
#ifdef __GLASGOW_HASKELL__
280287
{-# INLINE putList #-}
@@ -287,6 +294,7 @@ instance Binary Word32 where
287294

288295
-- Words64s are written as 8 bytes in big-endian (network) order
289296
instance Binary Word64 where
297+
{-# INLINE put #-}
290298
put = putWord64be
291299
#ifdef __GLASGOW_HASKELL__
292300
{-# INLINE putList #-}
@@ -299,6 +307,7 @@ instance Binary Word64 where
299307

300308
-- Int8s are written as a single byte.
301309
instance Binary Int8 where
310+
{-# INLINE put #-}
302311
put = putInt8
303312
#ifdef __GLASGOW_HASKELL__
304313
{-# INLINE putList #-}
@@ -311,6 +320,7 @@ instance Binary Int8 where
311320

312321
-- Int16s are written as a 2 bytes in big endian format
313322
instance Binary Int16 where
323+
{-# INLINE put #-}
314324
put = putInt16be
315325
#ifdef __GLASGOW_HASKELL__
316326
{-# INLINE putList #-}
@@ -323,6 +333,7 @@ instance Binary Int16 where
323333

324334
-- Int32s are written as a 4 bytes in big endian format
325335
instance Binary Int32 where
336+
{-# INLINE put #-}
326337
put = putInt32be
327338
#ifdef __GLASGOW_HASKELL__
328339
{-# INLINE putList #-}
@@ -335,6 +346,7 @@ instance Binary Int32 where
335346

336347
-- Int64s are written as a 8 bytes in big endian format
337348
instance Binary Int64 where
349+
{-# INLINE put #-}
338350
put = putInt64be
339351
#ifdef __GLASGOW_HASKELL__
340352
{-# INLINE putList #-}
@@ -349,6 +361,7 @@ instance Binary Int64 where
349361

350362
-- Words are are written as Word64s, that is, 8 bytes in big endian format
351363
instance Binary Word where
364+
{-# INLINE put #-}
352365
put = putWord64be . fromIntegral
353366
#ifdef __GLASGOW_HASKELL__
354367
{-# INLINE putList #-}
@@ -361,6 +374,7 @@ instance Binary Word where
361374

362375
-- Ints are are written as Int64s, that is, 8 bytes in big endian format
363376
instance Binary Int where
377+
{-# INLINE put #-}
364378
put = putInt64be . fromIntegral
365379
#ifdef __GLASGOW_HASKELL__
366380
{-# INLINE putList #-}
@@ -558,11 +572,13 @@ freezeByteArray arr = IO $ \s ->
558572
-}
559573

560574
instance (Binary a,Integral a) => Binary (R.Ratio a) where
575+
{-# INLINABLE put #-}
561576
put r = put (R.numerator r) <> put (R.denominator r)
577+
{-# INLINABLE get #-}
562578
get = do
563579
n <- get
564580
d <- get
565-
if d == 0 then fail "zero denominator" else pure (n R.% d)
581+
if d == 0 then fail "zero denominator" else pure $! (n R.% d)
566582

567583
instance Binary a => Binary (Complex a) where
568584
{-# INLINE put #-}
@@ -707,6 +723,7 @@ getMany n
707723
{-# INLINE getMany #-}
708724

709725
instance (Binary a) => Binary (Maybe a) where
726+
{-# INLINE put #-}
710727
put Nothing = putWord8 0
711728
put (Just x) = putWord8 1 <> put x
712729
{-# INLINE get #-}
@@ -718,6 +735,7 @@ instance (Binary a) => Binary (Maybe a) where
718735
_ -> fail ("Data.Binary.get @Maybe: invalid tag " ++ show w)
719736

720737
instance (Binary a, Binary b) => Binary (Either a b) where
738+
{-# INLINE put #-}
721739
put (Left a) = putWord8 0 <> put a
722740
put (Right b) = putWord8 1 <> put b
723741
{-# INLINE get #-}
@@ -961,7 +979,9 @@ deriving instance Binary m => Binary (Semigroup.WrappedMonoid m)
961979

962980
-- | @since 0.8.4.0
963981
instance (Binary a, Binary b) => Binary (Semigroup.Arg a b) where
982+
{-# INLINE get #-}
964983
get = liftM2 Semigroup.Arg get get
984+
{-# INLINE put #-}
965985
put (Semigroup.Arg a b) = put a <> put b
966986

967987
------------------------------------------------------------------------

‎tests/QC.hs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,7 @@ tests =
14771477
, testWithGen "Semigroup.First Int" (Semigroup.First <$> (arbitrary :: Gen Int))
14781478
, testWithGen "Semigroup.Last Int" (Semigroup.Last <$> (arbitrary :: Gen Int))
14791479
, testWithGen "Semigroup.WrappedMonoid Int" (Semigroup.WrapMonoid <$> (arbitrary :: Gen Int))
1480+
, testWithGen "Semigroup.Arg Int Word16" (liftM2 Semigroup.Arg (arbitrary :: Gen Int) (arbitrary :: Gen Word16))
14801481
, testWithGen "NonEmpty Int" (liftM2 (:|) (arbitrary :: Gen Int) arbitrary)
14811482
, testWithGen "Proxy Int" (return (Proxy :: Proxy Int))
14821483
]

0 commit comments

Comments
 (0)