on master help sign in
Remove Serailize.Size and change the signature of Serialize.size
5fef843Adithya Kumar
benchmark/Streamly/Benchmark/Data/Serialize.hs+1−3
@@ -292,9 +292,7 @@ getSize :: forall a. SERIALIZE_CLASS a => a -> Int
292#ifdef USE_UNBOX
292#ifdef USE_UNBOX
293getSize _ = sizeOf (Proxy :: Proxy a)
293getSize _ = sizeOf (Proxy :: Proxy a)
294#else
294#else
295−getSize val =
295+getSize = size 0
296− case size :: Size a of
297− Size f -> f 0 val
298#endif
296#endif
299
297
300-------------------------------------------------------------------------------
298-------------------------------------------------------------------------------
core/src/Streamly/Data/Serialize.hs−2
@@ -17,7 +17,6 @@ module Streamly.Data.Serialize
17 , deriveUnboxWith
17 , deriveUnboxWith
18
18
19 -- * Serialize
19 -- * Serialize
20− , Size(..)
21 , Serialize(..)
20 , Serialize(..)
22 , deriveSerialize
21 , deriveSerialize
23 , deriveSerializeWith
22 , deriveSerializeWith
@@ -36,7 +35,6 @@ module Streamly.Data.Serialize
36
35
37import Streamly.Internal.Data.Serialize
36import Streamly.Internal.Data.Serialize
38 ( Serialize(..)
37 ( Serialize(..)
39− , Size(..)
40 , decode
38 , decode
41 , encode
39 , encode
42 , pinnedEncode
40 , pinnedEncode
core/src/Streamly/Internal/Data/Serialize.hs+14−22
@@ -7,8 +7,7 @@
7--
7--
8
8
9module Streamly.Internal.Data.Serialize
9module Streamly.Internal.Data.Serialize
10− ( Size(..)
11− , Serialize(..)
10+ ( Serialize(..)
12 , encode
11 , encode
13 , pinnedEncode
12 , pinnedEncode
14 , decode
13 , decode
@@ -47,11 +46,6 @@ import GHC.Exts
47-- Types
46-- Types
48--------------------------------------------------------------------------------
47--------------------------------------------------------------------------------
49
48
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−
55-- | A type implementing the 'Serialize' interface supplies operations for
49-- | A type implementing the 'Serialize' interface supplies operations for
56-- reading and writing the type from and to a mutable byte array (an unboxed
50-- reading and writing the type from and to a mutable byte array (an unboxed
57-- representation of the type) in memory. The read operation 'deserialize'
51-- 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
66-- 'Serialize' contains enough information to serialize and deserialize variable
60-- 'Serialize' contains enough information to serialize and deserialize variable
67-- length types.
61-- length types.
68--
62--
69−-- >>> import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
63+-- >>> import Streamly.Internal.Data.Serialize (Serialize(..))
70--
64--
71-- >>> :{
65-- >>> :{
72-- data Object = Object
66-- data Object = Object
@@ -77,11 +71,7 @@ newtype Size a = Size (Int -> a -> Int) -- a left fold or Sum monoid
77--
71--
78-- >>> :{
72-- >>> :{
79-- instance Serialize Object where
73-- 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)
85-- deserialize i arr len = do
75-- deserialize i arr len = do
86-- (i1, x0) <- deserialize i arr len
76-- (i1, x0) <- deserialize i arr len
87-- (i2, x1) <- deserialize i1 arr len
77-- (i2, x1) <- deserialize i1 arr len
@@ -93,9 +83,15 @@ newtype Size a = Size (Int -> a -> Int) -- a left fold or Sum monoid
93-- :}
83-- :}
94--
84--
95class Serialize a where
85class Serialize a where
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+
96− -- | Get the 'Size', in bytes, reqired to store the serialized
92+ -- | Get the size, in bytes, reqired to store the serialized
97 -- representation of the type. Size cannot be zero.
93 -- representation of the type. Size cannot be zero.
98− size :: Size a
94+ size :: Int -> a -> Int
99
95
100 -- We can implement the following functions without returning the `Int`
96 -- We can implement the following functions without returning the `Int`
101 -- offset but that may require traversing the Haskell structure again to get
97 -- offset but that may require traversing the Haskell structure again to get
@@ -180,7 +176,7 @@ serializeUnsafe off arr val =
180#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
176#define DERIVE_SERIALIZE_FROM_UNBOX(_type) \
181instance Serialize _type where \
177instance Serialize _type where \
182; {-# INLINE size #-} \
178; {-# INLINE size #-} \
183−; size = Size (\acc _ -> acc + Unbox.sizeOf (Proxy :: Proxy _type)) \
179+; size acc _ = acc + Unbox.sizeOf (Proxy :: Proxy _type) \
184; {-# INLINE deserialize #-} \
180; {-# INLINE deserialize #-} \
185; deserialize off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
181; deserialize off arr end = deserializeUnsafe off arr end :: IO (Int, _type) \
186; {-# INLINE serialize #-} \
182; {-# INLINE serialize #-} \
@@ -208,9 +204,7 @@ DERIVE_SERIALIZE_FROM_UNBOX((FunPtr a))
208instance forall a. Serialize a => Serialize [a] where
204instance forall a. Serialize a => Serialize [a] where
209
205
210 -- {-# INLINE size #-}
206 -- {-# 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
214
208
215 -- Inlining this causes large compilation times for tests
209 -- Inlining this causes large compilation times for tests
216 {-# INLINABLE deserialize #-}
210 {-# INLINABLE deserialize #-}
@@ -248,9 +242,7 @@ instance forall a. Serialize a => Serialize [a] where
248encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
242encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
249encodeAs ps a =
243encodeAs ps a =
250 unsafeInlineIO $ do
244 unsafeInlineIO $ do
251− let len =
245+ let len = size 0 a
252− case size :: Size a of
253− Size f -> f 0 a
254 -- We encode the length of the encoding as a header hence the 8 extra
246 -- We encode the length of the encoding as a header hence the 8 extra
255 -- bytes to encode Int64
247 -- bytes to encode Int64
256 mbarr <- Unbox.newBytesAs ps (8 + len)
248 mbarr <- Unbox.newBytesAs ps (8 + len)
core/src/Streamly/Internal/Data/Serialize/TH.hs+9−19
@@ -80,9 +80,7 @@ matchConstructor cname numFields exp0 =
80 []
80 []
81
81
82exprGetSize :: Q Exp -> (Int, Type) -> Q Exp
82exprGetSize :: 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))|]
86
84
87getTagSize :: Int -> Int
85getTagSize :: Int -> Int
88getTagSize numConstructors
86getTagSize numConstructors
@@ -122,18 +120,13 @@ mkSizeOfExpr headTy constructors =
122 [constructor@(DataCon _ _ _ fields)] ->
120 [constructor@(DataCon _ _ _ fields)] ->
123 case fields of
121 case fields of
124 -- Unit type
122 -- Unit type
125− [] ->
126− appE
127− (conE 'Size)
128− (lamE [varP _acc, wildP] [| $(varE _acc) + 1 |])
123+ [] -> lamE [varP _acc, wildP] [| $(varE _acc) + 1 |]
129 -- Product type
124 -- Product type
130 _ ->
125 _ ->
131− appE
132− (conE 'Size)
133− (lamE
126+ lamE
134− [varP _acc, varP _x]
127+ [varP _acc, varP _x]
135− (caseE (varE _x)
128+ (caseE (varE _x)
136− [matchCons (varE _acc) constructor]))
129+ [matchCons (varE _acc) constructor])
137 -- Sum type
130 -- Sum type
138 _ -> sizeOfHeadDt
131 _ -> sizeOfHeadDt
139
132
@@ -160,12 +153,9 @@ mkSizeOfExpr headTy constructors =
160 -- XXX We fix VarSize for simplicity. Should be changed later.
153 -- XXX We fix VarSize for simplicity. Should be changed later.
161 sizeOfHeadDt =
154 sizeOfHeadDt =
162 let acc = [|$(varE _acc) + $(tagSizeExp)|]
155 let acc = [|$(varE _acc) + $(tagSizeExp)|]
163− f =
164− (lamE
156+ in lamE
165− [varP _acc, varP _x]
157+ [varP _acc, varP _x]
166− (caseE (varE _x) (fmap (matchCons acc) constructors))
158+ (caseE (varE _x) (fmap (matchCons acc) constructors))
167− )
168− in appE (conE 'Size) f
169
159
170--------------------------------------------------------------------------------
160--------------------------------------------------------------------------------
171-- Peek
161-- Peek
test/Streamly/Test/Data/Serialize.hs+2−6
@@ -57,9 +57,7 @@ roundtrip
57 -> IO ()
57 -> IO ()
58roundtrip val = do
58roundtrip val = do
59
59
60− let sz =
61− case Serialize.size :: Serialize.Size a of
60+ let sz = Serialize.size 0 val
62− Serialize.Size f -> f 0 val
63
61
64 -- putStrLn "----------------------------------------------------------------"
62 -- putStrLn "----------------------------------------------------------------"
65 -- putStrLn $ show val
63 -- putStrLn $ show val
@@ -79,9 +77,7 @@ testSerializeList
79 -> IO ()
77 -> IO ()
80testSerializeList sizeOfA val = do
78testSerializeList sizeOfA val = do
81
79
82− let sz =
83− case Serialize.size :: Serialize.Size a of
80+ let sz = Serialize.size 0 val
84− Serialize.Size f -> f 0 val
85
81
86 sz `shouldBe` sizeOfA
82 sz `shouldBe` sizeOfA
87
83
test/Streamly/Test/Data/Unbox.hs+2−4
@@ -32,7 +32,7 @@ import GHC.Real (Ratio(..))
32
32
33#ifdef USE_SERIALIZE
33#ifdef USE_SERIALIZE
34
34
35−import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
35+import Streamly.Internal.Data.Serialize (Serialize(..))
36import Streamly.Internal.Data.Serialize.TH
36import Streamly.Internal.Data.Serialize.TH
37
37
38#else
38#else
@@ -179,9 +179,7 @@ variableSizeOf ::
179 forall a. Serialize a
179 forall a. Serialize a
180 => a
180 => a
181 -> Int
181 -> Int
182−variableSizeOf val =
182+variableSizeOf = size 0
183− case size :: Size a of
184− Size f -> f 0 val
185#endif
183#endif
186
184
187testSerialization ::
185testSerialization ::