on master help sign in
Rename Data.Serialize to Data.MutByteArray
commit6705ec00b950c539017312631805ab58fe97ba7f authorHarendra Kumar <harendra@composewell.com> dateSat 25 Nov 2023, 09:13 (3 years ago) parentbc14a48
32 files changed+520−517
benchmark/Streamly/Benchmark/Data/Array/Stream.hs+1−1 benchmark/Streamly/Benchmark/Data/Serialize.hs+1−1 benchmark/Streamly/Benchmark/Data/Serialize/RecCompatible.hs+2−2 benchmark/Streamly/Benchmark/Data/Serialize/RecNonCompatible.hs+1−1 benchmark/Streamly/Benchmark/Data/Serialize/TH.hs+1−1 benchmark/Streamly/Benchmark/Unicode/Stream.hs+1−1 core/src/Streamly/Data/Serialize.hs → core/src/Streamly/Data/MutByteArray.hs+3−3 core/src/Streamly/Internal/Data/Array/Type.hs+1−1 core/src/Streamly/Internal/Data/IORef/Unboxed.hs+2−1 core/src/Streamly/Internal/Data/MutArray/Type.hs+2−2 core/src/Streamly/Internal/Data/MutByteArray.hs+220−219 core/src/Streamly/Internal/Data/MutByteArray/Type.hs+249 core/src/Streamly/Internal/Data/Serialize.hs−250 core/src/Streamly/Internal/Data/Serialize/TH/RecHeader.hs+1−1 core/src/Streamly/Internal/Data/Serialize/Type.hs+7−6 core/src/Streamly/Internal/Data/Unbox.hs+4−4 core/streamly-core.cabal+5−4 src/Streamly/Internal/Data/Stream/IsStream/Common.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+1−1 src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+1−1 src/Streamly/Internal/Network/Inet/TCP.hs+1−1 src/Streamly/Internal/Network/Socket.hs+1−1 src/Streamly/Network/Socket.hs+1−1 test/Streamly/Test/Data/Array.hs+1−1 test/Streamly/Test/Data/MutArray.hs+1−1 test/Streamly/Test/Data/Serialize.hs+3−3 test/Streamly/Test/Data/Serialize/CompatV0.hs+2−2 test/Streamly/Test/Data/Serialize/CompatV1.hs+2−2 test/Streamly/Test/Data/Unbox.hs+1−1 test/Streamly/Test/Data/Unbox/TH.hs+1−1 test/Streamly/Test/Prelude/Serial.hs+1−1
benchmark/Streamly/Benchmark/Data/Array/Stream.hs+1−1
@@ -53,7 +53,7 @@ import Streamly.Benchmark.Common.Handle
5353import Control.Monad.IO.Class (MonadIO)
5454
5555#ifdef INSPECTION
56−import Streamly.Internal.Data.Serialize (Unbox)
56+import Streamly.Internal.Data.MutByteArray (Unbox)
5757import Streamly.Internal.Data.Stream (Step(..))
5858import Test.Inspection
5959#endif
benchmark/Streamly/Benchmark/Data/Serialize.hs+1−1
@@ -24,7 +24,7 @@ import System.Random (randomRIO)
2424import Test.QuickCheck (Arbitrary, arbitrary)
2525#endif
2626
27−import Streamly.Internal.Data.Serialize hiding (encode)
27+import Streamly.Internal.Data.MutByteArray hiding (encode)
2828#ifdef USE_UNBOX
2929import Data.Proxy (Proxy(..))
3030#else
benchmark/Streamly/Benchmark/Data/Serialize/RecCompatible.hs+2−2
@@ -9,10 +9,10 @@ module Streamly.Benchmark.Data.Serialize.RecCompatible
99-- Imports
1010--------------------------------------------------------------------------------
1111
12−import Streamly.Internal.Data.Serialize (Serialize)
12+import Streamly.Internal.Data.MutByteArray (Serialize)
1313import Streamly.Benchmark.Data.Serialize.TH (genLargeRecord)
1414
15−import qualified Streamly.Internal.Data.Serialize as Serialize
15+import qualified Streamly.Internal.Data.MutByteArray as Serialize
1616
1717--------------------------------------------------------------------------------
1818-- Code
benchmark/Streamly/Benchmark/Data/Serialize/RecNonCompatible.hs+1−1
@@ -11,7 +11,7 @@ module Streamly.Benchmark.Data.Serialize.RecNonCompatible
1111
1212import Streamly.Benchmark.Data.Serialize.TH (genLargeRecord)
1313
14−import qualified Streamly.Internal.Data.Serialize as Serialize
14+import qualified Streamly.Internal.Data.MutByteArray as Serialize
1515
1616--------------------------------------------------------------------------------
1717-- Code
benchmark/Streamly/Benchmark/Data/Serialize/TH.hs+1−1
@@ -8,7 +8,7 @@ module Streamly.Benchmark.Data.Serialize.TH (genLargeRecord) where
88
99import Language.Haskell.TH
1010
11−import Streamly.Internal.Data.Serialize (makeI)
11+import Streamly.Internal.Data.MutByteArray (makeI)
1212import Control.DeepSeq (NFData(..))
1313
1414--------------------------------------------------------------------------------
benchmark/Streamly/Benchmark/Unicode/Stream.hs+1−1
@@ -43,7 +43,7 @@ import Streamly.Benchmark.Common
4343import Streamly.Benchmark.Common.Handle
4444
4545#ifdef INSPECTION
46−import Streamly.Internal.Data.Serialize (Unbox)
46+import Streamly.Internal.Data.MutByteArray (Unbox)
4747import Streamly.Internal.Data.Stream (Step(..))
4848import qualified Streamly.Internal.Data.Fold as Fold
4949import 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 @@
11-- |
2−-- Module : Streamly.Data.Serialize
2+-- Module : Streamly.Data.MutByteArray
33-- Copyright : (c) 2023 Composewell Technologies
44-- License : BSD3-3-Clause
55-- Maintainer : streamly@composewell.com
@@ -42,7 +42,7 @@
4242-- to and from an 'Array' type. You can also serialize and deserialize directly
4343-- to and from a 'MutByteArray', using the type class methods.
4444--
45−module Streamly.Data.Serialize
45+module Streamly.Data.MutByteArray
4646 (
4747
4848 -- * Mutable Byte Array
@@ -82,4 +82,4 @@ module Streamly.Data.Serialize
8282-- Imports
8383--------------------------------------------------------------------------------
8484
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
121121import qualified Streamly.Internal.Data.Stream.Type as D
122122import qualified Streamly.Internal.Data.Stream.Generate as D
123123import 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
125125import qualified Streamly.Internal.Data.Unfold.Type as Unfold
126126import qualified Text.ParserCombinators.ReadPrec as ReadPrec
127127
core/src/Streamly/Internal/Data/IORef/Unboxed.hs+2−1
@@ -40,7 +40,8 @@ where
4040
4141import Control.Monad.IO.Class (MonadIO(..))
4242import Data.Proxy (Proxy(..))
43−import Streamly.Internal.Data.MutByteArray (MutByteArray(..), newByteArray)
43+import Streamly.Internal.Data.MutByteArray.Type
44+ (MutByteArray(..), newByteArray)
4445import Streamly.Internal.Data.Unbox (Unbox(..), sizeOf)
4546
4647import 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(..))
239239import Data.Word (Word8)
240240import Foreign.C.Types (CSize(..), CInt(..))
241241import Foreign.Ptr (plusPtr, minusPtr, nullPtr)
242−import Streamly.Internal.Data.MutByteArray
242+import Streamly.Internal.Data.MutByteArray.Type
243243 ( MutByteArray(..)
244244 , MutableByteArray
245245 , PinnedState(..)
@@ -266,7 +266,7 @@ import Streamly.Internal.Data.Unfold.Type (Unfold(..))
266266import Streamly.Internal.System.IO (arrayPayloadSize, defaultChunkSize)
267267
268268import 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
270270import qualified Streamly.Internal.Data.Producer as Producer
271271import qualified Streamly.Internal.Data.Stream.Type as D
272272import 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 #-}
25
36-- |
47-- Module : Streamly.Internal.Data.MutByteArray
58-- Copyright : (c) 2023 Composewell Technologies
69-- License : BSD3-3-Clause
710-- Maintainer : streamly@composewell.com
8−-- Stability : experimental
911-- Portability : GHC
1012--
13+
1114module Streamly.Internal.Data.MutByteArray
1215 (
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
16+ -- * MutByteArray
17+ module Streamly.Internal.Data.MutByteArray.Type
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
3525 ) where
3626
37−import Control.Monad.IO.Class (MonadIO(..))
38−#ifdef DEBUG
39−import Debug.Trace (trace)
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(..))
4040#endif
41−import GHC.Base (IO(..))
42−import System.IO.Unsafe (unsafePerformIO)
4341
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
4547
4648--------------------------------------------------------------------------------
47−-- The ArrayContents type
49+-- Common instances
4850--------------------------------------------------------------------------------
4951
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.
52+-- Note
53+-- ====
8554--
86−-- From the GHC manual:
55+-- Even a non-functional change such as changing the order of constructors will
56+-- change the instance derivation.
8757--
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.
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.
9564--
96−-- If the array is not pinned it is copied to pinned memory before passing it
97−-- to the monadic action.
65+-- Although such chages for the most basic types won't happen we need to detect
66+-- if it ever happens.
9867--
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.
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:
10377--
104−-- /Unsafe/ because of direct pointer operations. The user must ensure that
105−-- they are writing within the legal bounds of the array.
78+-- @
79+-- addLibrarySearchPath: C:\... (Win32 error 3): The system cannot find the path specified.
80+-- @
10681--
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.
10885--
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
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)
118158
119159--------------------------------------------------------------------------------
120−-- Creation
160+-- Integer
121161--------------------------------------------------------------------------------
122162
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 ())
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+
245240#endif
246− IO
247− $ \s# ->
248− case cloneMutableArrayWith# newByteArray# marr# s# of
249− (# s1#, marr1# #) -> (# s1#, MutByteArray marr1# #)
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/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)
2727import Language.Haskell.TH
2828import Streamly.Internal.Data.Serialize.Type (Serialize(..))
2929import Data.Foldable (foldlM)
30−import Streamly.Internal.Data.MutByteArray (MutByteArray)
30+import Streamly.Internal.Data.MutByteArray.Type (MutByteArray)
3131import Data.Proxy (Proxy(..))
3232
3333import 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)
2828import Data.List (foldl')
2929import Data.Proxy (Proxy (..))
3030import Streamly.Internal.Data.Unbox (Unbox)
31−import Streamly.Internal.Data.MutByteArray (MutByteArray(..), PinnedState(..))
31+import Streamly.Internal.Data.MutByteArray.Type
32+ (MutByteArray(..), PinnedState(..))
3233import Streamly.Internal.Data.Array.Type (Array(..))
3334import Streamly.Internal.System.IO (unsafeInlineIO)
3435import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
3536import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
3637import GHC.Stable (StablePtr(..))
3738
38−import qualified Streamly.Internal.Data.MutByteArray as MBA
39+import qualified Streamly.Internal.Data.MutByteArray.Type as MBA
3940import qualified Streamly.Internal.Data.Unbox as Unbox
4041import qualified Streamly.Internal.Data.Array as Array
4142import qualified Streamly.Internal.Data.MutArray as MutArray
@@ -95,17 +96,17 @@ import GHC.Exts
9596-- :}
9697--
9798-- @
98−-- import Streamly.Data.Serialize (deriveSerialize)
99+-- import Streamly.Data.MutByteArray (deriveSerialize)
99100-- \$(deriveSerialize [d|instance Serialize Object|])
100101-- @
101102--
102−-- See 'Streamly.Data.Serialize.deriveSerialize' and
103−-- 'Streamly.Data.Serialize.deriveSerializeWith' for more information on
103+-- See 'Streamly.Data.MutByteArray.deriveSerialize' and
104+-- 'Streamly.Data.MutByteArray.deriveSerializeWith' for more information on
104105-- deriving using Template Haskell.
105106--
106107-- Here is an example of a manual instance.
107108--
108−-- >>> import Streamly.Data.Serialize (Serialize(..))
109+-- >>> import Streamly.Data.MutByteArray (Serialize(..))
109110--
110111-- >>> :{
111112-- instance Serialize Object where
core/src/Streamly/Internal/Data/Unbox.hs+4−4
@@ -67,7 +67,7 @@ import GHC.Exts
6767import GHC.TypeLits
6868import Prelude hiding (read)
6969
70−import Streamly.Internal.Data.MutByteArray (MutByteArray(..))
70+import Streamly.Internal.Data.MutByteArray.Type (MutByteArray(..))
7171
7272--------------------------------------------------------------------------------
7373-- The Unbox type class
@@ -196,17 +196,17 @@ import Streamly.Internal.Data.MutByteArray (MutByteArray(..))
196196-- } deriving Generic
197197-- :}
198198--
199−-- >>> import Streamly.Data.Serialize (Unbox(..))
199+-- >>> import Streamly.Data.MutByteArray (Unbox(..))
200200-- >>> instance Unbox Object
201201--
202202-- To derive the instance via Template Haskell:
203203--
204204-- @
205−-- import Streamly.Data.Serialize (deriveUnbox)
205+-- import Streamly.Data.MutByteArray (deriveUnbox)
206206-- \$(deriveUnbox [d|instance Unbox Object|])
207207-- @
208208--
209−-- See 'Streamly.Data.Serialize.deriveUnbox' for more information on deriving
209+-- See 'Streamly.Data.MutByteArray.deriveUnbox' for more information on deriving
210210-- using Template Haskell.
211211--
212212-- If you want to write the instance manually:
core/streamly-core.cabal+5−4
@@ -292,7 +292,7 @@ library
292292 , Streamly.Internal.Data.Producer
293293
294294 -- streamly-core-array-types
295− , Streamly.Internal.Data.Serialize
295+ , Streamly.Internal.Data.MutByteArray
296296 , Streamly.Internal.Data.Serialize.Parser
297297 , Streamly.Internal.Data.Serialize.Stream
298298
@@ -367,7 +367,7 @@ library
367367 -- streamly-core released modules in alphabetic order
368368 -- NOTE: these must be added to streamly.cabal as well
369369 , Streamly.Console.Stdio
370− , Streamly.Data.Serialize
370+ , Streamly.Data.MutByteArray
371371 , Streamly.Data.Array
372372 , Streamly.Data.Array.Generic
373373 , Streamly.Data.MutArray
@@ -421,9 +421,10 @@ library
421421 , Streamly.Internal.Data.Array.Type
422422
423423 -- Unboxed IORef
424− , Streamly.Internal.Data.MutByteArray
425− , Streamly.Internal.Data.Unbox
426424 , Streamly.Internal.Data.IORef.Unboxed
425+
426+ , Streamly.Internal.Data.MutByteArray.Type
427+ , Streamly.Internal.Data.Unbox
427428 , Streamly.Internal.Data.Unbox.TH
428429 , Streamly.Internal.Data.Serialize.Type
429430 , 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
7979import Streamly.Internal.Data.Stream.Serial (SerialT)
8080import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
8181import Streamly.Internal.System.IO (defaultChunkSize)
82−import Streamly.Data.Serialize (Unbox)
82+import Streamly.Data.MutByteArray (Unbox)
8383
8484import qualified Streamly.Internal.Data.Array as A
8585import 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
164164import Streamly.Internal.Data.Stream.IsStream.Type
165165 (IsStream, fromStreamD, toStreamD)
166166import Streamly.Internal.Data.Stream.Serial (SerialT)
167−import Streamly.Data.Serialize (Unbox)
167+import Streamly.Data.MutByteArray (Unbox)
168168
169169import qualified Streamly.Internal.Data.Array as A
170170import 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
103103 (IsStream (..), fromSerial, consM, fromStreamD)
104104import Streamly.Internal.Data.Stream.Serial (SerialT, WSerialT)
105105import Streamly.Internal.Data.Time.Units (AbsTime , RelTime64, addToAbsTime64)
106−import Streamly.Data.Serialize (Unbox)
106+import Streamly.Data.MutByteArray (Unbox)
107107
108108import qualified Streamly.Internal.Data.MutArray as Unboxed
109109 (pollIntIORef, IORef)
src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+1−1
@@ -180,7 +180,7 @@ import Streamly.Internal.Data.Stream.Serial(toStreamK)
180180import Streamly.Internal.Data.Time.Units
181181 ( AbsTime, MilliSecond64(..), addToAbsTime, toRelTime
182182 , toAbsTime)
183−import Streamly.Data.Serialize (Unbox)
183+import Streamly.Data.MutByteArray (Unbox)
184184
185185import qualified Data.Heap as H
186186import 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)
111111import Streamly.Internal.Data.Fold ( Fold(..) )
112112import Streamly.Data.Stream (Stream)
113113import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
114−import Streamly.Data.Serialize (Unbox)
114+import Streamly.Data.MutByteArray (Unbox)
115115import Streamly.Data.Unfold (Unfold)
116116import Streamly.Internal.Network.Socket (SockSpec(..), accept, acceptor)
117117import Streamly.Internal.System.IO (defaultChunkSize)
src/Streamly/Internal/Network/Socket.hs+1−1
@@ -72,7 +72,7 @@ import Control.Monad.IO.Class (MonadIO(..))
7272import Data.Maybe (isNothing, fromJust)
7373import Data.Word (Word8)
7474import Foreign.Ptr (plusPtr, Ptr, castPtr)
75−import Streamly.Data.Serialize (Unbox)
75+import Streamly.Data.MutByteArray (Unbox)
7676import Network.Socket
7777 (Socket, SocketOption(..), Family(..), SockAddr(..),
7878 ProtocolNumber, withSocketsDo, SocketType(..), socket, bind,
src/Streamly/Network/Socket.hs+1−1
@@ -138,7 +138,7 @@ import Data.Word (Word8)
138138import Network.Socket (Socket, SockAddr)
139139import Streamly.Internal.Data.Unfold (Unfold(..))
140140import Streamly.Internal.Data.Array (Array(..))
141−import Streamly.Data.Serialize (Unbox)
141+import Streamly.Data.MutByteArray (Unbox)
142142
143143import Streamly.Internal.Network.Socket hiding (accept, read, readChunks)
144144import Prelude hiding (read)
test/Streamly/Test/Data/Array.hs+1−1
@@ -14,7 +14,7 @@ import Data.Proxy (Proxy(..))
1414import Data.Word(Word8)
1515import Foreign.Storable (peek)
1616import GHC.Ptr (plusPtr)
17−import Streamly.Internal.Data.Serialize (Unbox, sizeOf)
17+import Streamly.Internal.Data.MutByteArray (Unbox, sizeOf)
1818import Streamly.Internal.Data.MutArray (MutArray)
1919import Test.QuickCheck (chooseInt, listOf)
2020
test/Streamly/Test/Data/MutArray.hs+1−1
@@ -13,7 +13,7 @@ import GHC.Int (Int16(..), Int32(..), Int64(..), Int8(..))
1313import GHC.Real (Ratio(..))
1414import GHC.Stable (StablePtr(..))
1515import GHC.Word (Word16(..), Word32(..), Word64(..), Word8(..))
16−import Streamly.Internal.Data.Serialize (Unbox)
16+import Streamly.Internal.Data.MutByteArray (Unbox)
1717import Streamly.Test.Common (chooseInt)
1818import Test.Hspec (hspec, describe, it)
1919import Test.Hspec.QuickCheck
test/Streamly/Test/Data/Serialize.hs+3−3
@@ -22,15 +22,15 @@ module Streamly.Test.Data.Serialize (main) where
2222--------------------------------------------------------------------------------
2323
2424import System.Random (randomRIO)
25−import Streamly.Internal.Data.Serialize (MutByteArray, newByteArray)
25+import Streamly.Internal.Data.MutByteArray (MutByteArray, newByteArray)
2626import GHC.Generics (Generic)
27−import Streamly.Data.Serialize (Serialize)
27+import Streamly.Data.MutByteArray (Serialize)
2828import Streamly.Test.Data.Serialize.TH (genDatatype)
2929
3030import Data.Functor.Identity (Identity (..))
3131
3232import 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
3434
3535import qualified Streamly.Test.Data.Serialize.CompatV0 as CompatV0
3636import 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
1010--------------------------------------------------------------------------------
1111
1212import Test.QuickCheck (Arbitrary, arbitrary, elements)
13−import Streamly.Internal.Data.Serialize (Serialize)
14−import qualified Streamly.Internal.Data.Serialize as Serialize
13+import Streamly.Internal.Data.MutByteArray (Serialize)
14+import qualified Streamly.Internal.Data.MutByteArray as Serialize
1515
1616--------------------------------------------------------------------------------
1717-- Types
test/Streamly/Test/Data/Serialize/CompatV1.hs+2−2
@@ -10,8 +10,8 @@ module Streamly.Test.Data.Serialize.CompatV1
1010--------------------------------------------------------------------------------
1111
1212import Test.QuickCheck (Arbitrary, arbitrary, elements)
13−import Streamly.Internal.Data.Serialize (Serialize)
14−import qualified Streamly.Internal.Data.Serialize as Serialize
13+import Streamly.Internal.Data.MutByteArray (Serialize)
14+import qualified Streamly.Internal.Data.MutByteArray as Serialize
1515
1616--------------------------------------------------------------------------------
1717-- Types
test/Streamly/Test/Data/Unbox.hs+1−1
@@ -27,7 +27,7 @@ import Data.Proxy (Proxy(..))
2727import GHC.Generics (Generic, Rep(..))
2828import GHC.Real (Ratio(..))
2929
30−import Streamly.Internal.Data.Serialize
30+import Streamly.Internal.Data.MutByteArray
3131
3232import Test.Hspec as H
3333
test/Streamly/Test/Data/Unbox/TH.hs+1−1
@@ -15,7 +15,7 @@ module Streamly.Test.Data.Unbox.TH (main) where
1515--------------------------------------------------------------------------------
1616
1717import Data.Proxy (Proxy(..))
18−import Streamly.Internal.Data.Serialize
18+import Streamly.Internal.Data.MutByteArray
1919
2020import Test.Hspec as H
2121
test/Streamly/Test/Prelude/Serial.hs+1−1
@@ -26,7 +26,7 @@ import Data.Maybe ( isJust, fromJust )
2626import Data.Word (Word8)
2727import Data.Semigroup (Sum(..), getSum)
2828import Foreign.Storable (Storable)
29−import Streamly.Internal.Data.Serialize (Unbox)
29+import Streamly.Internal.Data.MutByteArray (Unbox)
3030import Test.Hspec.QuickCheck
3131import Test.QuickCheck
3232 ( Gen