@@ -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.
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'.
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
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
@@ -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.
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
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.
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.
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)
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.
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
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)
@@ -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)
203#ifdef DEBUG
203#ifdef DEBUG
204− checkBounds "serialize" next arr
204+ checkBounds "serializeAt" next arr
206 Unbox.pokeAt off arr val
206 Unbox.pokeAt off arr val
207 pure next
207 pure next
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
219DERIVE_SERIALIZE_FROM_UNBOX(())
219DERIVE_SERIALIZE_FROM_UNBOX(())
@@ -237,68 +237,69 @@ DERIVE_SERIALIZE_FROM_UNBOX((FunPtr a))
238instance forall a. Serialize a => Serialize [a] where
238instance forall a. Serialize a => Serialize [a] where
240− -- {-# INLINE size #-}
240+ -- {-# INLINE addSizeTo #-}
241− size acc xs = foldl' size (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
242+ foldl' addSizeTo (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
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
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
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
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)
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)
290instance (Serialize a, Serialize b) => Serialize (a, b) where
291instance (Serialize a, Serialize b) => Serialize (a, b) where
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
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
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))