Skip to content

Commit 4e27abc

Browse files
committed
Match first argument strictly in Put (*>) and (<>) and use B.append
In Data.Binary.Put, pattern-match the first argument strictly in (*>) and mappend' while keeping the second argument lazy (preserving infinite streaming Put such as 'let go = putWord8 1 <> go' and 'mapM_ put [1..]'), and use Data.Binary.Builder.append directly instead of Monoid.mappend. This eliminates the intermediate Builder thunk on the left-hand side of every (<>) / (>>) without breaking Safe Haskell or lazy streaming. Benchmarks (GHC 9.10.3, tasty-bench +RTS -T, vs parent commit npqmvlxx): - wasm/generic/encode Module: 2.04 ms -> 1.81 ms (-12%), 15 MB -> 13 MB alloc - generics-bench (Cabal24 enc): 1.38 ms -> 1.34 ms (same), 9.2 MB -> 8.2 MB alloc (-11%) - pcap/encode & pcap/generic: 2.2 MB -> 2.1 MB / 4.0 MB -> 3.9 MB alloc (same time) Assisted-By: Antigravity (Gemini 4)
1 parent c189cf3 commit 4e27abc

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

‎src/Data/Binary/Put.hs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ instance Applicative PutM where
125125
PairS x w' = unPut k
126126
in PairS (f x) (w `Monoid.mappend` w')
127127

128-
m *> k = Put $
129-
let PairS _ w = unPut m
130-
PairS b w' = unPut k
131-
in PairS b (w `Monoid.mappend` w')
128+
m *> k = case unPut m of
129+
PairS _ w -> Put $
130+
let PairS b w' = unPut k
131+
in PairS b (w `B.append` w')
132132
{-# INLINE (*>) #-}
133133

134134
-- Standard Writer monad, with aggressive inlining
@@ -157,10 +157,9 @@ instance Monoid.Monoid (PutM ()) where
157157
{-# INLINE mappend #-}
158158

159159
mappend' :: Put -> Put -> Put
160-
mappend' m k = Put $
161-
let PairS _ w = unPut m
162-
PairS _ w' = unPut k
163-
in PairS () (w `Monoid.mappend` w')
160+
mappend' (Put (PairS _ w)) k = Put $
161+
let PairS _ w' = unPut k
162+
in PairS () (w `B.append` w')
164163
{-# INLINE mappend' #-}
165164

166165
#ifdef HAS_SEMIGROUP

0 commit comments

Comments
 (0)