Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan committed Jul 6, 2023
1 parent 860bde0 commit faad01d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.
1 change: 0 additions & 1 deletion core/src/Streamly/Data/MutArray/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module Streamly.Data.MutArray.Generic

-- * Streams
, read
, readRev

-- * Random reads
, getIndex
Expand Down
10 changes: 3 additions & 7 deletions core/src/Streamly/Internal/Data/Array/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,11 @@ getIndexUnsafe :: Int -> Array a -> a
getIndexUnsafe i arr =
unsafePerformIO $ MArray.getIndexUnsafe i (unsafeThaw arr)

invalidIndex :: String -> Int -> a
invalidIndex label i =
error $ label ++ ": invalid array index " ++ show i

getIndex :: Int -> Array a -> a
getIndex :: Int -> Array a -> Maybe a
getIndex i arr@Array {..} =
if i >= 0 && i < arrLen
then getIndexUnsafe i arr
else invalidIndex "getIndex" i
then Just $ getIndexUnsafe i arr
else Nothing

{-# INLINE writeLastN #-}
writeLastN :: MonadIO m => Int -> Fold m a (Array a)
Expand Down
26 changes: 4 additions & 22 deletions core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
, writeWith
, write
, fromStreamN
, fromStream

, fromListN
, fromList

-- , writeRevN
-- , writeRev
Expand Down Expand Up @@ -179,7 +175,7 @@ import GHC.Base
, readArray#
, writeArray#
)
import GHC.IO (IO(..), unsafePerformIO)
import GHC.IO (IO(..))
import GHC.Int (Int(..))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Producer.Type (Producer (..))
Expand Down Expand Up @@ -533,11 +529,10 @@ getSlice index len arr@MutArray{..} =
toList :: MonadIO m => MutArray a -> m [a]
toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]

-- | Use the 'read' unfold instead.
--
-- @read = D.unfold read@
-- | Generates a stream from the elements of @MutArray@
--
-- We can try this if the unfold has any performance issues.
-- @read = D.unfold reader@
-- /Pre-release/
{-# INLINE_NORMAL read #-}
read :: MonadIO m => MutArray a -> D.Stream m a
read arr@MutArray{..} =
Expand All @@ -562,7 +557,6 @@ readRev arr@MutArray{..} =
D.mapM (`getIndexUnsafe` arr)
$ D.enumerateFromThenToIntegral (arrLen - 1) (arrLen - 2) 0


-------------------------------------------------------------------------------
-- Folds
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -802,18 +796,6 @@ strip p arr = liftIO $ do
then getIndexL (idx + 1)
else return idx

{-# INLINE fromStream #-}
fromStream :: MonadIO m => Stream m a -> m (MutArray a)
fromStream = D.fold write

{-# INLINABLE fromList #-}
fromList :: [a] -> MutArray a
fromList xs = unsafePerformIO $ fromStream $ D.fromList xs

{-# INLINE fromStreamN #-}
fromStreamN :: MonadIO m => Int -> Stream m a -> m (MutArray a)
fromStreamN n = D.fold (writeN n)

{-# INLINABLE fromListN #-}
fromListN :: Int -> [a] -> MutArray a
fromListN n xs = unsafePerformIO $ fromStreamN n $ D.fromList xs
28 changes: 14 additions & 14 deletions core/src/Streamly/Internal/FileSystem/File.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ module Streamly.Internal.FileSystem.File
, writeChunks

-- ** Writing Streams
, putBytes
, putBytesWith
, putChunks
, fromBytes -- putBytes?
, fromBytesWith
, fromChunks

-- ** Append To File
, append
Expand Down Expand Up @@ -382,10 +382,10 @@ fromChunksMode mode file xs = S.fold drain $
--
-- /Pre-release/
--
{-# INLINE putChunks #-}
putChunks :: (MonadIO m, MonadCatch m)
{-# INLINE fromChunks #-}
fromChunks :: (MonadIO m, MonadCatch m)
=> FilePath -> Stream m (Array a) -> m ()
putChunks = fromChunksMode WriteMode
fromChunks = fromChunksMode WriteMode

-- GHC buffer size dEFAULT_FD_BUFFER_SIZE=8192 bytes.
--
Expand All @@ -401,16 +401,16 @@ putChunks = fromChunksMode WriteMode
--
-- /Pre-release/
--
{-# INLINE putBytesWith #-}
putBytesWith :: (MonadIO m, MonadCatch m)
{-# INLINE fromBytesWith #-}
fromBytesWith :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
putBytesWith n file xs = putChunks file $ S.chunksOf n xs
fromBytesWith n file xs = fromChunks file $ S.chunksOf n xs

{-# DEPRECATED fromBytesWithBufferOf "Please use 'putBytesWith' instead" #-}
{-# DEPRECATED fromBytesWithBufferOf "Please use 'fromBytesWith' instead" #-}
{-# INLINE fromBytesWithBufferOf #-}
fromBytesWithBufferOf :: (MonadIO m, MonadCatch m)
=> Int -> FilePath -> Stream m Word8 -> m ()
fromBytesWithBufferOf = putBytesWith
fromBytesWithBufferOf = fromBytesWith

-- > write = 'writeWith' defaultChunkSize
--
Expand All @@ -420,9 +420,9 @@ fromBytesWithBufferOf = putBytesWith
-- created. File is locked using single writer locking mode.
--
-- /Pre-release/
{-# INLINE putBytes #-}
putBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
putBytes = putBytesWith defaultChunkSize
{-# INLINE fromBytes #-}
fromBytes :: (MonadIO m, MonadCatch m) => FilePath -> Stream m Word8 -> m ()
fromBytes = fromBytesWith defaultChunkSize

{-
{-# INLINE write #-}
Expand Down

0 comments on commit faad01d

Please sign in to comment.