@@ -58,14 +58,14 @@ import GHC.Exts
5858-- particular constructor. For variable length data types the length is encoded
5959-- along with the data.
6060--
61−-- The 'deserialize' operation reads bytes from the mutable byte array and
61+-- The 'deserializeAt' operation reads bytes from the mutable byte array and
6262-- 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'
6464-- 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
6666-- 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
68−-- sufficient length before calling 'serialize'.
67+-- programmer is expected to use the @addSizeTo@ operation and allocate an
68+-- array of sufficient length before calling 'serializeAt'.
6969--
7070-- IMPORTANT: The serialized data's byte ordering remains the same as the host
7171-- machine's byte order. Therefore, it can not be deserialized from host
@@ -98,15 +98,15 @@ import GHC.Exts
9898--
9999-- >>> :{
100100-- instance Serialize Object where
101−-- size acc obj = size (size acc (_obj1 obj)) (_obj2 obj)
102−-- deserialize i arr len = do
101+-- addSizeTo acc obj = addSizeTo (addSizeTo acc (_obj1 obj)) (_obj2 obj)
102+-- deserializeAt i arr len = do
103103-- -- Check the array bounds before reading
104−-- (i1, x0) <- deserialize i arr len
105−-- (i2, x1) <- deserialize i1 arr len
104+-- (i1, x0) <- deserializeAt i arr len
105+-- (i2, x1) <- deserializeAt i1 arr len
106106-- pure (i2, Object x0 x1)
107−-- serialize i arr (Object x0 x1) = do
108−-- i1 <- serialize i arr x0
109−-- i2 <- serialize i1 arr x1
107+-- serializeAt i arr (Object x0 x1) = do
108+-- i1 <- serializeAt i arr x0
109+-- i2 <- serializeAt i1 arr x1
110110-- pure i2
111111-- :}
112112--
@@ -117,30 +117,30 @@ class Serialize a where
117117 -- It is of the form @Int -> a -> Int@ because you can have tail-recursive
118118 -- traversal of the structures.
119119
120− -- | @size accum value@ returns @accum@ incremented by the size of the
120+ -- | @addSizeTo accum value@ returns @accum@ incremented by the size of the
121121 -- serialized representation of @value@ in bytes. Size cannot be zero. It
122122 -- should be at least 1 byte.
123− size :: Int -> a -> Int
123+ addSizeTo :: Int -> a -> Int
124124
125125 -- We can implement the following functions without returning the `Int`
126126 -- offset but that may require traversing the Haskell structure again to get
127127 -- the size. Therefore, this is a performance optimization.
128128
129− -- | @deserialize byte-offset array arrayLen@ deserializes a value from the
129+ -- | @deserializeAt byte-offset array arrayLen@ deserializes a value from the
130130 -- given byte-offset in the array. Returns a tuple consisting of the next
131131 -- byte-offset and the deserialized value.
132132 --
133133 -- 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)
135135
136− -- | @serialize byte-offset array value@ writes the serialized
136+ -- | @serializeAt byte-offset array value@ writes the serialized
137137 -- representation of the @value@ in the array at the given byte-offset.
138138 -- Returns the next byte-offset.
139139 --
140140 -- This is an unsafe operation, the programmer must ensure that the array
141141 -- has enough space available to serialize the value as determined by the
142− -- @size@ operation.
143− serialize :: Int -> MutByteArray -> a -> IO Int
142+ -- @addSizeTo@ operation.
143+ serializeAt :: Int -> MutByteArray -> a -> IO Int
144144
145145--------------------------------------------------------------------------------
146146-- Instances
@@ -191,7 +191,7 @@ deserializeUnsafe off arr sz =
191191 if (next <= sz)
192192 then Unbox.peekAt off arr >>= \val -> pure (next, val)
193193 else error
194− $ "deserialize: accessing array at offset = "
194+ $ "deserializeAt: accessing array at offset = "
195195 ++ show (next - 1)
196196 ++ " max valid offset = " ++ show (sz - 1)
197197
@@ -201,19 +201,19 @@ serializeUnsafe off arr val =
201201 let next = off + Unbox.sizeOf (Proxy :: Proxy a)
202202 in do
203203#ifdef DEBUG
204− checkBounds "serialize" next arr
204+ checkBounds "serializeAt" next arr
205205#endif
206206 Unbox.pokeAt off arr val
207207 pure next
208208
209209#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
210210instance Serialize _type where \
211−; {-# INLINE size #-} \
212−; size acc _ = acc + Unbox.sizeOf (Proxy :: Proxy _type) \
213−; {-# INLINE deserialize #-} \
214−; deserialize off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
215−; {-# INLINE serialize #-} \
216−; serialize = \
211+; {-# INLINE addSizeTo #-} \
212+; addSizeTo acc _ = acc + Unbox.sizeOf (Proxy :: Proxy _type) \
213+; {-# INLINE deserializeAt #-} \
214+; deserializeAt off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
215+; {-# INLINE serializeAt #-} \
216+; serializeAt = \
217217 serializeUnsafe :: Int -> MutByteArray -> _type -> IO Int
218218
219219DERIVE_SERIALIZE_FROM_UNBOX(())
@@ -237,68 +237,69 @@ DERIVE_SERIALIZE_FROM_UNBOX((FunPtr a))
237237
238238instance forall a. Serialize a => Serialize [a] where
239239
240− -- {-# INLINE size #-}
241− size acc xs = foldl' size (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
240+ -- {-# INLINE addSizeTo #-}
241+ addSizeTo acc xs =
242+ foldl' addSizeTo (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
242243
243244 -- Inlining this causes large compilation times for tests
244− {-# INLINABLE deserialize #-}
245− deserialize off arr sz = do
246− (off1, len64) <- deserialize off arr sz :: IO (Int, Int64)
245+ {-# INLINABLE deserializeAt #-}
246+ deserializeAt off arr sz = do
247+ (off1, len64) <- deserializeAt off arr sz :: IO (Int, Int64)
247248 let len = (fromIntegral :: Int64 -> Int) len64
248249 peekList f o i | i >= 3 = do
249250 -- Unfold the loop three times
250− (o1, x1) <- deserialize o arr sz
251− (o2, x2) <- deserialize o1 arr sz
252− (o3, x3) <- deserialize o2 arr sz
251+ (o1, x1) <- deserializeAt o arr sz
252+ (o2, x2) <- deserializeAt o1 arr sz
253+ (o3, x3) <- deserializeAt o2 arr sz
253254 peekList (f . (\xs -> x1:x2:x3:xs)) o3 (i - 3)
254255 peekList f o 0 = pure (o, f [])
255256 peekList f o i = do
256− (o1, x) <- deserialize o arr sz
257+ (o1, x) <- deserializeAt o arr sz
257258 peekList (f . (x:)) o1 (i - 1)
258259 peekList id off1 len
259260
260261 -- Inlining this causes large compilation times for tests
261− {-# INLINABLE serialize #-}
262− serialize off arr val = do
262+ {-# INLINABLE serializeAt #-}
263+ serializeAt off arr val = do
263264 let off1 = off + Unbox.sizeOf (Proxy :: Proxy Int64)
264265 let pokeList acc o [] =
265266 Unbox.pokeAt off arr (acc :: Int64) >> pure o
266267 pokeList acc o (x:xs) = do
267− o1 <- serialize o arr x
268+ o1 <- serializeAt o arr x
268269 pokeList (acc + 1) o1 xs
269270 pokeList 0 off1 val
270271
271272instance Serialize (Array a) where
272− {-# INLINE size #-}
273− size i (Array {..}) = i + (arrEnd - arrStart) + 8
273+ {-# INLINE addSizeTo #-}
274+ addSizeTo i (Array {..}) = i + (arrEnd - arrStart) + 8
274275
275− {-# INLINE deserialize #-}
276− deserialize off arr end = do
277− (off1, byteLen) <- deserialize off arr end :: IO (Int, Int)
276+ {-# INLINE deserializeAt #-}
277+ deserializeAt off arr end = do
278+ (off1, byteLen) <- deserializeAt off arr end :: IO (Int, Int)
278279 let off2 = off1 + byteLen
279280 let slice = MutArray.MutArray arr off1 off2 off2
280281 newArr <- MutArray.clone slice
281282 pure (off2, Array.unsafeFreeze newArr)
282283
283− {-# INLINE serialize #-}
284− serialize off arr (Array {..}) = do
284+ {-# INLINE serializeAt #-}
285+ serializeAt off arr (Array {..}) = do
285286 let arrLen = arrEnd - arrStart
286− off1 <- serialize off arr arrLen
287+ off1 <- serializeAt off arr arrLen
287288 MBA.putSliceUnsafe arrContents arrStart arr off1 arrLen
288289 pure (off1 + arrLen)
289290
290291instance (Serialize a, Serialize b) => Serialize (a, b) where
291292
292− {-# INLINE size #-}
293− size acc (a, b) = size (size acc a) b
293+ {-# INLINE addSizeTo #-}
294+ addSizeTo acc (a, b) = addSizeTo (addSizeTo acc a) b
294295
295− {-# INLINE serialize #-}
296− serialize off arr (a, b) = do
297− off1 <- serialize off arr a
298− serialize off1 arr b
296+ {-# INLINE serializeAt #-}
297+ serializeAt off arr (a, b) = do
298+ off1 <- serializeAt off arr a
299+ serializeAt off1 arr b
299300
300− {-# INLINE deserialize #-}
301− deserialize off arr end = do
302− (off1, a) <- deserialize off arr end
303− (off2, b) <- deserialize off1 arr end
301+ {-# INLINE deserializeAt #-}
302+ deserializeAt off arr end = do
303+ (off1, a) <- deserializeAt off arr end
304+ (off2, b) <- deserializeAt off1 arr end
304305 pure (off2, (a, b))