on master help sign in
Remove Serailize.Size and change the signature of Serialize.size
commit5fef84310eb3fa8e887c5090ce45653fce9cea0e authorAdithya Kumar <adithya@composewell.com> dateTue 29 Aug 2023, 18:27 (3 years ago) parent6a39719
6 files changed+28−56
benchmark/Streamly/Benchmark/Data/Serialize.hs+1−3 core/src/Streamly/Data/Serialize.hs−2 core/src/Streamly/Internal/Data/Serialize.hs+14−22 core/src/Streamly/Internal/Data/Serialize/TH.hs+9−19 test/Streamly/Test/Data/Serialize.hs+2−6 test/Streamly/Test/Data/Unbox.hs+2−4
benchmark/Streamly/Benchmark/Data/Serialize.hs+1−3
@@ -292,9 +292,7 @@ getSize :: forall a. SERIALIZE_CLASS a => a -> Int
292292#ifdef USE_UNBOX
293293getSize _ = sizeOf (Proxy :: Proxy a)
294294#else
295−getSize val =
296− case size :: Size a of
297− Size f -> f 0 val
295+getSize = size 0
298296#endif
299297
300298-------------------------------------------------------------------------------
core/src/Streamly/Data/Serialize.hs−2
@@ -17,7 +17,6 @@ module Streamly.Data.Serialize
1717 , deriveUnboxWith
1818
1919 -- * Serialize
20− , Size(..)
2120 , Serialize(..)
2221 , deriveSerialize
2322 , deriveSerializeWith
@@ -36,7 +35,6 @@ module Streamly.Data.Serialize
3635
3736import Streamly.Internal.Data.Serialize
3837 ( Serialize(..)
39− , Size(..)
4038 , decode
4139 , encode
4240 , pinnedEncode
core/src/Streamly/Internal/Data/Serialize.hs+14−22
@@ -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)
core/src/Streamly/Internal/Data/Serialize/TH.hs+9−19
@@ -80,9 +80,7 @@ matchConstructor cname numFields exp0 =
8080 []
8181
8282exprGetSize :: Q Exp -> (Int, Type) -> Q Exp
83−exprGetSize acc (i, ty) =
84− [|case size :: Size $(pure ty) of
85− Size f -> f $(acc) $(varE (mkFieldName i))|]
83+exprGetSize acc (i, _) = [|size $(acc) $(varE (mkFieldName i))|]
8684
8785getTagSize :: Int -> Int
8886getTagSize numConstructors
@@ -122,18 +120,13 @@ mkSizeOfExpr headTy constructors =
122120 [constructor@(DataCon _ _ _ fields)] ->
123121 case fields of
124122 -- Unit type
125− [] ->
126− appE
127− (conE 'Size)
128− (lamE [varP _acc, wildP] [| $(varE _acc) + 1 |])
123+ [] -> lamE [varP _acc, wildP] [| $(varE _acc) + 1 |]
129124 -- Product type
130125 _ ->
131− appE
132− (conE 'Size)
133− (lamE
134− [varP _acc, varP _x]
135− (caseE (varE _x)
136− [matchCons (varE _acc) constructor]))
126+ lamE
127+ [varP _acc, varP _x]
128+ (caseE (varE _x)
129+ [matchCons (varE _acc) constructor])
137130 -- Sum type
138131 _ -> sizeOfHeadDt
139132
@@ -160,12 +153,9 @@ mkSizeOfExpr headTy constructors =
160153 -- XXX We fix VarSize for simplicity. Should be changed later.
161154 sizeOfHeadDt =
162155 let acc = [|$(varE _acc) + $(tagSizeExp)|]
163− f =
164− (lamE
165− [varP _acc, varP _x]
166− (caseE (varE _x) (fmap (matchCons acc) constructors))
167− )
168− in appE (conE 'Size) f
156+ in lamE
157+ [varP _acc, varP _x]
158+ (caseE (varE _x) (fmap (matchCons acc) constructors))
169159
170160--------------------------------------------------------------------------------
171161-- Peek
test/Streamly/Test/Data/Serialize.hs+2−6
@@ -57,9 +57,7 @@ roundtrip
5757 -> IO ()
5858roundtrip val = do
5959
60− let sz =
61− case Serialize.size :: Serialize.Size a of
62− Serialize.Size f -> f 0 val
60+ let sz = Serialize.size 0 val
6361
6462 -- putStrLn "----------------------------------------------------------------"
6563 -- putStrLn $ show val
@@ -79,9 +77,7 @@ testSerializeList
7977 -> IO ()
8078testSerializeList sizeOfA val = do
8179
82− let sz =
83− case Serialize.size :: Serialize.Size a of
84− Serialize.Size f -> f 0 val
80+ let sz = Serialize.size 0 val
8581
8682 sz `shouldBe` sizeOfA
8783
test/Streamly/Test/Data/Unbox.hs+2−4
@@ -32,7 +32,7 @@ import GHC.Real (Ratio(..))
3232
3333#ifdef USE_SERIALIZE
3434
35−import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
35+import Streamly.Internal.Data.Serialize (Serialize(..))
3636import Streamly.Internal.Data.Serialize.TH
3737
3838#else
@@ -179,9 +179,7 @@ variableSizeOf ::
179179 forall a. Serialize a
180180 => a
181181 -> Int
182−variableSizeOf val =
183− case size :: Size a of
184− Size f -> f 0 val
182+variableSizeOf = size 0
185183#endif
186184
187185testSerialization ::