on master help sign in
Move functions from Data.Serialize to Data.Internal.Serialize
9be03c8Adithya Kumar
core/src/Streamly/Data/Serialize.hs+10−52
@@ -30,65 +30,23 @@ module Streamly.Data.Serialize
30 , decode
30 , decode
31 ) where
31 ) where
32
32
33−#include "assert.hs"
33+--------------------------------------------------------------------------------
34+-- Imports
35+--------------------------------------------------------------------------------
34
36
35−import Data.Int (Int64)
36−import Data.Word (Word8)
37−import Streamly.Internal.Data.Array.Type (Array(..))
37+import Streamly.Internal.Data.Serialize
38−import Streamly.Internal.Data.Serialize (Serialize(..), Size(..))
38+ ( Serialize(..)
39+ , Size(..)
40+ , decode
41+ , encode
42+ , pinnedEncode
43+ )
39import Streamly.Internal.Data.Serialize.TH
44import Streamly.Internal.Data.Serialize.TH
40 ( deriveSerialize
45 ( deriveSerialize
41 , deriveSerializeWith
46 , deriveSerializeWith
42 )
47 )
43import Streamly.Internal.Data.Unbox
48import Streamly.Internal.Data.Unbox
44 ( MutableByteArray
49 ( MutableByteArray
45− , PinnedState(..)
46 , Unbox(..)
50 , Unbox(..)
47 )
51 )
48import Streamly.Internal.Data.Unbox.TH (deriveUnbox, deriveUnboxWith)
52import Streamly.Internal.Data.Unbox.TH (deriveUnbox, deriveUnboxWith)
49−import Streamly.Internal.System.IO (unsafeInlineIO)
50−
51−import qualified Streamly.Internal.Data.Unbox as Unbox
52−import qualified Streamly.Internal.Data.Array as Array
53−
54−--------------------------------------------------------------------------------
55−-- Serialize
56−--------------------------------------------------------------------------------
57−
58−{-# INLINE encodeAs #-}
59−encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
60−encodeAs ps a =
61− unsafeInlineIO $ do
62− let len =
63− case size :: Size a of
64− ConstSize sz -> sz
65− VarSize f -> f a
66− -- We encode the length of the encoding as a header hence the 8 extra
67− -- bytes to encode Int64
68− mbarr <- Unbox.newBytesAs ps (8 + len)
69− off1 <- serialize 0 mbarr (fromIntegral len :: Int64)
70− off2 <- serialize off1 mbarr a
71− assertM(off2 == len + off1)
72− pure $ Array mbarr 0 len
73−
74−{-# INLINE encode #-}
75−encode :: Serialize a => a -> Array Word8
76−encode = encodeAs Unpinned
77−
78−{-# INLINE pinnedEncode #-}
79−pinnedEncode :: Serialize a => a -> Array Word8
80−pinnedEncode = encodeAs Pinned
81−
82−--------------------------------------------------------------------------------
83−-- Deserialize
84−--------------------------------------------------------------------------------
85−
86−{-# INLINE decode #-}
87−decode :: Serialize a => Array Word8 -> a
88−decode arr@(Array {..}) = unsafeInlineIO $ do
89− let lenArr = Array.length arr
90− (off1, lenEncoding :: Int64) <- deserialize 0 arrContents
91− (off2, val) <- deserialize off1 arrContents
92− assertM(fromIntegral lenEncoding + off1 == off2)
93− assertM(lenArr == off2)
94− pure val
core/src/Streamly/Internal/Data/Serialize.hs+47−2
@@ -9,22 +9,29 @@
9module Streamly.Internal.Data.Serialize
9module Streamly.Internal.Data.Serialize
10 ( Size(..)
10 ( Size(..)
11 , Serialize(..)
11 , Serialize(..)
12+ , encode
13+ , pinnedEncode
14+ , decode
12 ) where
15 ) where
13
16
14--------------------------------------------------------------------------------
17--------------------------------------------------------------------------------
15-- Imports
18-- Imports
16--------------------------------------------------------------------------------
19--------------------------------------------------------------------------------
17
20
21+#include "assert.hs"
22+
18import Control.Monad (void)
23import Control.Monad (void)
19import Data.List (foldl')
24import Data.List (foldl')
20import Data.Proxy (Proxy (..))
25import Data.Proxy (Proxy (..))
21−import Streamly.Internal.Data.Unbox (MutableByteArray(..))
26+import Streamly.Internal.Data.Unbox (MutableByteArray(..), PinnedState(..))
22−
27+import Streamly.Internal.Data.Array.Type (Array(..))
28+import Streamly.Internal.System.IO (unsafeInlineIO)
23import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
29import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
24import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
30import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
25import GHC.Stable (StablePtr(..))
31import GHC.Stable (StablePtr(..))
26
32
27import qualified Streamly.Internal.Data.Unbox as Unbox
33import qualified Streamly.Internal.Data.Unbox as Unbox
34+import qualified Streamly.Internal.Data.Array as Array
28
35
29import GHC.Exts
36import GHC.Exts
30
37
@@ -167,3 +174,41 @@ instance forall a. Serialize a => Serialize [a] where
167 o1 <- serialize o arr x
174 o1 <- serialize o arr x
168 pokeList o1 xs
175 pokeList o1 xs
169 pokeList off1 val
176 pokeList off1 val
177+
178+--------------------------------------------------------------------------------
179+-- High level functions
180+--------------------------------------------------------------------------------
181+
182+{-# INLINE encodeAs #-}
183+encodeAs :: forall a. Serialize a => PinnedState -> a -> Array Word8
184+encodeAs ps a =
185+ unsafeInlineIO $ do
186+ let len =
187+ case size :: Size a of
188+ ConstSize sz -> sz
189+ VarSize f -> f a
190+ -- We encode the length of the encoding as a header hence the 8 extra
191+ -- bytes to encode Int64
192+ mbarr <- Unbox.newBytesAs ps (8 + len)
193+ off1 <- serialize 0 mbarr (fromIntegral len :: Int64)
194+ off2 <- serialize off1 mbarr a
195+ assertM(off2 == len + off1)
196+ pure $ Array mbarr 0 len
197+
198+{-# INLINE encode #-}
199+encode :: Serialize a => a -> Array Word8
200+encode = encodeAs Unpinned
201+
202+{-# INLINE pinnedEncode #-}
203+pinnedEncode :: Serialize a => a -> Array Word8
204+pinnedEncode = encodeAs Pinned
205+
206+{-# INLINE decode #-}
207+decode :: Serialize a => Array Word8 -> a
208+decode arr@(Array {..}) = unsafeInlineIO $ do
209+ let lenArr = Array.length arr
210+ (off1, lenEncoding :: Int64) <- deserialize 0 arrContents
211+ (off2, val) <- deserialize off1 arrContents
212+ assertM(fromIntegral lenEncoding + off1 == off2)
213+ assertM(lenArr == off2)
214+ pure val