@@ -7,8 +7,7 @@
77--
88
99module Streamly.Internal.Data.Serialize
10− ( Size(..)
11− , Serialize(..)
10+ ( Serialize(..)
1211 , encode
1312 , pinnedEncode
1413 , decode
@@ -47,11 +46,6 @@ import GHC.Exts
4746-- Types
4847--------------------------------------------------------------------------------
4948
50−-- XXX Use (a -> Sum Int) instead, remove the Size type
51−
52−-- | A left fold step to fold a generic structure to its serializable size.
53−newtype Size a = Size (Int -> a -> Int) -- a left fold or Sum monoid
54−
5549-- | A type implementing the 'Serialize' interface supplies operations for
5650-- reading and writing the type from and to a mutable byte array (an unboxed
5751-- representation of the type) in memory. The read operation 'deserialize'
@@ -66,7 +60,7 @@ newtype Size a = Size (Int -> a -> Int) -- a left fold or Sum monoid
6660-- 'Serialize' contains enough information to serialize and deserialize variable
6761-- length types.
6862--
69−-- >>> import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
63+-- >>> import Streamly.Internal.Data.Serialize (Serialize(..))
7064--
7165-- >>> :{
7266-- data Object = Object
@@ -77,11 +71,7 @@ newtype Size a = Size (Int -> a -> Int) -- a left fold or Sum monoid
7771--
7872-- >>> :{
7973-- instance Serialize Object where
80−-- size =
81−-- case (size :: Size [Int], size :: Size Int) of
82−-- (Size f, Size g) ->
83−-- Size $ \acc obj ->
84−-- acc + f 0 (_obj1 obj) + g 0 (_obj2 obj)
74+-- size acc obj = size (size acc (_obj1 obj)) (_obj2 obj)
8575-- deserialize i arr len = do
8676-- (i1, x0) <- deserialize i arr len
8777-- (i2, x1) <- deserialize i1 arr len
@@ -93,9 +83,15 @@ newtype Size a = Size (Int -> a -> Int) -- a left fold or Sum monoid
9383-- :}
9484--
9585class Serialize a where
96− -- | Get the 'Size', in bytes, reqired to store the serialized
86+ -- XXX Use (a -> Sum Int) instead, remove the Size type
87+
88+ -- A left fold step to fold a generic structure to its serializable size.
89+ -- It is of the form @Int -> a -> Int@ because you can have tail-recursive
90+ -- traversal of the structures.
91+
92+ -- | Get the size, in bytes, reqired to store the serialized
9793 -- representation of the type. Size cannot be zero.
98− size :: Size a
94+ size :: Int -> a -> Int
9995
10096 -- We can implement the following functions without returning the `Int`
10197 -- offset but that may require traversing the Haskell structure again to get
@@ -180,7 +176,7 @@ serializeUnsafe off arr val =
180176#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
181177instance Serialize _type where \
182178; {-# INLINE size #-} \
183−; size = Size (\acc _ -> acc + Unbox.sizeOf (Proxy :: Proxy _type)) \
179+; size acc _ = acc + Unbox.sizeOf (Proxy :: Proxy _type) \
184180; {-# INLINE deserialize #-} \
185181; deserialize off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
186182; {-# INLINE serialize #-} \
@@ -208,9 +204,7 @@ DERIVE_SERIALIZE_FROM_UNBOX((FunPtr a))
208204instance forall a. Serialize a => Serialize [a] where
209205
210206 -- {-# INLINE size #-}
211− size = Size $ \acc xs ->
212− case size :: Size a of
213− Size f -> foldl' f (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
207+ size acc xs = foldl' size (acc + (Unbox.sizeOf (Proxy :: Proxy Int))) xs
214208
215209 -- Inlining this causes large compilation times for tests
216210 {-# INLINABLE deserialize #-}
@@ -248,9 +242,7 @@ instance forall a. Serialize a => Serialize [a] where
248242encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
249243encodeAs ps a =
250244 unsafeInlineIO $ do
251− let len =
252− case size :: Size a of
253− Size f -> f 0 a
245+ let len = size 0 a
254246 -- We encode the length of the encoding as a header hence the 8 extra
255247 -- bytes to encode Int64
256248 mbarr <- Unbox.newBytesAs ps (8 + len)