on master help sign in
Rename serialize/deserialize/size of Serialize class
13581bfHarendra Kumar
benchmark/Streamly/Benchmark/Data/Serialize.hs+3−3
@@ -49,8 +49,8 @@ import Streamly.Benchmark.Data.Serialize.RecNonCompatible
49#else
49#else
50#define SERIALIZE_CLASS Serialize
50#define SERIALIZE_CLASS Serialize
51#define DERIVE_CLASS(typ) $(deriveSerialize [d|instance Serialize typ|])
51#define DERIVE_CLASS(typ) $(deriveSerialize [d|instance Serialize typ|])
52−#define SERIALIZE_OP serialize
52+#define SERIALIZE_OP serializeAt
53−#define DESERIALIZE_OP deserialize
53+#define DESERIALIZE_OP deserializeAt
54#endif
54#endif
55
55
56-------------------------------------------------------------------------------
56-------------------------------------------------------------------------------
@@ -288,7 +288,7 @@ getSize :: forall a. SERIALIZE_CLASS a => a -> Int
288#ifdef USE_UNBOX
288#ifdef USE_UNBOX
289getSize _ = sizeOf (Proxy :: Proxy a)
289getSize _ = sizeOf (Proxy :: Proxy a)
290#else
290#else
291−getSize = size 0
291+getSize = addSizeTo 0
292#endif
292#endif
293
293
294-------------------------------------------------------------------------------
294-------------------------------------------------------------------------------
core/src/Streamly/Data/MutByteArray.hs+3−3
@@ -75,9 +75,9 @@ module Streamly.Data.MutByteArray
75 -- Deriving Serialize
75 -- Deriving Serialize
76 , SerializeConfig
76 , SerializeConfig
77 , serializeConfig
77 , serializeConfig
78− , inlineSize
78+ , inlineAddSizeTo
79− , inlineSerialize
79+ , inlineSerializeAt
80− , inlineDeserialize
80+ , inlineDeserializeAt
81
81
82 , deriveSerialize
82 , deriveSerialize
83 , deriveSerializeWith
83 , deriveSerializeWith
core/src/Streamly/Internal/Data/Array.hs+4−3
@@ -561,9 +561,9 @@ streamFold f arr = f (A.read arr)
561encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
561encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
562encodeAs ps a =
562encodeAs ps a =
563 unsafeInlineIO $ do
563 unsafeInlineIO $ do
564− let len = Serialize.size 0 a
564+ let len = Serialize.addSizeTo 0 a
565 mbarr <- MBA.newBytesAs ps len
565 mbarr <- MBA.newBytesAs ps len
566− off <- Serialize.serialize 0 mbarr a
566+ off <- Serialize.serializeAt 0 mbarr a
567 assertM(len == off)
567 assertM(len == off)
568 pure $ Array mbarr 0 off
568 pure $ Array mbarr 0 off
569
569
@@ -584,6 +584,7 @@ pinnedSerialize = encodeAs Pinned
584deserialize :: Serialize a => Array Word8 -> a
584deserialize :: Serialize a => Array Word8 -> a
585deserialize arr@(Array {..}) = unsafeInlineIO $ do
585deserialize arr@(Array {..}) = unsafeInlineIO $ do
586 let lenArr = length arr
586 let lenArr = length arr
587+ (off, val) <-
587− (off, val) <- Serialize.deserialize arrStart arrContents (arrStart + lenArr)
588+ Serialize.deserializeAt arrStart arrContents (arrStart + lenArr)
588 assertM(off == arrStart + lenArr)
589 assertM(off == arrStart + lenArr)
589 pure val
590 pure val
core/src/Streamly/Internal/Data/MutByteArray.hs+59−58
@@ -88,73 +88,73 @@ import Streamly.Internal.Data.Unbox.TH
88-- $(Serialize.deriveSerialize ''Maybe)
88-- $(Serialize.deriveSerialize ''Maybe)
89instance Serialize a => Serialize (Maybe a) where
89instance Serialize a => Serialize (Maybe a) where
90
90
91− {-# INLINE size #-}
91+ {-# INLINE addSizeTo #-}
92− size acc x =
92+ addSizeTo acc x =
93 case x of
93 case x of
94 Nothing -> (acc + 1)
94 Nothing -> (acc + 1)
95− Just field0 -> (size (acc + 1)) field0
95+ Just field0 -> (addSizeTo (acc + 1)) field0
96
96
97− {-# INLINE deserialize #-}
97+ {-# INLINE deserializeAt #-}
98− deserialize initialOffset arr endOffset = do
98+ deserializeAt initialOffset arr endOffset = do
99− (i0, tag) <- ((deserialize initialOffset) arr) endOffset
99+ (i0, tag) <- ((deserializeAt initialOffset) arr) endOffset
100 case tag :: Word8 of
100 case tag :: Word8 of
101 0 -> pure (i0, Nothing)
101 0 -> pure (i0, Nothing)
102− 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
102+ 1 -> do (i1, a0) <- ((deserializeAt i0) arr) endOffset
103 pure (i1, Just a0)
103 pure (i1, Just a0)
104 _ -> error "Found invalid tag while peeking (Maybe a)"
104 _ -> error "Found invalid tag while peeking (Maybe a)"
105
105
106− {-# INLINE serialize #-}
106+ {-# INLINE serializeAt #-}
107− serialize initialOffset arr val =
107+ serializeAt initialOffset arr val =
108 case val of
108 case val of
109 Nothing -> do
109 Nothing -> do
110− i0 <- ((serialize initialOffset) arr) (0 :: Word8)
110+ i0 <- ((serializeAt initialOffset) arr) (0 :: Word8)
111 pure i0
111 pure i0
112 Just field0 -> do
112 Just field0 -> do
113− i0 <- ((serialize initialOffset) arr) (1 :: Word8)
113+ i0 <- ((serializeAt initialOffset) arr) (1 :: Word8)
114− i1 <- ((serialize i0) arr) field0
114+ i1 <- ((serializeAt i0) arr) field0
115 pure i1
115 pure i1
116
116
117-- $(Serialize.deriveSerialize ''Either)
117-- $(Serialize.deriveSerialize ''Either)
118instance (Serialize a, Serialize b) => Serialize (Either a b) where
118instance (Serialize a, Serialize b) => Serialize (Either a b) where
119
119
120− {-# INLINE size #-}
120+ {-# INLINE addSizeTo #-}
121− size acc x =
121+ addSizeTo acc x =
122 case x of
122 case x of
123− Left field0 -> (size (acc + 1)) field0
123+ Left field0 -> (addSizeTo (acc + 1)) field0
124− Right field0 -> (size (acc + 1)) field0
124+ Right field0 -> (addSizeTo (acc + 1)) field0
125
125
126− {-# INLINE deserialize #-}
126+ {-# INLINE deserializeAt #-}
127− deserialize initialOffset arr endOffset = do
127+ deserializeAt initialOffset arr endOffset = do
128− (i0, tag) <- ((deserialize initialOffset) arr) endOffset
128+ (i0, tag) <- ((deserializeAt initialOffset) arr) endOffset
129 case tag :: Word8 of
129 case tag :: Word8 of
130− 0 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
130+ 0 -> do (i1, a0) <- ((deserializeAt i0) arr) endOffset
131 pure (i1, Left a0)
131 pure (i1, Left a0)
132− 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
132+ 1 -> do (i1, a0) <- ((deserializeAt i0) arr) endOffset
133 pure (i1, Right a0)
133 pure (i1, Right a0)
134 _ -> error "Found invalid tag while peeking (Either a b)"
134 _ -> error "Found invalid tag while peeking (Either a b)"
135
135
136− {-# INLINE serialize #-}
136+ {-# INLINE serializeAt #-}
137− serialize initialOffset arr val =
137+ serializeAt initialOffset arr val =
138 case val of
138 case val of
139 Left field0 -> do
139 Left field0 -> do
140− i0 <- ((serialize initialOffset) arr) (0 :: Word8)
140+ i0 <- ((serializeAt initialOffset) arr) (0 :: Word8)
141− i1 <- ((serialize i0) arr) field0
141+ i1 <- ((serializeAt i0) arr) field0
142 pure i1
142 pure i1
143 Right field0 -> do
143 Right field0 -> do
144− i0 <- ((serialize initialOffset) arr) (1 :: Word8)
144+ i0 <- ((serializeAt initialOffset) arr) (1 :: Word8)
145− i1 <- ((serialize i0) arr) field0
145+ i1 <- ((serializeAt i0) arr) field0
146 pure i1
146 pure i1
147
147
148instance Serialize (Proxy a) where
148instance Serialize (Proxy a) where
149
149
150− {-# INLINE size #-}
150+ {-# INLINE addSizeTo #-}
151− size acc _ = (acc + 1)
151+ addSizeTo acc _ = (acc + 1)
152
152
153− {-# INLINE deserialize #-}
153+ {-# INLINE deserializeAt #-}
154− deserialize initialOffset _ _ = pure ((initialOffset + 1), Proxy)
154+ deserializeAt initialOffset _ _ = pure ((initialOffset + 1), Proxy)
155
155
156− {-# INLINE serialize #-}
156+ {-# INLINE serializeAt #-}
157− serialize initialOffset _ _ = pure (initialOffset + 1)
157+ serializeAt initialOffset _ _ = pure (initialOffset + 1)
158
158
159--------------------------------------------------------------------------------
159--------------------------------------------------------------------------------
160-- Integer
160-- Integer
@@ -168,39 +168,39 @@ data LiftedInteger
168-- $(Serialize.deriveSerialize ''LiftedInteger)
168-- $(Serialize.deriveSerialize ''LiftedInteger)
169instance Serialize LiftedInteger where
169instance Serialize LiftedInteger where
170
170
171− {-# INLINE size #-}
171+ {-# INLINE addSizeTo #-}
172− size acc x =
172+ addSizeTo acc x =
173 case x of
173 case x of
174− LIS field0 -> (size (acc + 1)) field0
174+ LIS field0 -> (addSizeTo (acc + 1)) field0
175− LIP field0 -> (size (acc + 1)) field0
175+ LIP field0 -> (addSizeTo (acc + 1)) field0
176− LIN field0 -> (size (acc + 1)) field0
176+ LIN field0 -> (addSizeTo (acc + 1)) field0
177
177
178− {-# INLINE deserialize #-}
178+ {-# INLINE deserializeAt #-}
179− deserialize initialOffset arr endOffset = do
179+ deserializeAt initialOffset arr endOffset = do
180− (i0, tag) <- ((deserialize initialOffset) arr) endOffset
180+ (i0, tag) <- ((deserializeAt initialOffset) arr) endOffset
181 case tag :: Word8 of
181 case tag :: Word8 of
182− 0 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
182+ 0 -> do (i1, a0) <- ((deserializeAt i0) arr) endOffset
183 pure (i1, LIS a0)
183 pure (i1, LIS a0)
184− 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
184+ 1 -> do (i1, a0) <- ((deserializeAt i0) arr) endOffset
185 pure (i1, LIP a0)
185 pure (i1, LIP a0)
186− 2 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
186+ 2 -> do (i1, a0) <- ((deserializeAt i0) arr) endOffset
187 pure (i1, LIN a0)
187 pure (i1, LIN a0)
188 _ -> error "Found invalid tag while peeking (LiftedInteger)"
188 _ -> error "Found invalid tag while peeking (LiftedInteger)"
189
189
190− {-# INLINE serialize #-}
190+ {-# INLINE serializeAt #-}
191− serialize initialOffset arr val =
191+ serializeAt initialOffset arr val =
192 case val of
192 case val of
193 LIS field0 -> do
193 LIS field0 -> do
194− i0 <- ((serialize initialOffset) arr) (0 :: Word8)
194+ i0 <- ((serializeAt initialOffset) arr) (0 :: Word8)
195− i1 <- ((serialize i0) arr) field0
195+ i1 <- ((serializeAt i0) arr) field0
196 pure i1
196 pure i1
197 LIP field0 -> do
197 LIP field0 -> do
198− i0 <- ((serialize initialOffset) arr) (1 :: Word8)
198+ i0 <- ((serializeAt initialOffset) arr) (1 :: Word8)
199− i1 <- ((serialize i0) arr) field0
199+ i1 <- ((serializeAt i0) arr) field0
200 pure i1
200 pure i1
201 LIN field0 -> do
201 LIN field0 -> do
202− i0 <- ((serialize initialOffset) arr) (2 :: Word8)
202+ i0 <- ((serializeAt initialOffset) arr) (2 :: Word8)
203− i1 <- ((serialize i0) arr) field0
203+ i1 <- ((serializeAt i0) arr) field0
204 pure i1
204 pure i1
205
205
206#if __GLASGOW_HASKELL__ >= 900
206#if __GLASGOW_HASKELL__ >= 900
@@ -240,11 +240,12 @@ unliftInteger (LIN (Array (MutByteArray x) _ _)) =
240#endif
240#endif
241
241
242instance Serialize Integer where
242instance Serialize Integer where
243− {-# INLINE size #-}
243+ {-# INLINE addSizeTo #-}
244− size i a = size i (liftInteger a)
244+ addSizeTo i a = addSizeTo i (liftInteger a)
245
245
246− {-# INLINE deserialize #-}
246+ {-# INLINE deserializeAt #-}
247+ deserializeAt off arr end =
247− deserialize off arr end = fmap unliftInteger <$> deserialize off arr end
248+ fmap unliftInteger <$> deserializeAt off arr end
248
249
249− {-# INLINE serialize #-}
250+ {-# INLINE serializeAt #-}
250− serialize off arr val = serialize off arr (liftInteger val)
251+ serializeAt off arr val = serializeAt off arr (liftInteger val)
core/src/Streamly/Internal/Data/Serialize/TH.hs+16−16
@@ -50,7 +50,7 @@ import Streamly.Internal.Data.Serialize.TH.RecHeader
50--------------------------------------------------------------------------------
50--------------------------------------------------------------------------------
51
51
52exprGetSize :: Q Exp -> (Int, Type) -> Q Exp
52exprGetSize :: Q Exp -> (Int, Type) -> Q Exp
53−exprGetSize acc (i, _) = [|size $(acc) $(varE (mkFieldName i))|]
53+exprGetSize acc (i, _) = [|addSizeTo $(acc) $(varE (mkFieldName i))|]
54
54
55getTagSize :: Int -> Int
55getTagSize :: Int -> Int
56getTagSize numConstructors
56getTagSize numConstructors
@@ -170,9 +170,9 @@ mkSizeDec (SerializeConfig {..}) headTy cons = do
170 pure
170 pure
171 ( maybe
171 ( maybe
172 []
172 []
173− (\x -> [PragmaD (InlineP 'size x FunLike AllPhases)])
173+ (\x -> [PragmaD (InlineP 'addSizeTo x FunLike AllPhases)])
174 cfgInlineSize
174 cfgInlineSize
175− ++ [FunD 'size [Clause [] (NormalB sizeOfMethod) []]]
175+ ++ [FunD 'addSizeTo [Clause [] (NormalB sizeOfMethod) []]]
176 )
176 )
177
177
178--------------------------------------------------------------------------------
178--------------------------------------------------------------------------------
@@ -192,7 +192,7 @@ mkDeserializeExpr True False headTy tyOfTy =
192 conLen <- newName "conLen"
192 conLen <- newName "conLen"
193 off1 <- newName "off1"
193 off1 <- newName "off1"
194 [|do ($(varP off1), $(varP conLen) :: Word8) <-
194 [|do ($(varP off1), $(varP conLen) :: Word8) <-
195− deserialize
195+ deserializeAt
196 $(varE _initialOffset)
196 $(varE _initialOffset)
197 $(varE _arr)
197 $(varE _arr)
198 $(varE _endOffset)
198 $(varE _endOffset)
@@ -212,7 +212,7 @@ mkDeserializeExpr True False headTy tyOfTy =
212 [|($(litIntegral lenCname) == $(varE conLen))
212 [|($(litIntegral lenCname) == $(varE conLen))
213 && $(xorCmp tag off _arr)|]
213 && $(xorCmp tag off _arr)|]
214 [|let $(varP (makeI 0)) = $(varE off) + $(litIntegral lenCname)
214 [|let $(varP (makeI 0)) = $(varE off) + $(litIntegral lenCname)
215− in $(mkDeserializeExprOne 'deserialize con)|]
215+ in $(mkDeserializeExprOne 'deserializeAt con)|]
216
216
217mkDeserializeExpr False False headTy tyOfTy =
217mkDeserializeExpr False False headTy tyOfTy =
218 case tyOfTy of
218 case tyOfTy of
@@ -223,7 +223,7 @@ mkDeserializeExpr False False headTy tyOfTy =
223 TheType con ->
223 TheType con ->
224 letE
224 letE
225 [valD (varP (mkName "i0")) (normalB (varE _initialOffset)) []]
225 [valD (varP (mkName "i0")) (normalB (varE _initialOffset)) []]
226− (mkDeserializeExprOne 'deserialize con)
226+ (mkDeserializeExprOne 'deserializeAt con)
227 -- Sum type
227 -- Sum type
228 MultiType cons -> do
228 MultiType cons -> do
229 let lenCons = length cons
229 let lenCons = length cons
@@ -231,7 +231,7 @@ mkDeserializeExpr False False headTy tyOfTy =
231 doE
231 doE
232 [ bindS
232 [ bindS
233 (tupP [varP (mkName "i0"), varP _tag])
233 (tupP [varP (mkName "i0"), varP _tag])
234− [|deserialize $(varE _initialOffset) $(varE _arr) $(varE _endOffset)|]
234+ [|deserializeAt $(varE _initialOffset) $(varE _arr) $(varE _endOffset)|]
235 , noBindS
235 , noBindS
236 (caseE
236 (caseE
237 (sigE (varE _tag) (conT tagType))
237 (sigE (varE _tag) (conT tagType))
@@ -241,7 +241,7 @@ mkDeserializeExpr False False headTy tyOfTy =
241 peekMatch (i, con) =
241 peekMatch (i, con) =
242 match
242 match
243 (litP (IntegerL i))
243 (litP (IntegerL i))
244− (normalB (mkDeserializeExprOne 'deserialize con)) []
244+ (normalB (mkDeserializeExprOne 'deserializeAt con)) []
245 peekErr =
245 peekErr =
246 match
246 match
247 wildP
247 wildP
@@ -279,11 +279,11 @@ mkDeserializeDec (SerializeConfig {..}) headTy cons = do
279 pure
279 pure
280 ( maybe
280 ( maybe
281 []
281 []
282− (\x -> [PragmaD (InlineP 'deserialize x FunLike AllPhases)])
282+ (\x -> [PragmaD (InlineP 'deserializeAt x FunLike AllPhases)])
283 cfgInlineDeserialize
283 cfgInlineDeserialize
284 ++
284 ++
285 [ FunD
285 [ FunD
286− 'deserialize
286+ 'deserializeAt
287 [ Clause
287 [ Clause
288 (if isUnitType cons && not cfgConstructorTagAsString
288 (if isUnitType cons && not cfgConstructorTagAsString
289 then [VarP _initialOffset, WildP, WildP]
289 then [VarP _initialOffset, WildP, WildP]
@@ -300,7 +300,7 @@ mkDeserializeDec (SerializeConfig {..}) headTy cons = do
300
300
301mkSerializeExprTag :: Name -> Int -> Q Exp
301mkSerializeExprTag :: Name -> Int -> Q Exp
302mkSerializeExprTag tagType tagVal =
302mkSerializeExprTag tagType tagVal =
303− [|serialize
303+ [|serializeAt
304 $(varE _initialOffset)
304 $(varE _initialOffset)
305 $(varE _arr)
305 $(varE _arr)
306 $((sigE (litE (IntegerL (fromIntegral tagVal))) (conT tagType)))|]
306 $((sigE (litE (IntegerL (fromIntegral tagVal))) (conT tagType)))|]
@@ -335,7 +335,7 @@ mkSerializeExpr True False tyOfTy =
335 (doE [ bindS
335 (doE [ bindS
336 (varP (mkName "i0"))
336 (varP (mkName "i0"))
337 (serializeW8List _initialOffset _arr conEnc)
337 (serializeW8List _initialOffset _arr conEnc)
338− , noBindS (mkSerializeExprFields 'serialize fields)
338+ , noBindS (mkSerializeExprFields 'serializeAt fields)
339 ])
339 ])
340
340
341mkSerializeExpr False False tyOfTy =
341mkSerializeExpr False False tyOfTy =
@@ -351,7 +351,7 @@ mkSerializeExpr False False tyOfTy =
351 [ matchConstructor
351 [ matchConstructor
352 cname
352 cname
353 (length fields)
353 (length fields)
354− (mkSerializeExprFields 'serialize fields)
354+ (mkSerializeExprFields 'serializeAt fields)
355 ])
355 ])
356 -- Sum type
356 -- Sum type
357 (MultiType cons) -> do
357 (MultiType cons) -> do
@@ -368,7 +368,7 @@ mkSerializeExpr False False tyOfTy =
368 (mkSerializeExprTag tagType tagVal)
368 (mkSerializeExprTag tagType tagVal)
369 , noBindS
369 , noBindS
370 (mkSerializeExprFields
370 (mkSerializeExprFields
371− 'serialize
371+ 'serializeAt
372 fields)
372 fields)
373 ]))
373 ]))
374 (zip [0 ..] cons))
374 (zip [0 ..] cons))
@@ -388,11 +388,11 @@ mkSerializeDec (SerializeConfig {..}) headTy cons = do
388 pure
388 pure
389 ( maybe
389 ( maybe
390 []
390 []
391− (\x -> [PragmaD (InlineP 'serialize x FunLike AllPhases)])
391+ (\x -> [PragmaD (InlineP 'serializeAt x FunLike AllPhases)])
392 cfgInlineSerialize
392 cfgInlineSerialize
393 ++
393 ++
394 [FunD
394 [FunD
395− 'serialize
395+ 'serializeAt
396 [ Clause
396 [ Clause
397 (if isUnitType cons && not cfgConstructorTagAsString
397 (if isUnitType cons && not cfgConstructorTagAsString
398 then [VarP _initialOffset, WildP, WildP]
398 then [VarP _initialOffset, WildP, WildP]
core/src/Streamly/Internal/Data/Serialize/TH/Bottom.hs+17−17
@@ -13,9 +13,9 @@ module Streamly.Internal.Data.Serialize.TH.Bottom
13 -- ** Config
13 -- ** Config
14 SerializeConfig(..)
14 SerializeConfig(..)
15 , serializeConfig
15 , serializeConfig
16− , inlineSize
16+ , inlineAddSizeTo
17− , inlineSerialize
17+ , inlineSerializeAt
18− , inlineDeserialize
18+ , inlineDeserializeAt
19 , encodeConstrNames
19 , encodeConstrNames
20 , encodeRecordFields
20 , encodeRecordFields
21
21
@@ -84,7 +84,7 @@ import Streamly.Internal.Data.Unbox.TH (DataCon(..))
84-- 'serializeConfig' and config setter functions to generate desired Config. For
84-- 'serializeConfig' and config setter functions to generate desired Config. For
85-- example:
85-- example:
86--
86--
87−-- >>> (inlineSize (Just Inline)) . (inlineSerialize (Just Inlinable)) serializeConfig
87+-- >>> (inlineAddSizeTo (Just Inline)) . (inlineSerializeAt (Just Inlinable)) serializeConfig
88--
88--
89data SerializeConfig =
89data SerializeConfig =
90 SerializeConfig
90 SerializeConfig
@@ -95,11 +95,11 @@ data SerializeConfig =
95 , cfgRecordSyntaxWithHeader :: Bool
95 , cfgRecordSyntaxWithHeader :: Bool
96 }
96 }
97
97
98−-- | How should we inline the 'size' function? The default in 'serializeConfig'
98+-- | How should we inline the 'addSizeTo' function? The default in 'serializeConfig'
99−-- is 'Nothing' which means left to the compiler. Forcing inline on @size@
99+-- is 'Nothing' which means left to the compiler. Forcing inline on @addSizeTo@
100-- function actually worsens some benchmarks and improves none.
100-- function actually worsens some benchmarks and improves none.
101−inlineSize :: Maybe Inline -> SerializeConfig -> SerializeConfig
101+inlineAddSizeTo :: Maybe Inline -> SerializeConfig -> SerializeConfig
102−inlineSize v cfg = cfg {cfgInlineSize = v}
102+inlineAddSizeTo v cfg = cfg {cfgInlineSize = v}
103
103
104-- XXX Should we make the default Inlinable instead?
104-- XXX Should we make the default Inlinable instead?
105
105
@@ -108,15 +108,15 @@ inlineSize v cfg = cfg {cfgInlineSize = v}
108-- the code and increase in compilation times when there are big functions and
108-- the code and increase in compilation times when there are big functions and
109-- too many nesting levels so you can change it accordingly. A 'Nothing' value
109-- too many nesting levels so you can change it accordingly. A 'Nothing' value
110-- leaves the decision to the compiler.
110-- leaves the decision to the compiler.
111−inlineSerialize :: Maybe Inline -> SerializeConfig -> SerializeConfig
111+inlineSerializeAt :: Maybe Inline -> SerializeConfig -> SerializeConfig
112−inlineSerialize v cfg = cfg {cfgInlineSerialize = v}
112+inlineSerializeAt v cfg = cfg {cfgInlineSerialize = v}
113
113
114-- XXX Should we make the default Inlinable instead?
114-- XXX Should we make the default Inlinable instead?
115
115
116-- | How should we inline the 'deserialize' function? See guidelines in
116-- | How should we inline the 'deserialize' function? See guidelines in
117−-- 'inlineSerialize'.
117+-- 'inlineSerializeAt'.
118−inlineDeserialize :: Maybe Inline -> SerializeConfig -> SerializeConfig
118+inlineDeserializeAt :: Maybe Inline -> SerializeConfig -> SerializeConfig
119−inlineDeserialize v cfg = cfg {cfgInlineDeserialize = v}
119+inlineDeserializeAt v cfg = cfg {cfgInlineDeserialize = v}
120
120
121-- | __Experimental__
121-- | __Experimental__
122--
122--
@@ -184,9 +184,9 @@ encodeRecordFields v cfg = cfg {cfgRecordSyntaxWithHeader = v}
184
184
185-- | The default configuration settings are:
185-- | The default configuration settings are:
186--
186--
187−-- * 'inlineSize' 'Nothing'
187+-- * 'inlineAddSizeTo' 'Nothing'
188−-- * 'inlineSerialize' (Just Inline)
188+-- * 'inlineSerializeAt' (Just Inline)
189−-- * 'inlineDeserialize' (Just Inline)
189+-- * 'inlineDeserializeAt' (Just Inline)
190--
190--
191serializeConfig :: SerializeConfig
191serializeConfig :: SerializeConfig
192serializeConfig =
192serializeConfig =
@@ -425,7 +425,7 @@ serializeW8List off arr w8List = do
425 makeBind i =
425 makeBind i =
426 bindS
426 bindS
427 (varP (makeN (i + 1)))
427 (varP (makeN (i + 1)))
428− [|$(varE 'serialize)
428+ [|$(varE 'serializeAt)
429 $(varE (makeN i))
429 $(varE (makeN i))
430 $(varE arr)
430 $(varE arr)
431 ($(litIntegral (w8List !! i)) :: Word8)|]
431 ($(litIntegral (w8List !! i)) :: Word8)|]
core/src/Streamly/Internal/Data/Serialize/TH/RecHeader.hs+23−23
@@ -103,36 +103,36 @@ newtype CompactList a =
103-- the list is 255.
103-- the list is 255.
104instance forall a. Serialize a => Serialize (CompactList a) where
104instance forall a. Serialize a => Serialize (CompactList a) where
105
105
106− -- {-# INLINE size #-}
106+ -- {-# INLINE addSizeTo #-}
107− size acc (CompactList xs) =
107+ addSizeTo acc (CompactList xs) =
108− foldl' size (acc + (Unbox.sizeOf (Proxy :: Proxy Word8))) xs
108+ foldl' addSizeTo (acc + (Unbox.sizeOf (Proxy :: Proxy Word8))) xs
109
109
110 -- Inlining this causes large compilation times for tests
110 -- Inlining this causes large compilation times for tests
111− {-# INLINABLE deserialize #-}
111+ {-# INLINABLE deserializeAt #-}
112− deserialize off arr sz = do
112+ deserializeAt off arr sz = do
113− (off1, len8) <- deserialize off arr sz :: IO (Int, Word8)
113+ (off1, len8) <- deserializeAt off arr sz :: IO (Int, Word8)
114 let len = w8_int len8
114 let len = w8_int len8
115 peekList f o i | i >= 3 = do
115 peekList f o i | i >= 3 = do
116 -- Unfold the loop three times
116 -- Unfold the loop three times
117− (o1, x1) <- deserialize o arr sz
117+ (o1, x1) <- deserializeAt o arr sz
118− (o2, x2) <- deserialize o1 arr sz
118+ (o2, x2) <- deserializeAt o1 arr sz
119− (o3, x3) <- deserialize o2 arr sz
119+ (o3, x3) <- deserializeAt o2 arr sz
120 peekList (f . (\xs -> x1:x2:x3:xs)) o3 (i - 3)
120 peekList (f . (\xs -> x1:x2:x3:xs)) o3 (i - 3)
121 peekList f o 0 = pure (o, f [])
121 peekList f o 0 = pure (o, f [])
122 peekList f o i = do
122 peekList f o i = do
123− (o1, x) <- deserialize o arr sz
123+ (o1, x) <- deserializeAt o arr sz
124 peekList (f . (x:)) o1 (i - 1)
124 peekList (f . (x:)) o1 (i - 1)
125 (nextOff, lst) <- peekList id off1 len
125 (nextOff, lst) <- peekList id off1 len
126 pure (nextOff, CompactList lst)
126 pure (nextOff, CompactList lst)
127
127
128 -- Inlining this causes large compilation times for tests
128 -- Inlining this causes large compilation times for tests
129− {-# INLINABLE serialize #-}
129+ {-# INLINABLE serializeAt #-}
130− serialize off arr (CompactList val) = do
130+ serializeAt off arr (CompactList val) = do
131− void $ serialize off arr (int_w8 (length val) :: Word8)
131+ void $ serializeAt off arr (int_w8 (length val) :: Word8)
132 let off1 = off + Unbox.sizeOf (Proxy :: Proxy Word8)
132 let off1 = off + Unbox.sizeOf (Proxy :: Proxy Word8)
133 let pokeList o [] = pure o
133 let pokeList o [] = pure o
134 pokeList o (x:xs) = do
134 pokeList o (x:xs) = do
135− o1 <- serialize o arr x
135+ o1 <- serializeAt o arr x
136 pokeList o1 xs
136 pokeList o1 xs
137 pokeList off1 val
137 pokeList off1 val
138
138
@@ -154,7 +154,7 @@ isMaybeType _ = False
154-- We add 4 here because we use 'serializeWithSize' for serializing.
154-- We add 4 here because we use 'serializeWithSize' for serializing.
155exprGetSize :: Q Exp -> (Int, Type) -> Q Exp
155exprGetSize :: Q Exp -> (Int, Type) -> Q Exp
156exprGetSize acc (i, _) =
156exprGetSize acc (i, _) =
157− [|size $(acc) $(varE (mkFieldName i)) + 4|]
157+ [|addSizeTo $(acc) $(varE (mkFieldName i)) + 4|]
158
158
159sizeOfHeader :: SimpleDataCon -> Int
159sizeOfHeader :: SimpleDataCon -> Int
160sizeOfHeader (SimpleDataCon _ fields) =
160sizeOfHeader (SimpleDataCon _ fields) =
@@ -227,7 +227,7 @@ headerValue (SimpleDataCon _ fields) =
227{-# INLINE serializeWithSize #-}
227{-# INLINE serializeWithSize #-}
228serializeWithSize :: Serialize a => Int -> MutByteArray -> a -> IO Int
228serializeWithSize :: Serialize a => Int -> MutByteArray -> a -> IO Int
229serializeWithSize off arr val = do
229serializeWithSize off arr val = do
230− off1 <- serialize (off + 4) arr val
230+ off1 <- serializeAt (off + 4) arr val
231 Unbox.pokeAt off arr (int_w32 (off1 - off - 4) :: Word32)
231 Unbox.pokeAt off arr (int_w32 (off1 - off - 4) :: Word32)
232 pure off1
232 pure off1
233
233
@@ -238,7 +238,7 @@ mkRecSerializeExpr initialOffset (con@(SimpleDataCon cname fields)) = do
238 -- We first compare the header length encoded and the current header
238 -- We first compare the header length encoded and the current header
239 -- length. Only if the header lengths match, we compare the headers.
239 -- length. Only if the header lengths match, we compare the headers.
240 [|do $(varP afterHLen) <-
240 [|do $(varP afterHLen) <-
241− serialize
241+ serializeAt
242 ($(varE initialOffset) + 4)
242 ($(varE initialOffset) + 4)
243 $(varE _arr)
243 $(varE _arr)
244 ($(litIntegral hlen) :: Word32)
244 ($(litIntegral hlen) :: Word32)
@@ -264,7 +264,7 @@ mkRecSerializeExpr initialOffset (con@(SimpleDataCon cname fields)) = do
264{-# INLINE deserializeWithSize #-}
264{-# INLINE deserializeWithSize #-}
265deserializeWithSize ::
265deserializeWithSize ::
266 Serialize a => Int -> MutByteArray -> Int -> IO (Int, a)
266 Serialize a => Int -> MutByteArray -> Int -> IO (Int, a)
267−deserializeWithSize off arr endOff = deserialize (off + 4) arr endOff
267+deserializeWithSize off arr endOff = deserializeAt (off + 4) arr endOff
268
268
269conUpdateFuncDec :: Name -> [Field] -> Q [Dec]
269conUpdateFuncDec :: Name -> [Field] -> Q [Dec]
270conUpdateFuncDec funcName fields = do
270conUpdateFuncDec funcName fields = do
@@ -282,7 +282,7 @@ conUpdateFuncDec funcName fields = do
282 wildP
282 wildP
283 (normalB
283 (normalB
284 [|do (valOff, valLen :: Word32) <-
284 [|do (valOff, valLen :: Word32) <-
285− deserialize
285+ deserializeAt
286 $(varE curOff)
286 $(varE curOff)
287 $(varE arr)
287 $(varE arr)
288 $(varE endOff)
288 $(varE endOff)
@@ -317,7 +317,7 @@ conUpdateFuncDec funcName fields = do
317 (litP fnameLit)
317 (litP fnameLit)
318 (normalB
318 (normalB
319 [|do (valOff, valLen :: Word32) <-
319 [|do (valOff, valLen :: Word32) <-
320− deserialize
320+ deserializeAt
321 $(varE currOff)
321 $(varE currOff)
322 $(varE arr)
322 $(varE arr)
323 $(varE endOff)
323 $(varE endOff)
@@ -345,7 +345,7 @@ mkDeserializeKeysDec funcName updateFunc (SimpleDataCon cname fields) = do
345 errorUnsupported "The datatype should use record syntax."
345 errorUnsupported "The datatype should use record syntax."
346 method <-
346 method <-
347 [|do (dataOff, hlist :: CompactList (CompactList Word8)) <-
347 [|do (dataOff, hlist :: CompactList (CompactList Word8)) <-
348− deserialize $(varE hOff) $(varE arr) $(varE endOff)
348+ deserializeAt $(varE hOff) $(varE arr) $(varE endOff)
349 let keys = wListToString . unCompactList <$> unCompactList hlist
349 let keys = wListToString . unCompactList <$> unCompactList hlist
350 ($(varP kvEncoded), _) <-
350 ($(varP kvEncoded), _) <-
351 foldlM
351 foldlM
@@ -390,9 +390,9 @@ mkRecDeserializeExpr initialOff endOff deserializeWithKeys con = do
390 sizeForHeaderLength = 4 -- Word32
390 sizeForHeaderLength = 4 -- Word32
391 sizePreData = sizeForFinalOff + sizeForHeaderLength + hlen
391 sizePreData = sizeForFinalOff + sizeForHeaderLength + hlen
392 [|do (hlenOff, encLen :: Word32) <-
392 [|do (hlenOff, encLen :: Word32) <-
393− deserialize $(varE initialOff) $(varE _arr) $(varE endOff)
393+ deserializeAt $(varE initialOff) $(varE _arr) $(varE endOff)
394 ($(varP hOff), hlen1 :: Word32) <-
394 ($(varP hOff), hlen1 :: Word32) <-
395− deserialize hlenOff $(varE _arr) $(varE endOff)
395+ deserializeAt hlenOff $(varE _arr) $(varE endOff)
396 if (hlen1 == $(litIntegral hlen)) && $(xorCmp hval hOff _arr)
396 if (hlen1 == $(litIntegral hlen)) && $(xorCmp hval hOff _arr)
397 then do
397 then do
398 let $(varP (makeI 0)) =
398 let $(varP (makeI 0)) =
core/src/Streamly/Internal/Data/Serialize/Type.hs+58−57
@@ -58,14 +58,14 @@ import GHC.Exts
58-- particular constructor. For variable length data types the length is encoded
58-- particular constructor. For variable length data types the length is encoded
59-- along with the data.
59-- along with the data.
60--
60--
61−-- The 'deserialize' operation reads bytes from the mutable byte array and
61+-- The 'deserializeAt' operation reads bytes from the mutable byte array and
62-- builds a Haskell data type from these bytes, the number of bytes it reads
62-- builds a Haskell data type from these bytes, the number of bytes it reads
63−-- depends on the type and the encoded value it is reading. 'serialize'
63+-- depends on the type and the encoded value it is reading. 'serializeAt'
64-- operation converts a Haskell data type to its binary representation which
64-- operation converts a Haskell data type to its binary representation which
65−-- must consist of as many bytes as returned by the @size@ operation on that
65+-- must consist of as many bytes as added by the @addSizeTo@ operation for that
66-- value and then stores these bytes into the mutable byte array. The
66-- value and then stores these bytes into the mutable byte array. The
67−-- programmer is expected to use the @size@ operation and allocate an array of
67+-- programmer is expected to use the @addSizeTo@ operation and allocate an
68−-- sufficient length before calling 'serialize'.
68+-- array of sufficient length before calling 'serializeAt'.
69--
69--
70-- IMPORTANT: The serialized data's byte ordering remains the same as the host
70-- IMPORTANT: The serialized data's byte ordering remains the same as the host
71-- machine's byte order. Therefore, it can not be deserialized from host
71-- machine's byte order. Therefore, it can not be deserialized from host
@@ -98,15 +98,15 @@ import GHC.Exts
98--
98--
99-- >>> :{
99-- >>> :{
100-- instance Serialize Object where
100-- instance Serialize Object where
101−-- size acc obj = size (size acc (_obj1 obj)) (_obj2 obj)
101+-- addSizeTo acc obj = addSizeTo (addSizeTo acc (_obj1 obj)) (_obj2 obj)
102−-- deserialize i arr len = do
102+-- deserializeAt i arr len = do
103-- -- Check the array bounds before reading
103-- -- Check the array bounds before reading
104−-- (i1, x0) <- deserialize i arr len
104+-- (i1, x0) <- deserializeAt i arr len
105−-- (i2, x1) <- deserialize i1 arr len
105+-- (i2, x1) <- deserializeAt i1 arr len
106-- pure (i2, Object x0 x1)
106-- pure (i2, Object x0 x1)
107−-- serialize i arr (Object x0 x1) = do
107+-- serializeAt i arr (Object x0 x1) = do
108−-- i1 <- serialize i arr x0
108+-- i1 <- serializeAt i arr x0
109−-- i2 <- serialize i1 arr x1
109+-- i2 <- serializeAt i1 arr x1
110-- pure i2
110-- pure i2
111-- :}
111-- :}
112--
112--
@@ -117,30 +117,30 @@ class Serialize a where
117 -- It is of the form @Int -> a -> Int@ because you can have tail-recursive
117 -- It is of the form @Int -> a -> Int@ because you can have tail-recursive
118 -- traversal of the structures.
118 -- traversal of the structures.
119
119
120− -- | @size accum value@ returns @accum@ incremented by the size of the
120+ -- | @addSizeTo accum value@ returns @accum@ incremented by the size of the
121 -- serialized representation of @value@ in bytes. Size cannot be zero. It
121 -- serialized representation of @value@ in bytes. Size cannot be zero. It
122 -- should be at least 1 byte.
122 -- should be at least 1 byte.
123− size :: Int -> a -> Int
123+ addSizeTo :: Int -> a -> Int
124
124
125 -- We can implement the following functions without returning the `Int`
125 -- We can implement the following functions without returning the `Int`
126 -- offset but that may require traversing the Haskell structure again to get
126 -- offset but that may require traversing the Haskell structure again to get
127 -- the size. Therefore, this is a performance optimization.
127 -- the size. Therefore, this is a performance optimization.
128
128
129− -- | @deserialize byte-offset array arrayLen@ deserializes a value from the
129+ -- | @deserializeAt byte-offset array arrayLen@ deserializes a value from the
130 -- given byte-offset in the array. Returns a tuple consisting of the next
130 -- given byte-offset in the array. Returns a tuple consisting of the next
131 -- byte-offset and the deserialized value.
131 -- byte-offset and the deserialized value.
132 --
132 --
133 -- Throws an exception if the operation would exceed the supplied arrayLen.
133 -- Throws an exception if the operation would exceed the supplied arrayLen.
134− deserialize :: Int -> MutByteArray -> Int -> IO (Int, a)
134+ deserializeAt :: Int -> MutByteArray -> Int -> IO (Int, a)
135
135
136− -- | @serialize byte-offset array value@ writes the serialized
136+ -- | @serializeAt byte-offset array value@ writes the serialized
137 -- representation of the @value@ in the array at the given byte-offset.
137 -- representation of the @value@ in the array at the given byte-offset.
138 -- Returns the next byte-offset.
138 -- Returns the next byte-offset.
139 --
139 --
140 -- This is an unsafe operation, the programmer must ensure that the array
140 -- This is an unsafe operation, the programmer must ensure that the array
141 -- has enough space available to serialize the value as determined by the
141 -- has enough space available to serialize the value as determined by the
142− -- @size@ operation.
142+ -- @addSizeTo@ operation.
143− serialize :: Int -> MutByteArray -> a -> IO Int
143+ serializeAt :: Int -> MutByteArray -> a -> IO Int
144
144
145--------------------------------------------------------------------------------
145--------------------------------------------------------------------------------
146-- Instances
146-- Instances
@@ -191,7 +191,7 @@ deserializeUnsafe off arr sz =
191 if (next <= sz)
191 if (next <= sz)
192 then Unbox.peekAt off arr >>= \val -> pure (next, val)
192 then Unbox.peekAt off arr >>= \val -> pure (next, val)
193 else error
193 else error
194− $ "deserialize: accessing array at offset = "
194+ $ "deserializeAt: accessing array at offset = "
195 ++ show (next - 1)
195 ++ show (next - 1)
196 ++ " max valid offset = " ++ show (sz - 1)
196 ++ " max valid offset = " ++ show (sz - 1)
197
197
@@ -201,19 +201,19 @@ serializeUnsafe off arr val =
201 let next = off + Unbox.sizeOf (Proxy :: Proxy a)
201 let next = off + Unbox.sizeOf (Proxy :: Proxy a)
202 in do
202 in do
203#ifdef DEBUG
203#ifdef DEBUG
204− checkBounds "serialize" next arr
204+ checkBounds "serializeAt" next arr
205#endif
205#endif
206 Unbox.pokeAt off arr val
206 Unbox.pokeAt off arr val
207 pure next
207 pure next
208
208
209#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
209#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
210instance Serialize _type where \
210instance Serialize _type where \
211−; {-# INLINE size #-} \
211+; {-# INLINE addSizeTo #-} \
212−; size acc _ = acc + Unbox.sizeOf (Proxy :: Proxy _type) \
212+; addSizeTo acc _ = acc + Unbox.sizeOf (Proxy :: Proxy _type) \
213−; {-# INLINE deserialize #-} \
213+; {-# INLINE deserializeAt #-} \
214−; deserialize off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
214+; deserializeAt off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
215−; {-# INLINE serialize #-} \
215+; {-# INLINE serializeAt #-} \
216−; serialize = \
216+; serializeAt = \
217 serializeUnsafe :: Int -> MutByteArray -> _type -> IO Int
217 serializeUnsafe :: Int -> MutByteArray -> _type -> IO Int
218
218
219DERIVE_SERIALIZE_FROM_UNBOX(())
219DERIVE_SERIALIZE_FROM_UNBOX(())
@@ -237,68 +237,69 @@ DERIVE_SERIALIZE_FROM_UNBOX((FunPtr a))
237
237
238instance forall a. Serialize a => Serialize [a] where
238instance forall a. Serialize a => Serialize [a] where
239
239
240− -- {-# INLINE size #-}
240+ -- {-# INLINE addSizeTo #-}
241+ addSizeTo acc xs =
241− size acc xs = foldl' size (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
242+ foldl' addSizeTo (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
242
243
243 -- Inlining this causes large compilation times for tests
244 -- Inlining this causes large compilation times for tests
244− {-# INLINABLE deserialize #-}
245+ {-# INLINABLE deserializeAt #-}
245− deserialize off arr sz = do
246+ deserializeAt off arr sz = do
246− (off1, len64) <- deserialize off arr sz :: IO (Int, Int64)
247+ (off1, len64) <- deserializeAt off arr sz :: IO (Int, Int64)
247 let len = (fromIntegral :: Int64 -> Int) len64
248 let len = (fromIntegral :: Int64 -> Int) len64
248 peekList f o i | i >= 3 = do
249 peekList f o i | i >= 3 = do
249 -- Unfold the loop three times
250 -- Unfold the loop three times
250− (o1, x1) <- deserialize o arr sz
251+ (o1, x1) <- deserializeAt o arr sz
251− (o2, x2) <- deserialize o1 arr sz
252+ (o2, x2) <- deserializeAt o1 arr sz
252− (o3, x3) <- deserialize o2 arr sz
253+ (o3, x3) <- deserializeAt o2 arr sz
253 peekList (f . (\xs -> x1:x2:x3:xs)) o3 (i - 3)
254 peekList (f . (\xs -> x1:x2:x3:xs)) o3 (i - 3)
254 peekList f o 0 = pure (o, f [])
255 peekList f o 0 = pure (o, f [])
255 peekList f o i = do
256 peekList f o i = do
256− (o1, x) <- deserialize o arr sz
257+ (o1, x) <- deserializeAt o arr sz
257 peekList (f . (x:)) o1 (i - 1)
258 peekList (f . (x:)) o1 (i - 1)
258 peekList id off1 len
259 peekList id off1 len
259
260
260 -- Inlining this causes large compilation times for tests
261 -- Inlining this causes large compilation times for tests
261− {-# INLINABLE serialize #-}
262+ {-# INLINABLE serializeAt #-}
262− serialize off arr val = do
263+ serializeAt off arr val = do
263 let off1 = off + Unbox.sizeOf (Proxy :: Proxy Int64)
264 let off1 = off + Unbox.sizeOf (Proxy :: Proxy Int64)
264 let pokeList acc o [] =
265 let pokeList acc o [] =
265 Unbox.pokeAt off arr (acc :: Int64) >> pure o
266 Unbox.pokeAt off arr (acc :: Int64) >> pure o
266 pokeList acc o (x:xs) = do
267 pokeList acc o (x:xs) = do
267− o1 <- serialize o arr x
268+ o1 <- serializeAt o arr x
268 pokeList (acc + 1) o1 xs
269 pokeList (acc + 1) o1 xs
269 pokeList 0 off1 val
270 pokeList 0 off1 val
270
271
271instance Serialize (Array a) where
272instance Serialize (Array a) where
272− {-# INLINE size #-}
273+ {-# INLINE addSizeTo #-}
273− size i (Array {..}) = i + (arrEnd - arrStart) + 8
274+ addSizeTo i (Array {..}) = i + (arrEnd - arrStart) + 8
274
275
275− {-# INLINE deserialize #-}
276+ {-# INLINE deserializeAt #-}
276− deserialize off arr end = do
277+ deserializeAt off arr end = do
277− (off1, byteLen) <- deserialize off arr end :: IO (Int, Int)
278+ (off1, byteLen) <- deserializeAt off arr end :: IO (Int, Int)
278 let off2 = off1 + byteLen
279 let off2 = off1 + byteLen
279 let slice = MutArray.MutArray arr off1 off2 off2
280 let slice = MutArray.MutArray arr off1 off2 off2
280 newArr <- MutArray.clone slice
281 newArr <- MutArray.clone slice
281 pure (off2, Array.unsafeFreeze newArr)
282 pure (off2, Array.unsafeFreeze newArr)
282
283
283− {-# INLINE serialize #-}
284+ {-# INLINE serializeAt #-}
284− serialize off arr (Array {..}) = do
285+ serializeAt off arr (Array {..}) = do
285 let arrLen = arrEnd - arrStart
286 let arrLen = arrEnd - arrStart
286− off1 <- serialize off arr arrLen
287+ off1 <- serializeAt off arr arrLen
287 MBA.putSliceUnsafe arrContents arrStart arr off1 arrLen
288 MBA.putSliceUnsafe arrContents arrStart arr off1 arrLen
288 pure (off1 + arrLen)
289 pure (off1 + arrLen)
289
290
290instance (Serialize a, Serialize b) => Serialize (a, b) where
291instance (Serialize a, Serialize b) => Serialize (a, b) where
291
292
292− {-# INLINE size #-}
293+ {-# INLINE addSizeTo #-}
293− size acc (a, b) = size (size acc a) b
294+ addSizeTo acc (a, b) = addSizeTo (addSizeTo acc a) b
294
295
295− {-# INLINE serialize #-}
296+ {-# INLINE serializeAt #-}
296− serialize off arr (a, b) = do
297+ serializeAt off arr (a, b) = do
297− off1 <- serialize off arr a
298+ off1 <- serializeAt off arr a
298− serialize off1 arr b
299+ serializeAt off1 arr b
299
300
300− {-# INLINE deserialize #-}
301+ {-# INLINE deserializeAt #-}
301− deserialize off arr end = do
302+ deserializeAt off arr end = do
302− (off1, a) <- deserialize off arr end
303+ (off1, a) <- deserializeAt off arr end
303− (off2, b) <- deserialize off1 arr end
304+ (off2, b) <- deserializeAt off1 arr end
304 pure (off2, (a, b))
305 pure (off2, (a, b))
test/Streamly/Test/Data/Serialize.hs+4−4
@@ -160,7 +160,7 @@ poke ::
160 => a
160 => a
161 -> IO (MutByteArray, Int, Int)
161 -> IO (MutByteArray, Int, Int)
162poke val = do
162poke val = do
163− let sz = Serialize.size 0 val
163+ let sz = Serialize.addSizeTo 0 val
164
164
165 let excessSize = 100
165 let excessSize = 100
166 randomOff <- randomRIO (10, excessSize)
166 randomOff <- randomRIO (10, excessSize)
@@ -171,7 +171,7 @@ poke val = do
171 serEndOff = randomOff + sz
171 serEndOff = randomOff + sz
172 arr <- Serialize.new arrSize
172 arr <- Serialize.new arrSize
173
173
174− off1 <- Serialize.serialize serStartOff arr val
174+ off1 <- Serialize.serializeAt serStartOff arr val
175 off1 `shouldBe` serEndOff
175 off1 `shouldBe` serEndOff
176 pure (arr, serStartOff, serEndOff)
176 pure (arr, serStartOff, serEndOff)
177
177
@@ -181,7 +181,7 @@ peekAndVerify ::
181 -> a
181 -> a
182 -> IO ()
182 -> IO ()
183peekAndVerify (arr, serStartOff, serEndOff) val = do
183peekAndVerify (arr, serStartOff, serEndOff) val = do
184− (off2, val2) <- Serialize.deserialize serStartOff arr serEndOff
184+ (off2, val2) <- Serialize.deserializeAt serStartOff arr serEndOff
185 val2 `shouldBe` val
185 val2 `shouldBe` val
186 off2 `shouldBe` serEndOff
186 off2 `shouldBe` serEndOff
187 let slice = Array.Array arr serStartOff serEndOff
187 let slice = Array.Array arr serStartOff serEndOff
@@ -207,7 +207,7 @@ testSerializeList
207 -> IO ()
207 -> IO ()
208testSerializeList sizeOfA val = do
208testSerializeList sizeOfA val = do
209
209
210− let sz = Serialize.size 0 val
210+ let sz = Serialize.addSizeTo 0 val
211
211
212 sz `shouldBe` sizeOfA
212 sz `shouldBe` sizeOfA
213
213
test/Streamly/Test/Data/Unbox.hs+3−3
@@ -40,8 +40,8 @@ import Test.Hspec as H
40
40
41#define MODULE_NAME "Data.Serialize.Deriving.TH"
41#define MODULE_NAME "Data.Serialize.Deriving.TH"
42#define DERIVE_UNBOX(typ) $(deriveSerialize [d|instance Serialize typ|])
42#define DERIVE_UNBOX(typ) $(deriveSerialize [d|instance Serialize typ|])
43−#define PEEK(i, arr, sz) (deserialize i arr sz)
43+#define PEEK(i, arr, sz) (deserializeAt i arr sz)
44−#define POKE(i, arr, val) (serialize i arr val)
44+#define POKE(i, arr, val) (serializeAt i arr val)
45#define TYPE_CLASS Serialize
45#define TYPE_CLASS Serialize
46
46
47#else
47#else
@@ -185,7 +185,7 @@ variableSizeOf ::
185 forall a. Serialize a
185 forall a. Serialize a
186 => a
186 => a
187 -> Int
187 -> Int
188−variableSizeOf = size 0
188+variableSizeOf = addSizeTo 0
189#endif
189#endif
190
190
191testSerialization ::
191testSerialization ::