on master help sign in
Rename Data.Serialize to Data.MutByteArray
6705ec0Harendra Kumar
benchmark/Streamly/Benchmark/Data/Array/Stream.hs+1−1
@@ -53,7 +53,7 @@ import Streamly.Benchmark.Common.Handle
53import Control.Monad.IO.Class (MonadIO)
53import Control.Monad.IO.Class (MonadIO)
54
54
55#ifdef INSPECTION
55#ifdef INSPECTION
56−import Streamly.Internal.Data.Serialize (Unbox)
56+import Streamly.Internal.Data.MutByteArray (Unbox)
57import Streamly.Internal.Data.Stream (Step(..))
57import Streamly.Internal.Data.Stream (Step(..))
58import Test.Inspection
58import Test.Inspection
59#endif
59#endif
benchmark/Streamly/Benchmark/Data/Serialize.hs+1−1
@@ -24,7 +24,7 @@ import System.Random (randomRIO)
24import Test.QuickCheck (Arbitrary, arbitrary)
24import Test.QuickCheck (Arbitrary, arbitrary)
25#endif
25#endif
26
26
27−import Streamly.Internal.Data.Serialize hiding (encode)
27+import Streamly.Internal.Data.MutByteArray hiding (encode)
28#ifdef USE_UNBOX
28#ifdef USE_UNBOX
29import Data.Proxy (Proxy(..))
29import Data.Proxy (Proxy(..))
30#else
30#else
benchmark/Streamly/Benchmark/Data/Serialize/RecCompatible.hs+2−2
@@ -9,10 +9,10 @@ module Streamly.Benchmark.Data.Serialize.RecCompatible
9-- Imports
9-- Imports
10--------------------------------------------------------------------------------
10--------------------------------------------------------------------------------
11
11
12−import Streamly.Internal.Data.Serialize (Serialize)
12+import Streamly.Internal.Data.MutByteArray (Serialize)
13import Streamly.Benchmark.Data.Serialize.TH (genLargeRecord)
13import Streamly.Benchmark.Data.Serialize.TH (genLargeRecord)
14
14
15−import qualified Streamly.Internal.Data.Serialize as Serialize
15+import qualified Streamly.Internal.Data.MutByteArray as Serialize
16
16
17--------------------------------------------------------------------------------
17--------------------------------------------------------------------------------
18-- Code
18-- Code
benchmark/Streamly/Benchmark/Data/Serialize/RecNonCompatible.hs+1−1
@@ -11,7 +11,7 @@ module Streamly.Benchmark.Data.Serialize.RecNonCompatible
11
11
12import Streamly.Benchmark.Data.Serialize.TH (genLargeRecord)
12import Streamly.Benchmark.Data.Serialize.TH (genLargeRecord)
13
13
14−import qualified Streamly.Internal.Data.Serialize as Serialize
14+import qualified Streamly.Internal.Data.MutByteArray as Serialize
15
15
16--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
17-- Code
17-- Code
benchmark/Streamly/Benchmark/Data/Serialize/TH.hs+1−1
@@ -8,7 +8,7 @@ module Streamly.Benchmark.Data.Serialize.TH (genLargeRecord) where
8
8
9import Language.Haskell.TH
9import Language.Haskell.TH
10
10
11−import Streamly.Internal.Data.Serialize (makeI)
11+import Streamly.Internal.Data.MutByteArray (makeI)
12import Control.DeepSeq (NFData(..))
12import Control.DeepSeq (NFData(..))
13
13
14--------------------------------------------------------------------------------
14--------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Unicode/Stream.hs+1−1
@@ -43,7 +43,7 @@ import Streamly.Benchmark.Common
43import Streamly.Benchmark.Common.Handle
43import Streamly.Benchmark.Common.Handle
44
44
45#ifdef INSPECTION
45#ifdef INSPECTION
46−import Streamly.Internal.Data.Serialize (Unbox)
46+import Streamly.Internal.Data.MutByteArray (Unbox)
47import Streamly.Internal.Data.Stream (Step(..))
47import Streamly.Internal.Data.Stream (Step(..))
48import qualified Streamly.Internal.Data.Fold as Fold
48import qualified Streamly.Internal.Data.Fold as Fold
49import qualified Streamly.Internal.Data.Tuple.Strict as Strict
49import qualified Streamly.Internal.Data.Tuple.Strict as Strict
core/src/Streamly/Data/Serialize.hs → core/src/Streamly/Data/MutByteArray.hsrenamed+3−3
@@ -1,5 +1,5 @@
1-- |
1-- |
2−-- Module : Streamly.Data.Serialize
2+-- Module : Streamly.Data.MutByteArray
3-- Copyright : (c) 2023 Composewell Technologies
3-- Copyright : (c) 2023 Composewell Technologies
4-- License : BSD3-3-Clause
4-- License : BSD3-3-Clause
5-- Maintainer : streamly@composewell.com
5-- Maintainer : streamly@composewell.com
@@ -42,7 +42,7 @@
42-- to and from an 'Array' type. You can also serialize and deserialize directly
42-- to and from an 'Array' type. You can also serialize and deserialize directly
43-- to and from a 'MutByteArray', using the type class methods.
43-- to and from a 'MutByteArray', using the type class methods.
44--
44--
45−module Streamly.Data.Serialize
45+module Streamly.Data.MutByteArray
46 (
46 (
47
47
48 -- * Mutable Byte Array
48 -- * Mutable Byte Array
@@ -82,4 +82,4 @@ module Streamly.Data.Serialize
82-- Imports
82-- Imports
83--------------------------------------------------------------------------------
83--------------------------------------------------------------------------------
84
84
85−import Streamly.Internal.Data.Serialize
85+import Streamly.Internal.Data.MutByteArray
core/src/Streamly/Internal/Data/Array/Type.hs+1−1
@@ -121,7 +121,7 @@ import qualified Streamly.Internal.Data.MutArray.Type as MA
121import qualified Streamly.Internal.Data.Stream.Type as D
121import qualified Streamly.Internal.Data.Stream.Type as D
122import qualified Streamly.Internal.Data.Stream.Generate as D
122import qualified Streamly.Internal.Data.Stream.Generate as D
123import qualified Streamly.Internal.Data.StreamK.Type as K
123import qualified Streamly.Internal.Data.StreamK.Type as K
124−import qualified Streamly.Internal.Data.MutByteArray as Unboxed
124+import qualified Streamly.Internal.Data.MutByteArray.Type as Unboxed
125import qualified Streamly.Internal.Data.Unfold.Type as Unfold
125import qualified Streamly.Internal.Data.Unfold.Type as Unfold
126import qualified Text.ParserCombinators.ReadPrec as ReadPrec
126import qualified Text.ParserCombinators.ReadPrec as ReadPrec
127
127
core/src/Streamly/Internal/Data/IORef/Unboxed.hs+2−1
@@ -40,7 +40,8 @@ where
40
40
41import Control.Monad.IO.Class (MonadIO(..))
41import Control.Monad.IO.Class (MonadIO(..))
42import Data.Proxy (Proxy(..))
42import Data.Proxy (Proxy(..))
43−import Streamly.Internal.Data.MutByteArray (MutByteArray(..), newByteArray)
43+import Streamly.Internal.Data.MutByteArray.Type
44+ (MutByteArray(..), newByteArray)
44import Streamly.Internal.Data.Unbox (Unbox(..), sizeOf)
45import Streamly.Internal.Data.Unbox (Unbox(..), sizeOf)
45
46
46import qualified Streamly.Internal.Data.Stream.Type as D
47import qualified Streamly.Internal.Data.Stream.Type as D
core/src/Streamly/Internal/Data/MutArray/Type.hs+2−2
@@ -239,7 +239,7 @@ import Data.Proxy (Proxy(..))
239import Data.Word (Word8)
239import Data.Word (Word8)
240import Foreign.C.Types (CSize(..), CInt(..))
240import Foreign.C.Types (CSize(..), CInt(..))
241import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
241import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
242−import Streamly.Internal.Data.MutByteArray
242+import Streamly.Internal.Data.MutByteArray.Type
243 ( MutByteArray(..)
243 ( MutByteArray(..)
244 , MutableByteArray
244 , MutableByteArray
245 , PinnedState(..)
245 , PinnedState(..)
@@ -266,7 +266,7 @@ import Streamly.Internal.Data.Unfold.Type (Unfold(..))
266import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
266import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
267
267
268import qualified Streamly.Internal.Data.Fold.Type as FL
268import qualified Streamly.Internal.Data.Fold.Type as FL
269−import qualified Streamly.Internal.Data.MutByteArray as Unboxed
269+import qualified Streamly.Internal.Data.MutByteArray.Type as Unboxed
270import qualified Streamly.Internal.Data.Producer as Producer
270import qualified Streamly.Internal.Data.Producer as Producer
271import qualified Streamly.Internal.Data.Stream.Type as D
271import qualified Streamly.Internal.Data.Stream.Type as D
272import qualified Streamly.Internal.Data.Stream.Lift as D
272import qualified Streamly.Internal.Data.Stream.Lift as D
core/src/Streamly/Internal/Data/MutByteArray.hs+220−219
@@ -1,249 +1,250 @@
1−{-# LANGUAGE UnboxedTuples #-}
1+{-# LANGUAGE TemplateHaskell #-}
2+
3+-- This is required as all the instances in this module are orphan instances.
4+{-# OPTIONS_GHC -fno-warn-orphans #-}
2
5
3-- |
6-- |
4-- Module : Streamly.Internal.Data.MutByteArray
7-- Module : Streamly.Internal.Data.MutByteArray
5-- Copyright : (c) 2023 Composewell Technologies
8-- Copyright : (c) 2023 Composewell Technologies
6-- License : BSD3-3-Clause
9-- License : BSD3-3-Clause
7-- Maintainer : streamly@composewell.com
10-- Maintainer : streamly@composewell.com
8−-- Stability : experimental
9-- Portability : GHC
11-- Portability : GHC
10--
12--
13+
11module Streamly.Internal.Data.MutByteArray
14module Streamly.Internal.Data.MutByteArray
12 (
15 (
13− -- ** MutByteArray
16+ -- * MutByteArray
14− MutByteArray(..)
17+ module Streamly.Internal.Data.MutByteArray.Type
15− , MutableByteArray
18+ -- * Unbox
16− , getMutableByteArray#
19+ , module Streamly.Internal.Data.Unbox
17−
20+ , module Streamly.Internal.Data.Unbox.TH
18− -- ** Pinning
21+ -- * Serialize
19− , PinnedState(..)
22+ , module Streamly.Internal.Data.Serialize.Type
20− , isPinned
23+ -- * Serialize TH
21− , pin
24+ , module Streamly.Internal.Data.Serialize.TH
22− , unpin
23−
24− -- ** Allocation
25− , nil
26− , newBytesAs
27− , newByteArray
28− , pinnedNewByteArray
29− , pinnedNewAlignedBytes
30−
31− -- ** Access
32− , sizeOfMutableByteArray
33− , putSliceUnsafe
34− , asPtrUnsafe
35 ) where
25 ) where
36
26
37−import Control.Monad.IO.Class (MonadIO(..))
27+--------------------------------------------------------------------------------
38−#ifdef DEBUG
28+-- Imports
39−import Debug.Trace (trace)
29+--------------------------------------------------------------------------------
30+
31+import Data.Proxy (Proxy(..))
32+import Streamly.Internal.Data.Array (Array(..))
33+import GHC.Exts (Int(..), sizeofByteArray#, unsafeCoerce#)
34+import GHC.Word (Word8)
35+
36+#if __GLASGOW_HASKELL__ >= 900
37+import GHC.Num.Integer (Integer(..))
38+#else
39+import GHC.Integer.GMP.Internals (Integer(..), BigNat(..))
40#endif
40#endif
41−import GHC.Base (IO(..))
42−import System.IO.Unsafe (unsafePerformIO)
43
41
44−import GHC.Exts
42+import Streamly.Internal.Data.MutByteArray.Type
43+import Streamly.Internal.Data.Serialize.TH
44+import Streamly.Internal.Data.Serialize.Type
45+import Streamly.Internal.Data.Unbox
46+import Streamly.Internal.Data.Unbox.TH
45
47
46--------------------------------------------------------------------------------
48--------------------------------------------------------------------------------
47−-- The ArrayContents type
49+-- Common instances
48--------------------------------------------------------------------------------
50--------------------------------------------------------------------------------
49
51
50−data PinnedState
52+-- Note
51− = Pinned
53+-- ====
52− | Unpinned
53−
54−-- XXX can use UnliftedNewtypes
55−
56−-- | A lifted mutable byte array type wrapping @MutableByteArray# RealWorld@.
57−-- This is a low level array used to back high level unboxed arrays and
58−-- serialized data.
59−data MutByteArray = MutByteArray (MutableByteArray# RealWorld)
60−
61−{-# DEPRECATED MutableByteArray "Please use MutByteArray instead" #-}
62−type MutableByteArray = MutByteArray
63−
64−{-# INLINE getMutableByteArray# #-}
65−getMutableByteArray# :: MutByteArray -> MutableByteArray# RealWorld
66−getMutableByteArray# (MutByteArray mbarr) = mbarr
67−
68−-- | Return the size of the array in bytes.
69−{-# INLINE sizeOfMutableByteArray #-}
70−sizeOfMutableByteArray :: MutByteArray -> IO Int
71−sizeOfMutableByteArray (MutByteArray arr) =
72− IO $ \s ->
73− case getSizeofMutableByteArray# arr s of
74− (# s1, i #) -> (# s1, I# i #)
75−
76−{-# INLINE touch #-}
77−touch :: MutByteArray -> IO ()
78−touch (MutByteArray contents) =
79− IO $ \s -> case touch# contents s of s' -> (# s', () #)
80−
81−-- XXX We can provide another API for "unsafe" FFI calls passing an unlifted
82−-- pointer to the FFI call. For unsafe calls we do not need to pin the array.
83−-- We can pass an unlifted pointer to the FFI routine to avoid GC kicking in
84−-- before the pointer is wrapped.
85--
54--
86−-- From the GHC manual:
55+-- Even a non-functional change such as changing the order of constructors will
56+-- change the instance derivation.
87--
57--
88−-- GHC, since version 8.4, guarantees that garbage collection will never occur
58+-- This will not pose a problem if both, encode, and decode are done by the same
89−-- during an unsafe call, even in the bytecode interpreter, and further
59+-- version of the application. There *might* be a problem if version that
90−-- guarantees that unsafe calls will be performed in the calling thread. Making
60+-- encodes differs from the version that decodes.
91−-- it safe to pass heap-allocated objects to unsafe functions.
61+--
92−
62+-- We need to add some compatibility tests using different versions of
93−-- | Use a @MutByteArray@ as @Ptr a@. This is useful when we want to pass
63+-- dependencies.
94−-- an array as a pointer to some operating system call or to a "safe" FFI call.
95--
64--
96−-- If the array is not pinned it is copied to pinned memory before passing it
65+-- Although such chages for the most basic types won't happen we need to detect
97−-- to the monadic action.
66+-- if it ever happens.
98--
67--
99−-- /Performance Notes:/ Forces a copy if the array is not pinned. It is advised
68+-- Should we worry about these kind of changes and this kind of compatibility?
100−-- that the programmer keeps this in mind and creates a pinned array
69+-- This is a problem for all types of derivations that depend on the order of
101−-- opportunistically before this operation occurs, to avoid the cost of a copy
70+-- constructors, for example, Enum.
102−-- if possible.
71+
72+-- Note on Windows build
73+-- =====================
74+--
75+-- On Windows, having template haskell splices here fail the build with the
76+-- following error:
103--
77--
104−-- /Unsafe/ because of direct pointer operations. The user must ensure that
78+-- @
105−-- they are writing within the legal bounds of the array.
79+-- addLibrarySearchPath: C:\... (Win32 error 3): The system cannot find the path specified.
80+-- @
106--
81--
107−-- /Pre-release/
82+-- The error might be irrelavant but having these splices triggers it. We should
83+-- either fix the problem or avoid the use to template haskell splices in this
84+-- file.
108--
85--
86+-- Similar issue: https://github.com/haskell/cabal/issues/4741
87+
88+-- $(Serialize.deriveSerialize ''Maybe)
89+instance Serialize a => Serialize (Maybe a) where
90+
109−{-# INLINE asPtrUnsafe #-}
91+ {-# INLINE size #-}
110−asPtrUnsafe :: MonadIO m => MutByteArray -> (Ptr a -> m b) -> m b
92+ size acc x =
111−asPtrUnsafe arr f = do
93+ case x of
112− contents <- liftIO $ pin arr
94+ Nothing -> (acc + 1)
113− let !ptr = Ptr (byteArrayContents#
95+ Just field0 -> (size (acc + 1)) field0
114− (unsafeCoerce# (getMutableByteArray# contents)))
96+
115− r <- f ptr
97+ {-# INLINE deserialize #-}
116− liftIO $ touch contents
98+ deserialize initialOffset arr endOffset = do
117− return r
99+ (i0, tag) <- ((deserialize initialOffset) arr) endOffset
100+ case tag :: Word8 of
101+ 0 -> pure (i0, Nothing)
102+ 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
103+ pure (i1, Just a0)
104+ _ -> error "Found invalid tag while peeking (Maybe a)"
105+
106+ {-# INLINE serialize #-}
107+ serialize initialOffset arr val =
108+ case val of
109+ Nothing -> do
110+ i0 <- ((serialize initialOffset) arr) (0 :: Word8)
111+ pure i0
112+ Just field0 -> do
113+ i0 <- ((serialize initialOffset) arr) (1 :: Word8)
114+ i1 <- ((serialize i0) arr) field0
115+ pure i1
116+
117+-- $(Serialize.deriveSerialize ''Either)
118+instance (Serialize a, Serialize b) => Serialize (Either a b) where
119+
120+ {-# INLINE size #-}
121+ size acc x =
122+ case x of
123+ Left field0 -> (size (acc + 1)) field0
124+ Right field0 -> (size (acc + 1)) field0
125+
126+ {-# INLINE deserialize #-}
127+ deserialize initialOffset arr endOffset = do
128+ (i0, tag) <- ((deserialize initialOffset) arr) endOffset
129+ case tag :: Word8 of
130+ 0 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
131+ pure (i1, Left a0)
132+ 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
133+ pure (i1, Right a0)
134+ _ -> error "Found invalid tag while peeking (Either a b)"
135+
136+ {-# INLINE serialize #-}
137+ serialize initialOffset arr val =
138+ case val of
139+ Left field0 -> do
140+ i0 <- ((serialize initialOffset) arr) (0 :: Word8)
141+ i1 <- ((serialize i0) arr) field0
142+ pure i1
143+ Right field0 -> do
144+ i0 <- ((serialize initialOffset) arr) (1 :: Word8)
145+ i1 <- ((serialize i0) arr) field0
146+ pure i1
147+
148+instance Serialize (Proxy a) where
149+
150+ {-# INLINE size #-}
151+ size acc _ = (acc + 1)
152+
153+ {-# INLINE deserialize #-}
154+ deserialize initialOffset _ _ = pure ((initialOffset + 1), Proxy)
155+
156+ {-# INLINE serialize #-}
157+ serialize initialOffset _ _ = pure (initialOffset + 1)
118
158
119--------------------------------------------------------------------------------
159--------------------------------------------------------------------------------
120−-- Creation
160+-- Integer
121--------------------------------------------------------------------------------
161--------------------------------------------------------------------------------
122
162
123−{-# NOINLINE nil #-}
163+data LiftedInteger
124−nil :: MutByteArray
164+ = LIS Int
125−nil = unsafePerformIO $ newByteArray 0
165+ | LIP (Array Word)
126−
166+ | LIN (Array Word)
167+
168+-- $(Serialize.deriveSerialize ''LiftedInteger)
169+instance Serialize LiftedInteger where
170+
127−{-# INLINE newByteArray #-}
171+ {-# INLINE size #-}
128−newByteArray :: Int -> IO MutByteArray
172+ size acc x =
129−newByteArray nbytes | nbytes < 0 =
173+ case x of
130− errorWithoutStackTrace "newByteArray: size must be >= 0"
174+ LIS field0 -> (size (acc + 1)) field0
131−newByteArray (I# nbytes) = IO $ \s ->
175+ LIP field0 -> (size (acc + 1)) field0
132− case newByteArray# nbytes s of
176+ LIN field0 -> (size (acc + 1)) field0
133− (# s', mbarr# #) ->
177+
134− let c = MutByteArray mbarr#
178+ {-# INLINE deserialize #-}
135− in (# s', c #)
179+ deserialize initialOffset arr endOffset = do
136−
180+ (i0, tag) <- ((deserialize initialOffset) arr) endOffset
137−{-# INLINE pinnedNewByteArray #-}
181+ case tag :: Word8 of
138−pinnedNewByteArray :: Int -> IO MutByteArray
182+ 0 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
139−pinnedNewByteArray nbytes | nbytes < 0 =
183+ pure (i1, LIS a0)
140− errorWithoutStackTrace "pinnedNewByteArray: size must be >= 0"
184+ 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
141−pinnedNewByteArray (I# nbytes) = IO $ \s ->
185+ pure (i1, LIP a0)
142− case newPinnedByteArray# nbytes s of
186+ 2 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
143− (# s', mbarr# #) ->
187+ pure (i1, LIN a0)
144− let c = MutByteArray mbarr#
188+ _ -> error "Found invalid tag while peeking (LiftedInteger)"
145− in (# s', c #)
189+
146−
190+ {-# INLINE serialize #-}
147−{-# INLINE pinnedNewAlignedBytes #-}
191+ serialize initialOffset arr val =
148−pinnedNewAlignedBytes :: Int -> Int -> IO MutByteArray
192+ case val of
149−pinnedNewAlignedBytes nbytes _align | nbytes < 0 =
193+ LIS field0 -> do
150− errorWithoutStackTrace "pinnedNewAlignedBytes: size must be >= 0"
194+ i0 <- ((serialize initialOffset) arr) (0 :: Word8)
151−pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
195+ i1 <- ((serialize i0) arr) field0
152− case newAlignedPinnedByteArray# nbytes align s of
196+ pure i1
153− (# s', mbarr# #) ->
197+ LIP field0 -> do
154− let c = MutByteArray mbarr#
198+ i0 <- ((serialize initialOffset) arr) (1 :: Word8)
155− in (# s', c #)
199+ i1 <- ((serialize i0) arr) field0
156−
200+ pure i1
157−{-# INLINE newBytesAs #-}
201+ LIN field0 -> do
158−newBytesAs :: PinnedState -> Int -> IO MutByteArray
202+ i0 <- ((serialize initialOffset) arr) (2 :: Word8)
159−newBytesAs Unpinned = newByteArray
203+ i1 <- ((serialize i0) arr) field0
160−newBytesAs Pinned = pinnedNewByteArray
204+ pure i1
161−
205+
162−-------------------------------------------------------------------------------
206+#if __GLASGOW_HASKELL__ >= 900
163−-- Copying
207+
164−-------------------------------------------------------------------------------
208+{-# INLINE liftInteger #-}
165−
209+liftInteger :: Integer -> LiftedInteger
166−-- | Put a sub range of a source array into a subrange of a destination array.
210+liftInteger (IS x) = LIS (I# x)
167−-- This is not safe as it does not check the bounds.
211+liftInteger (IP x) =
168−{-# INLINE putSliceUnsafe #-}
212+ LIP (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
169−putSliceUnsafe ::
213+liftInteger (IN x) =
170− MonadIO m
214+ LIN (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
171− => MutByteArray
215+
172− -> Int
216+{-# INLINE unliftInteger #-}
173− -> MutByteArray
217+unliftInteger :: LiftedInteger -> Integer
174− -> Int
218+unliftInteger (LIS (I# x)) = IS x
175− -> Int
219+unliftInteger (LIP (Array (MutByteArray x) _ _)) = IP (unsafeCoerce# x)
176− -> m ()
220+unliftInteger (LIN (Array (MutByteArray x) _ _)) = IN (unsafeCoerce# x)
177−putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
221+
178−#ifdef DEBUG
222+#else
179− srcLen <- sizeOfMutableByteArray src
223+
180− dstLen <- sizeOfMutableByteArray src
181− when (srcLen - srcStartBytes < lenBytes) $ error "SRC: Insufficient length."
182− when (dstLen - dstStartBytes < lenBytes) $ error "DST: Insufficient length."
183−#endif
184− let !(I# srcStartBytes#) = srcStartBytes
185− !(I# dstStartBytes#) = dstStartBytes
186− !(I# lenBytes#) = lenBytes
187− let arrS# = getMutableByteArray# src
188− arrD# = getMutableByteArray# dst
189− IO $ \s# -> (# copyMutableByteArray#
190− arrS# srcStartBytes# arrD# dstStartBytes# lenBytes# s#
191− , () #)
192−
193−-------------------------------------------------------------------------------
194−-- Pinning & Unpinning
195−-------------------------------------------------------------------------------
196−
197−{-# INLINE isPinned #-}
198−isPinned :: MutByteArray -> Bool
199−isPinned (MutByteArray arr#) =
200− let pinnedInt = I# (isMutableByteArrayPinned# arr#)
201− in pinnedInt /= 0
202−
203−
204−{-# INLINE cloneMutableArrayWith# #-}
205−cloneMutableArrayWith#
206− :: (Int# -> State# RealWorld -> (# State# RealWorld
207− , MutableByteArray# RealWorld #))
208− -> MutableByteArray# RealWorld
209− -> State# RealWorld
210− -> (# State# RealWorld, MutableByteArray# RealWorld #)
211−cloneMutableArrayWith# alloc# arr# s# =
212− case getSizeofMutableByteArray# arr# s# of
213− (# s1#, i# #) ->
214− case alloc# i# s1# of
215− (# s2#, arr1# #) ->
216− case copyMutableByteArray# arr# 0# arr1# 0# i# s2# of
217− s3# -> (# s3#, arr1# #)
218−
219−{-# INLINE pin #-}
224+{-# INLINE liftInteger #-}
220−pin :: MutByteArray -> IO MutByteArray
225+liftInteger :: Integer -> LiftedInteger
221−pin arr@(MutByteArray marr#) =
226+liftInteger (S# x) = LIS (I# x)
222− if isPinned arr
227+liftInteger (Jp# (BN# x)) =
223− then return arr
228+ LIP (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
224− else
229+liftInteger (Jn# (BN# x)) =
225−#ifdef DEBUG
230+ LIN (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
226− do
231+
227− -- XXX dump stack trace
228− trace ("pin: Copying array") (return ())
229−#endif
230− IO
231− $ \s# ->
232− case cloneMutableArrayWith# newPinnedByteArray# marr# s# of
233− (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
234−
235−{-# INLINE unpin #-}
232+{-# INLINE unliftInteger #-}
236−unpin :: MutByteArray -> IO MutByteArray
233+unliftInteger :: LiftedInteger -> Integer
237−unpin arr@(MutByteArray marr#) =
234+unliftInteger (LIS (I# x)) = S# x
238− if not (isPinned arr)
235+unliftInteger (LIP (Array (MutByteArray x) _ _)) =
239− then return arr
236+ Jp# (BN# (unsafeCoerce# x))
240− else
237+unliftInteger (LIN (Array (MutByteArray x) _ _)) =
241−#ifdef DEBUG
238+ Jn# (BN# (unsafeCoerce# x))
242− do
239+
243− -- XXX dump stack trace
244− trace ("unpin: Copying array") (return ())
245#endif
240#endif
246− IO
241+
247− $ \s# ->
242+instance Serialize Integer where
248− case cloneMutableArrayWith# newByteArray# marr# s# of
243+ {-# INLINE size #-}
249− (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
244+ size i a = size i (liftInteger a)
245+
246+ {-# INLINE deserialize #-}
247+ deserialize off arr end = fmap unliftInteger <$> deserialize off arr end
248+
249+ {-# INLINE serialize #-}
250+ serialize off arr val = serialize off arr (liftInteger val)
core/src/Streamly/Internal/Data/MutByteArray/Type.hsnew file+249
@@ -0,0 +1,249 @@
1+{-# LANGUAGE UnboxedTuples #-}
2+
3+-- |
4+-- Module : Streamly.Internal.Data.MutByteArray.Type
5+-- Copyright : (c) 2023 Composewell Technologies
6+-- License : BSD3-3-Clause
7+-- Maintainer : streamly@composewell.com
8+-- Stability : experimental
9+-- Portability : GHC
10+--
11+module Streamly.Internal.Data.MutByteArray.Type
12+ (
13+ -- ** MutByteArray
14+ MutByteArray(..)
15+ , MutableByteArray
16+ , getMutableByteArray#
17+
18+ -- ** Pinning
19+ , PinnedState(..)
20+ , isPinned
21+ , pin
22+ , unpin
23+
24+ -- ** Allocation
25+ , nil
26+ , newBytesAs
27+ , newByteArray
28+ , pinnedNewByteArray
29+ , pinnedNewAlignedBytes
30+
31+ -- ** Access
32+ , sizeOfMutableByteArray
33+ , putSliceUnsafe
34+ , asPtrUnsafe
35+ ) where
36+
37+import Control.Monad.IO.Class (MonadIO(..))
38+#ifdef DEBUG
39+import Debug.Trace (trace)
40+#endif
41+import GHC.Base (IO(..))
42+import System.IO.Unsafe (unsafePerformIO)
43+
44+import GHC.Exts
45+
46+--------------------------------------------------------------------------------
47+-- The ArrayContents type
48+--------------------------------------------------------------------------------
49+
50+data PinnedState
51+ = Pinned
52+ | Unpinned
53+
54+-- XXX can use UnliftedNewtypes
55+
56+-- | A lifted mutable byte array type wrapping @MutableByteArray# RealWorld@.
57+-- This is a low level array used to back high level unboxed arrays and
58+-- serialized data.
59+data MutByteArray = MutByteArray (MutableByteArray# RealWorld)
60+
61+{-# DEPRECATED MutableByteArray "Please use MutByteArray instead" #-}
62+type MutableByteArray = MutByteArray
63+
64+{-# INLINE getMutableByteArray# #-}
65+getMutableByteArray# :: MutByteArray -> MutableByteArray# RealWorld
66+getMutableByteArray# (MutByteArray mbarr) = mbarr
67+
68+-- | Return the size of the array in bytes.
69+{-# INLINE sizeOfMutableByteArray #-}
70+sizeOfMutableByteArray :: MutByteArray -> IO Int
71+sizeOfMutableByteArray (MutByteArray arr) =
72+ IO $ \s ->
73+ case getSizeofMutableByteArray# arr s of
74+ (# s1, i #) -> (# s1, I# i #)
75+
76+{-# INLINE touch #-}
77+touch :: MutByteArray -> IO ()
78+touch (MutByteArray contents) =
79+ IO $ \s -> case touch# contents s of s' -> (# s', () #)
80+
81+-- XXX We can provide another API for "unsafe" FFI calls passing an unlifted
82+-- pointer to the FFI call. For unsafe calls we do not need to pin the array.
83+-- We can pass an unlifted pointer to the FFI routine to avoid GC kicking in
84+-- before the pointer is wrapped.
85+--
86+-- From the GHC manual:
87+--
88+-- GHC, since version 8.4, guarantees that garbage collection will never occur
89+-- during an unsafe call, even in the bytecode interpreter, and further
90+-- guarantees that unsafe calls will be performed in the calling thread. Making
91+-- it safe to pass heap-allocated objects to unsafe functions.
92+
93+-- | Use a @MutByteArray@ as @Ptr a@. This is useful when we want to pass
94+-- an array as a pointer to some operating system call or to a "safe" FFI call.
95+--
96+-- If the array is not pinned it is copied to pinned memory before passing it
97+-- to the monadic action.
98+--
99+-- /Performance Notes:/ Forces a copy if the array is not pinned. It is advised
100+-- that the programmer keeps this in mind and creates a pinned array
101+-- opportunistically before this operation occurs, to avoid the cost of a copy
102+-- if possible.
103+--
104+-- /Unsafe/ because of direct pointer operations. The user must ensure that
105+-- they are writing within the legal bounds of the array.
106+--
107+-- /Pre-release/
108+--
109+{-# INLINE asPtrUnsafe #-}
110+asPtrUnsafe :: MonadIO m => MutByteArray -> (Ptr a -> m b) -> m b
111+asPtrUnsafe arr f = do
112+ contents <- liftIO $ pin arr
113+ let !ptr = Ptr (byteArrayContents#
114+ (unsafeCoerce# (getMutableByteArray# contents)))
115+ r <- f ptr
116+ liftIO $ touch contents
117+ return r
118+
119+--------------------------------------------------------------------------------
120+-- Creation
121+--------------------------------------------------------------------------------
122+
123+{-# NOINLINE nil #-}
124+nil :: MutByteArray
125+nil = unsafePerformIO $ newByteArray 0
126+
127+{-# INLINE newByteArray #-}
128+newByteArray :: Int -> IO MutByteArray
129+newByteArray nbytes | nbytes < 0 =
130+ errorWithoutStackTrace "newByteArray: size must be >= 0"
131+newByteArray (I# nbytes) = IO $ \s ->
132+ case newByteArray# nbytes s of
133+ (# s', mbarr# #) ->
134+ let c = MutByteArray mbarr#
135+ in (# s', c #)
136+
137+{-# INLINE pinnedNewByteArray #-}
138+pinnedNewByteArray :: Int -> IO MutByteArray
139+pinnedNewByteArray nbytes | nbytes < 0 =
140+ errorWithoutStackTrace "pinnedNewByteArray: size must be >= 0"
141+pinnedNewByteArray (I# nbytes) = IO $ \s ->
142+ case newPinnedByteArray# nbytes s of
143+ (# s', mbarr# #) ->
144+ let c = MutByteArray mbarr#
145+ in (# s', c #)
146+
147+{-# INLINE pinnedNewAlignedBytes #-}
148+pinnedNewAlignedBytes :: Int -> Int -> IO MutByteArray
149+pinnedNewAlignedBytes nbytes _align | nbytes < 0 =
150+ errorWithoutStackTrace "pinnedNewAlignedBytes: size must be >= 0"
151+pinnedNewAlignedBytes (I# nbytes) (I# align) = IO $ \s ->
152+ case newAlignedPinnedByteArray# nbytes align s of
153+ (# s', mbarr# #) ->
154+ let c = MutByteArray mbarr#
155+ in (# s', c #)
156+
157+{-# INLINE newBytesAs #-}
158+newBytesAs :: PinnedState -> Int -> IO MutByteArray
159+newBytesAs Unpinned = newByteArray
160+newBytesAs Pinned = pinnedNewByteArray
161+
162+-------------------------------------------------------------------------------
163+-- Copying
164+-------------------------------------------------------------------------------
165+
166+-- | Put a sub range of a source array into a subrange of a destination array.
167+-- This is not safe as it does not check the bounds.
168+{-# INLINE putSliceUnsafe #-}
169+putSliceUnsafe ::
170+ MonadIO m
171+ => MutByteArray
172+ -> Int
173+ -> MutByteArray
174+ -> Int
175+ -> Int
176+ -> m ()
177+putSliceUnsafe src srcStartBytes dst dstStartBytes lenBytes = liftIO $ do
178+#ifdef DEBUG
179+ srcLen <- sizeOfMutableByteArray src
180+ dstLen <- sizeOfMutableByteArray src
181+ when (srcLen - srcStartBytes < lenBytes) $ error "SRC: Insufficient length."
182+ when (dstLen - dstStartBytes < lenBytes) $ error "DST: Insufficient length."
183+#endif
184+ let !(I# srcStartBytes#) = srcStartBytes
185+ !(I# dstStartBytes#) = dstStartBytes
186+ !(I# lenBytes#) = lenBytes
187+ let arrS# = getMutableByteArray# src
188+ arrD# = getMutableByteArray# dst
189+ IO $ \s# -> (# copyMutableByteArray#
190+ arrS# srcStartBytes# arrD# dstStartBytes# lenBytes# s#
191+ , () #)
192+
193+-------------------------------------------------------------------------------
194+-- Pinning & Unpinning
195+-------------------------------------------------------------------------------
196+
197+{-# INLINE isPinned #-}
198+isPinned :: MutByteArray -> Bool
199+isPinned (MutByteArray arr#) =
200+ let pinnedInt = I# (isMutableByteArrayPinned# arr#)
201+ in pinnedInt /= 0
202+
203+
204+{-# INLINE cloneMutableArrayWith# #-}
205+cloneMutableArrayWith#
206+ :: (Int# -> State# RealWorld -> (# State# RealWorld
207+ , MutableByteArray# RealWorld #))
208+ -> MutableByteArray# RealWorld
209+ -> State# RealWorld
210+ -> (# State# RealWorld, MutableByteArray# RealWorld #)
211+cloneMutableArrayWith# alloc# arr# s# =
212+ case getSizeofMutableByteArray# arr# s# of
213+ (# s1#, i# #) ->
214+ case alloc# i# s1# of
215+ (# s2#, arr1# #) ->
216+ case copyMutableByteArray# arr# 0# arr1# 0# i# s2# of
217+ s3# -> (# s3#, arr1# #)
218+
219+{-# INLINE pin #-}
220+pin :: MutByteArray -> IO MutByteArray
221+pin arr@(MutByteArray marr#) =
222+ if isPinned arr
223+ then return arr
224+ else
225+#ifdef DEBUG
226+ do
227+ -- XXX dump stack trace
228+ trace ("pin: Copying array") (return ())
229+#endif
230+ IO
231+ $ \s# ->
232+ case cloneMutableArrayWith# newPinnedByteArray# marr# s# of
233+ (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
234+
235+{-# INLINE unpin #-}
236+unpin :: MutByteArray -> IO MutByteArray
237+unpin arr@(MutByteArray marr#) =
238+ if not (isPinned arr)
239+ then return arr
240+ else
241+#ifdef DEBUG
242+ do
243+ -- XXX dump stack trace
244+ trace ("unpin: Copying array") (return ())
245+#endif
246+ IO
247+ $ \s# ->
248+ case cloneMutableArrayWith# newByteArray# marr# s# of
249+ (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
core/src/Streamly/Internal/Data/Serialize.hsdeleted−250
@@ -1,250 +0,0 @@
1−{-# LANGUAGE TemplateHaskell #-}
2−
3−-- This is required as all the instances in this module are orphan instances.
4−{-# OPTIONS_GHC -fno-warn-orphans #-}
5−
6−-- |
7−-- Module : Streamly.Internal.Data.Serialize
8−-- Copyright : (c) 2023 Composewell Technologies
9−-- License : BSD3-3-Clause
10−-- Maintainer : streamly@composewell.com
11−-- Portability : GHC
12−--
13−
14−module Streamly.Internal.Data.Serialize
15− (
16− -- * MutByteArray
17− module Streamly.Internal.Data.MutByteArray
18− -- * Unbox
19− , module Streamly.Internal.Data.Unbox
20− , module Streamly.Internal.Data.Unbox.TH
21− -- * Serialize
22− , module Streamly.Internal.Data.Serialize.Type
23− -- * Serialize TH
24− , module Streamly.Internal.Data.Serialize.TH
25− ) where
26−
27−--------------------------------------------------------------------------------
28−-- Imports
29−--------------------------------------------------------------------------------
30−
31−import Data.Proxy (Proxy(..))
32−import Streamly.Internal.Data.Array (Array(..))
33−import GHC.Exts (Int(..), sizeofByteArray#, unsafeCoerce#)
34−import GHC.Word (Word8)
35−
36−#if __GLASGOW_HASKELL__ >= 900
37−import GHC.Num.Integer (Integer(..))
38−#else
39−import GHC.Integer.GMP.Internals (Integer(..), BigNat(..))
40−#endif
41−
42−import Streamly.Internal.Data.MutByteArray
43−import Streamly.Internal.Data.Serialize.TH
44−import Streamly.Internal.Data.Serialize.Type
45−import Streamly.Internal.Data.Unbox
46−import Streamly.Internal.Data.Unbox.TH
47−
48−--------------------------------------------------------------------------------
49−-- Common instances
50−--------------------------------------------------------------------------------
51−
52−-- Note
53−-- ====
54−--
55−-- Even a non-functional change such as changing the order of constructors will
56−-- change the instance derivation.
57−--
58−-- This will not pose a problem if both, encode, and decode are done by the same
59−-- version of the application. There *might* be a problem if version that
60−-- encodes differs from the version that decodes.
61−--
62−-- We need to add some compatibility tests using different versions of
63−-- dependencies.
64−--
65−-- Although such chages for the most basic types won't happen we need to detect
66−-- if it ever happens.
67−--
68−-- Should we worry about these kind of changes and this kind of compatibility?
69−-- This is a problem for all types of derivations that depend on the order of
70−-- constructors, for example, Enum.
71−
72−-- Note on Windows build
73−-- =====================
74−--
75−-- On Windows, having template haskell splices here fail the build with the
76−-- following error:
77−--
78−-- @
79−-- addLibrarySearchPath: C:\... (Win32 error 3): The system cannot find the path specified.
80−-- @
81−--
82−-- The error might be irrelavant but having these splices triggers it. We should
83−-- either fix the problem or avoid the use to template haskell splices in this
84−-- file.
85−--
86−-- Similar issue: https://github.com/haskell/cabal/issues/4741
87−
88−-- $(Serialize.deriveSerialize ''Maybe)
89−instance Serialize a => Serialize (Maybe a) where
90−
91− {-# INLINE size #-}
92− size acc x =
93− case x of
94− Nothing -> (acc + 1)
95− Just field0 -> (size (acc + 1)) field0
96−
97− {-# INLINE deserialize #-}
98− deserialize initialOffset arr endOffset = do
99− (i0, tag) <- ((deserialize initialOffset) arr) endOffset
100− case tag :: Word8 of
101− 0 -> pure (i0, Nothing)
102− 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
103− pure (i1, Just a0)
104− _ -> error "Found invalid tag while peeking (Maybe a)"
105−
106− {-# INLINE serialize #-}
107− serialize initialOffset arr val =
108− case val of
109− Nothing -> do
110− i0 <- ((serialize initialOffset) arr) (0 :: Word8)
111− pure i0
112− Just field0 -> do
113− i0 <- ((serialize initialOffset) arr) (1 :: Word8)
114− i1 <- ((serialize i0) arr) field0
115− pure i1
116−
117−-- $(Serialize.deriveSerialize ''Either)
118−instance (Serialize a, Serialize b) => Serialize (Either a b) where
119−
120− {-# INLINE size #-}
121− size acc x =
122− case x of
123− Left field0 -> (size (acc + 1)) field0
124− Right field0 -> (size (acc + 1)) field0
125−
126− {-# INLINE deserialize #-}
127− deserialize initialOffset arr endOffset = do
128− (i0, tag) <- ((deserialize initialOffset) arr) endOffset
129− case tag :: Word8 of
130− 0 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
131− pure (i1, Left a0)
132− 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
133− pure (i1, Right a0)
134− _ -> error "Found invalid tag while peeking (Either a b)"
135−
136− {-# INLINE serialize #-}
137− serialize initialOffset arr val =
138− case val of
139− Left field0 -> do
140− i0 <- ((serialize initialOffset) arr) (0 :: Word8)
141− i1 <- ((serialize i0) arr) field0
142− pure i1
143− Right field0 -> do
144− i0 <- ((serialize initialOffset) arr) (1 :: Word8)
145− i1 <- ((serialize i0) arr) field0
146− pure i1
147−
148−instance Serialize (Proxy a) where
149−
150− {-# INLINE size #-}
151− size acc _ = (acc + 1)
152−
153− {-# INLINE deserialize #-}
154− deserialize initialOffset _ _ = pure ((initialOffset + 1), Proxy)
155−
156− {-# INLINE serialize #-}
157− serialize initialOffset _ _ = pure (initialOffset + 1)
158−
159−--------------------------------------------------------------------------------
160−-- Integer
161−--------------------------------------------------------------------------------
162−
163−data LiftedInteger
164− = LIS Int
165− | LIP (Array Word)
166− | LIN (Array Word)
167−
168−-- $(Serialize.deriveSerialize ''LiftedInteger)
169−instance Serialize LiftedInteger where
170−
171− {-# INLINE size #-}
172− size acc x =
173− case x of
174− LIS field0 -> (size (acc + 1)) field0
175− LIP field0 -> (size (acc + 1)) field0
176− LIN field0 -> (size (acc + 1)) field0
177−
178− {-# INLINE deserialize #-}
179− deserialize initialOffset arr endOffset = do
180− (i0, tag) <- ((deserialize initialOffset) arr) endOffset
181− case tag :: Word8 of
182− 0 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
183− pure (i1, LIS a0)
184− 1 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
185− pure (i1, LIP a0)
186− 2 -> do (i1, a0) <- ((deserialize i0) arr) endOffset
187− pure (i1, LIN a0)
188− _ -> error "Found invalid tag while peeking (LiftedInteger)"
189−
190− {-# INLINE serialize #-}
191− serialize initialOffset arr val =
192− case val of
193− LIS field0 -> do
194− i0 <- ((serialize initialOffset) arr) (0 :: Word8)
195− i1 <- ((serialize i0) arr) field0
196− pure i1
197− LIP field0 -> do
198− i0 <- ((serialize initialOffset) arr) (1 :: Word8)
199− i1 <- ((serialize i0) arr) field0
200− pure i1
201− LIN field0 -> do
202− i0 <- ((serialize initialOffset) arr) (2 :: Word8)
203− i1 <- ((serialize i0) arr) field0
204− pure i1
205−
206−#if __GLASGOW_HASKELL__ >= 900
207−
208−{-# INLINE liftInteger #-}
209−liftInteger :: Integer -> LiftedInteger
210−liftInteger (IS x) = LIS (I# x)
211−liftInteger (IP x) =
212− LIP (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
213−liftInteger (IN x) =
214− LIN (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
215−
216−{-# INLINE unliftInteger #-}
217−unliftInteger :: LiftedInteger -> Integer
218−unliftInteger (LIS (I# x)) = IS x
219−unliftInteger (LIP (Array (MutByteArray x) _ _)) = IP (unsafeCoerce# x)
220−unliftInteger (LIN (Array (MutByteArray x) _ _)) = IN (unsafeCoerce# x)
221−
222−#else
223−
224−{-# INLINE liftInteger #-}
225−liftInteger :: Integer -> LiftedInteger
226−liftInteger (S# x) = LIS (I# x)
227−liftInteger (Jp# (BN# x)) =
228− LIP (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
229−liftInteger (Jn# (BN# x)) =
230− LIN (Array (MutByteArray (unsafeCoerce# x)) 0 (I# (sizeofByteArray# x)))
231−
232−{-# INLINE unliftInteger #-}
233−unliftInteger :: LiftedInteger -> Integer
234−unliftInteger (LIS (I# x)) = S# x
235−unliftInteger (LIP (Array (MutByteArray x) _ _)) =
236− Jp# (BN# (unsafeCoerce# x))
237−unliftInteger (LIN (Array (MutByteArray x) _ _)) =
238− Jn# (BN# (unsafeCoerce# x))
239−
240−#endif
241−
242−instance Serialize Integer where
243− {-# INLINE size #-}
244− size i a = size i (liftInteger a)
245−
246− {-# INLINE deserialize #-}
247− deserialize off arr end = fmap unliftInteger <$> deserialize off arr end
248−
249− {-# INLINE serialize #-}
250− serialize off arr val = serialize off arr (liftInteger val)
core/src/Streamly/Internal/Data/Serialize/TH/RecHeader.hs+1−1
@@ -27,7 +27,7 @@ import Data.Maybe (fromJust)
27import Language.Haskell.TH
27import Language.Haskell.TH
28import Streamly.Internal.Data.Serialize.Type (Serialize(..))
28import Streamly.Internal.Data.Serialize.Type (Serialize(..))
29import Data.Foldable (foldlM)
29import Data.Foldable (foldlM)
30−import Streamly.Internal.Data.MutByteArray (MutByteArray)
30+import Streamly.Internal.Data.MutByteArray.Type (MutByteArray)
31import Data.Proxy (Proxy(..))
31import Data.Proxy (Proxy(..))
32
32
33import qualified Streamly.Internal.Data.Unbox as Unbox
33import qualified Streamly.Internal.Data.Unbox as Unbox
core/src/Streamly/Internal/Data/Serialize/Type.hs+7−6
@@ -28,14 +28,15 @@ import Control.Exception (assert)
28import Data.List (foldl')
28import Data.List (foldl')
29import Data.Proxy (Proxy (..))
29import Data.Proxy (Proxy (..))
30import Streamly.Internal.Data.Unbox (Unbox)
30import Streamly.Internal.Data.Unbox (Unbox)
31−import Streamly.Internal.Data.MutByteArray (MutByteArray(..), PinnedState(..))
31+import Streamly.Internal.Data.MutByteArray.Type
32+ (MutByteArray(..), PinnedState(..))
32import Streamly.Internal.Data.Array.Type (Array(..))
33import Streamly.Internal.Data.Array.Type (Array(..))
33import Streamly.Internal.System.IO (unsafeInlineIO)
34import Streamly.Internal.System.IO (unsafeInlineIO)
34import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
35import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
35import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
36import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
36import GHC.Stable (StablePtr(..))
37import GHC.Stable (StablePtr(..))
37
38
38−import qualified Streamly.Internal.Data.MutByteArray as MBA
39+import qualified Streamly.Internal.Data.MutByteArray.Type as MBA
39import qualified Streamly.Internal.Data.Unbox as Unbox
40import qualified Streamly.Internal.Data.Unbox as Unbox
40import qualified Streamly.Internal.Data.Array as Array
41import qualified Streamly.Internal.Data.Array as Array
41import qualified Streamly.Internal.Data.MutArray as MutArray
42import qualified Streamly.Internal.Data.MutArray as MutArray
@@ -95,17 +96,17 @@ import GHC.Exts
95-- :}
96-- :}
96--
97--
97-- @
98-- @
98−-- import Streamly.Data.Serialize (deriveSerialize)
99+-- import Streamly.Data.MutByteArray (deriveSerialize)
99-- \$(deriveSerialize [d|instance Serialize Object|])
100-- \$(deriveSerialize [d|instance Serialize Object|])
100-- @
101-- @
101--
102--
102−-- See 'Streamly.Data.Serialize.deriveSerialize' and
103+-- See 'Streamly.Data.MutByteArray.deriveSerialize' and
103−-- 'Streamly.Data.Serialize.deriveSerializeWith' for more information on
104+-- 'Streamly.Data.MutByteArray.deriveSerializeWith' for more information on
104-- deriving using Template Haskell.
105-- deriving using Template Haskell.
105--
106--
106-- Here is an example of a manual instance.
107-- Here is an example of a manual instance.
107--
108--
108−-- >>> import Streamly.Data.Serialize (Serialize(..))
109+-- >>> import Streamly.Data.MutByteArray (Serialize(..))
109--
110--
110-- >>> :{
111-- >>> :{
111-- instance Serialize Object where
112-- instance Serialize Object where
core/src/Streamly/Internal/Data/Unbox.hs+4−4
@@ -67,7 +67,7 @@ import GHC.Exts
67import GHC.TypeLits
67import GHC.TypeLits
68import Prelude hiding (read)
68import Prelude hiding (read)
69
69
70−import Streamly.Internal.Data.MutByteArray (MutByteArray(..))
70+import Streamly.Internal.Data.MutByteArray.Type (MutByteArray(..))
71
71
72--------------------------------------------------------------------------------
72--------------------------------------------------------------------------------
73-- The Unbox type class
73-- The Unbox type class
@@ -196,17 +196,17 @@ import Streamly.Internal.Data.MutByteArray (MutByteArray(..))
196-- } deriving Generic
196-- } deriving Generic
197-- :}
197-- :}
198--
198--
199−-- >>> import Streamly.Data.Serialize (Unbox(..))
199+-- >>> import Streamly.Data.MutByteArray (Unbox(..))
200-- >>> instance Unbox Object
200-- >>> instance Unbox Object
201--
201--
202-- To derive the instance via Template Haskell:
202-- To derive the instance via Template Haskell:
203--
203--
204-- @
204-- @
205−-- import Streamly.Data.Serialize (deriveUnbox)
205+-- import Streamly.Data.MutByteArray (deriveUnbox)
206-- \$(deriveUnbox [d|instance Unbox Object|])
206-- \$(deriveUnbox [d|instance Unbox Object|])
207-- @
207-- @
208--
208--
209−-- See 'Streamly.Data.Serialize.deriveUnbox' for more information on deriving
209+-- See 'Streamly.Data.MutByteArray.deriveUnbox' for more information on deriving
210-- using Template Haskell.
210-- using Template Haskell.
211--
211--
212-- If you want to write the instance manually:
212-- If you want to write the instance manually:
core/streamly-core.cabal+5−4
@@ -292,7 +292,7 @@ library
292 , Streamly.Internal.Data.Producer
292 , Streamly.Internal.Data.Producer
293
293
294 -- streamly-core-array-types
294 -- streamly-core-array-types
295− , Streamly.Internal.Data.Serialize
295+ , Streamly.Internal.Data.MutByteArray
296 , Streamly.Internal.Data.Serialize.Parser
296 , Streamly.Internal.Data.Serialize.Parser
297 , Streamly.Internal.Data.Serialize.Stream
297 , Streamly.Internal.Data.Serialize.Stream
298
298
@@ -367,7 +367,7 @@ library
367 -- streamly-core released modules in alphabetic order
367 -- streamly-core released modules in alphabetic order
368 -- NOTE: these must be added to streamly.cabal as well
368 -- NOTE: these must be added to streamly.cabal as well
369 , Streamly.Console.Stdio
369 , Streamly.Console.Stdio
370− , Streamly.Data.Serialize
370+ , Streamly.Data.MutByteArray
371 , Streamly.Data.Array
371 , Streamly.Data.Array
372 , Streamly.Data.Array.Generic
372 , Streamly.Data.Array.Generic
373 , Streamly.Data.MutArray
373 , Streamly.Data.MutArray
@@ -421,9 +421,10 @@ library
421 , Streamly.Internal.Data.Array.Type
421 , Streamly.Internal.Data.Array.Type
422
422
423 -- Unboxed IORef
423 -- Unboxed IORef
424− , Streamly.Internal.Data.MutByteArray
425− , Streamly.Internal.Data.Unbox
426 , Streamly.Internal.Data.IORef.Unboxed
424 , Streamly.Internal.Data.IORef.Unboxed
425+
426+ , Streamly.Internal.Data.MutByteArray.Type
427+ , Streamly.Internal.Data.Unbox
427 , Streamly.Internal.Data.Unbox.TH
428 , Streamly.Internal.Data.Unbox.TH
428 , Streamly.Internal.Data.Serialize.Type
429 , Streamly.Internal.Data.Serialize.Type
429 , Streamly.Internal.Data.Serialize.TH
430 , Streamly.Internal.Data.Serialize.TH
src/Streamly/Internal/Data/Stream/IsStream/Common.hs+1−1
@@ -79,7 +79,7 @@ import Streamly.Internal.Data.Stream.IsStream.Type
79import Streamly.Internal.Data.Stream.Serial (SerialT)
79import Streamly.Internal.Data.Stream.Serial (SerialT)
80import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
80import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
81import Streamly.Internal.System.IO (defaultChunkSize)
81import Streamly.Internal.System.IO (defaultChunkSize)
82−import Streamly.Data.Serialize (Unbox)
82+import Streamly.Data.MutByteArray (Unbox)
83
83
84import qualified Streamly.Internal.Data.Array as A
84import qualified Streamly.Internal.Data.Array as A
85import qualified Streamly.Internal.Data.Stream.Async as Async
85import qualified Streamly.Internal.Data.Stream.Async as Async
src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+1−1
@@ -164,7 +164,7 @@ import Streamly.Internal.Data.Stream.IsStream.Common
164import Streamly.Internal.Data.Stream.IsStream.Type
164import Streamly.Internal.Data.Stream.IsStream.Type
165 (IsStream, fromStreamD, toStreamD)
165 (IsStream, fromStreamD, toStreamD)
166import Streamly.Internal.Data.Stream.Serial (SerialT)
166import Streamly.Internal.Data.Stream.Serial (SerialT)
167−import Streamly.Data.Serialize (Unbox)
167+import Streamly.Data.MutByteArray (Unbox)
168
168
169import qualified Streamly.Internal.Data.Array as A
169import qualified Streamly.Internal.Data.Array as A
170import qualified Streamly.Internal.Data.Stream.Serial as Stream
170import qualified Streamly.Internal.Data.Stream.Serial as Stream
src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+1−1
@@ -103,7 +103,7 @@ import Streamly.Internal.Data.Stream.IsStream.Type
103 (IsStream (..), fromSerial, consM, fromStreamD)
103 (IsStream (..), fromSerial, consM, fromStreamD)
104import Streamly.Internal.Data.Stream.Serial (SerialT, WSerialT)
104import Streamly.Internal.Data.Stream.Serial (SerialT, WSerialT)
105import Streamly.Internal.Data.Time.Units (AbsTime , RelTime64, addToAbsTime64)
105import Streamly.Internal.Data.Time.Units (AbsTime , RelTime64, addToAbsTime64)
106−import Streamly.Data.Serialize (Unbox)
106+import Streamly.Data.MutByteArray (Unbox)
107
107
108import qualified Streamly.Internal.Data.MutArray as Unboxed
108import qualified Streamly.Internal.Data.MutArray as Unboxed
109 (pollIntIORef, IORef)
109 (pollIntIORef, IORef)
src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+1−1
@@ -180,7 +180,7 @@ import Streamly.Internal.Data.Stream.Serial(toStreamK)
180import Streamly.Internal.Data.Time.Units
180import Streamly.Internal.Data.Time.Units
181 ( AbsTime, MilliSecond64(..), addToAbsTime, toRelTime
181 ( AbsTime, MilliSecond64(..), addToAbsTime, toRelTime
182 , toAbsTime)
182 , toAbsTime)
183−import Streamly.Data.Serialize (Unbox)
183+import Streamly.Data.MutByteArray (Unbox)
184
184
185import qualified Data.Heap as H
185import qualified Data.Heap as H
186import qualified Streamly.Internal.Data.Unfold as Unfold
186import qualified Streamly.Internal.Data.Unfold as Unfold
src/Streamly/Internal/Network/Inet/TCP.hs+1−1
@@ -111,7 +111,7 @@ import Streamly.Internal.Data.Array (pinnedWriteNUnsafe)
111import Streamly.Internal.Data.Fold ( Fold(..) )
111import Streamly.Internal.Data.Fold ( Fold(..) )
112import Streamly.Data.Stream (Stream)
112import Streamly.Data.Stream (Stream)
113import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
113import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
114−import Streamly.Data.Serialize (Unbox)
114+import Streamly.Data.MutByteArray (Unbox)
115import Streamly.Data.Unfold (Unfold)
115import Streamly.Data.Unfold (Unfold)
116import Streamly.Internal.Network.Socket (SockSpec(..), accept, acceptor)
116import Streamly.Internal.Network.Socket (SockSpec(..), accept, acceptor)
117import Streamly.Internal.System.IO (defaultChunkSize)
117import Streamly.Internal.System.IO (defaultChunkSize)
src/Streamly/Internal/Network/Socket.hs+1−1
@@ -72,7 +72,7 @@ import Control.Monad.IO.Class (MonadIO(..))
72import Data.Maybe (isNothing, fromJust)
72import Data.Maybe (isNothing, fromJust)
73import Data.Word (Word8)
73import Data.Word (Word8)
74import Foreign.Ptr (plusPtr, Ptr, castPtr)
74import Foreign.Ptr (plusPtr, Ptr, castPtr)
75−import Streamly.Data.Serialize (Unbox)
75+import Streamly.Data.MutByteArray (Unbox)
76import Network.Socket
76import Network.Socket
77 (Socket, SocketOption(..), Family(..), SockAddr(..),
77 (Socket, SocketOption(..), Family(..), SockAddr(..),
78 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
78 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
src/Streamly/Network/Socket.hs+1−1
@@ -138,7 +138,7 @@ import Data.Word (Word8)
138import Network.Socket (Socket, SockAddr)
138import Network.Socket (Socket, SockAddr)
139import Streamly.Internal.Data.Unfold (Unfold(..))
139import Streamly.Internal.Data.Unfold (Unfold(..))
140import Streamly.Internal.Data.Array (Array(..))
140import Streamly.Internal.Data.Array (Array(..))
141−import Streamly.Data.Serialize (Unbox)
141+import Streamly.Data.MutByteArray (Unbox)
142
142
143import Streamly.Internal.Network.Socket hiding (accept, read, readChunks)
143import Streamly.Internal.Network.Socket hiding (accept, read, readChunks)
144import Prelude hiding (read)
144import Prelude hiding (read)
test/Streamly/Test/Data/Array.hs+1−1
@@ -14,7 +14,7 @@ import Data.Proxy (Proxy(..))
14import Data.Word(Word8)
14import Data.Word(Word8)
15import Foreign.Storable (peek)
15import Foreign.Storable (peek)
16import GHC.Ptr (plusPtr)
16import GHC.Ptr (plusPtr)
17−import Streamly.Internal.Data.Serialize (Unbox, sizeOf)
17+import Streamly.Internal.Data.MutByteArray (Unbox, sizeOf)
18import Streamly.Internal.Data.MutArray (MutArray)
18import Streamly.Internal.Data.MutArray (MutArray)
19import Test.QuickCheck (chooseInt, listOf)
19import Test.QuickCheck (chooseInt, listOf)
20
20
test/Streamly/Test/Data/MutArray.hs+1−1
@@ -13,7 +13,7 @@ import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
13import GHC.Real (Ratio(..))
13import GHC.Real (Ratio(..))
14import GHC.Stable (StablePtr(..))
14import GHC.Stable (StablePtr(..))
15import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
15import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
16−import Streamly.Internal.Data.Serialize (Unbox)
16+import Streamly.Internal.Data.MutByteArray (Unbox)
17import Streamly.Test.Common (chooseInt)
17import Streamly.Test.Common (chooseInt)
18import Test.Hspec (hspec, describe, it)
18import Test.Hspec (hspec, describe, it)
19import Test.Hspec.QuickCheck
19import Test.Hspec.QuickCheck
test/Streamly/Test/Data/Serialize.hs+3−3
@@ -22,15 +22,15 @@ module Streamly.Test.Data.Serialize (main) where
22--------------------------------------------------------------------------------
22--------------------------------------------------------------------------------
23
23
24import System.Random (randomRIO)
24import System.Random (randomRIO)
25−import Streamly.Internal.Data.Serialize (MutByteArray, newByteArray)
25+import Streamly.Internal.Data.MutByteArray (MutByteArray, newByteArray)
26import GHC.Generics (Generic)
26import GHC.Generics (Generic)
27−import Streamly.Data.Serialize (Serialize)
27+import Streamly.Data.MutByteArray (Serialize)
28import Streamly.Test.Data.Serialize.TH (genDatatype)
28import Streamly.Test.Data.Serialize.TH (genDatatype)
29
29
30import Data.Functor.Identity (Identity (..))
30import Data.Functor.Identity (Identity (..))
31
31
32import qualified Streamly.Internal.Data.Array as Array
32import qualified Streamly.Internal.Data.Array as Array
33−import qualified Streamly.Internal.Data.Serialize as Serialize
33+import qualified Streamly.Internal.Data.MutByteArray as Serialize
34
34
35import qualified Streamly.Test.Data.Serialize.CompatV0 as CompatV0
35import qualified Streamly.Test.Data.Serialize.CompatV0 as CompatV0
36import qualified Streamly.Test.Data.Serialize.CompatV1 as CompatV1
36import qualified Streamly.Test.Data.Serialize.CompatV1 as CompatV1
test/Streamly/Test/Data/Serialize/CompatV0.hs+2−2
@@ -10,8 +10,8 @@ module Streamly.Test.Data.Serialize.CompatV0
10--------------------------------------------------------------------------------
10--------------------------------------------------------------------------------
11
11
12import Test.QuickCheck (Arbitrary, arbitrary, elements)
12import Test.QuickCheck (Arbitrary, arbitrary, elements)
13−import Streamly.Internal.Data.Serialize (Serialize)
13+import Streamly.Internal.Data.MutByteArray (Serialize)
14−import qualified Streamly.Internal.Data.Serialize as Serialize
14+import qualified Streamly.Internal.Data.MutByteArray as Serialize
15
15
16--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
17-- Types
17-- Types
test/Streamly/Test/Data/Serialize/CompatV1.hs+2−2
@@ -10,8 +10,8 @@ module Streamly.Test.Data.Serialize.CompatV1
10--------------------------------------------------------------------------------
10--------------------------------------------------------------------------------
11
11
12import Test.QuickCheck (Arbitrary, arbitrary, elements)
12import Test.QuickCheck (Arbitrary, arbitrary, elements)
13−import Streamly.Internal.Data.Serialize (Serialize)
13+import Streamly.Internal.Data.MutByteArray (Serialize)
14−import qualified Streamly.Internal.Data.Serialize as Serialize
14+import qualified Streamly.Internal.Data.MutByteArray as Serialize
15
15
16--------------------------------------------------------------------------------
16--------------------------------------------------------------------------------
17-- Types
17-- Types
test/Streamly/Test/Data/Unbox.hs+1−1
@@ -27,7 +27,7 @@ import Data.Proxy (Proxy(..))
27import GHC.Generics (Generic, Rep(..))
27import GHC.Generics (Generic, Rep(..))
28import GHC.Real (Ratio(..))
28import GHC.Real (Ratio(..))
29
29
30−import Streamly.Internal.Data.Serialize
30+import Streamly.Internal.Data.MutByteArray
31
31
32import Test.Hspec as H
32import Test.Hspec as H
33
33
test/Streamly/Test/Data/Unbox/TH.hs+1−1
@@ -15,7 +15,7 @@ module Streamly.Test.Data.Unbox.TH (main) where
15--------------------------------------------------------------------------------
15--------------------------------------------------------------------------------
16
16
17import Data.Proxy (Proxy(..))
17import Data.Proxy (Proxy(..))
18−import Streamly.Internal.Data.Serialize
18+import Streamly.Internal.Data.MutByteArray
19
19
20import Test.Hspec as H
20import Test.Hspec as H
21
21
test/Streamly/Test/Prelude/Serial.hs+1−1
@@ -26,7 +26,7 @@ import Data.Maybe ( isJust, fromJust )
26import Data.Word (Word8)
26import Data.Word (Word8)
27import Data.Semigroup (Sum(..), getSum)
27import Data.Semigroup (Sum(..), getSum)
28import Foreign.Storable (Storable)
28import Foreign.Storable (Storable)
29−import Streamly.Internal.Data.Serialize (Unbox)
29+import Streamly.Internal.Data.MutByteArray (Unbox)
30import Test.Hspec.QuickCheck
30import Test.Hspec.QuickCheck
31import Test.QuickCheck
31import Test.QuickCheck
32 ( Gen
32 ( Gen