From f990b47220dbfbd7602aec09cbe5dfdb9b73c27a Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Wed, 24 May 2023 14:11:35 +0530 Subject: [PATCH] fix review comments --- core/src/Streamly/Data/MutArray/Generic.hs | 1 - .../Streamly/Internal/Data/Array/Generic.hs | 10 ++----- .../Internal/Data/Array/Generic/Mut/Type.hs | 17 +---------- core/src/Streamly/Internal/FileSystem/File.hs | 28 +++++++++---------- 4 files changed, 18 insertions(+), 38 deletions(-) diff --git a/core/src/Streamly/Data/MutArray/Generic.hs b/core/src/Streamly/Data/MutArray/Generic.hs index dcad8fc6eb..3821e87a83 100644 --- a/core/src/Streamly/Data/MutArray/Generic.hs +++ b/core/src/Streamly/Data/MutArray/Generic.hs @@ -37,7 +37,6 @@ module Streamly.Data.MutArray.Generic -- * Streams , read - , readRev -- * Random reads , getIndex diff --git a/core/src/Streamly/Internal/Data/Array/Generic.hs b/core/src/Streamly/Internal/Data/Array/Generic.hs index 8646772678..697aea585d 100644 --- a/core/src/Streamly/Internal/Data/Array/Generic.hs +++ b/core/src/Streamly/Internal/Data/Array/Generic.hs @@ -209,15 +209,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) diff --git a/core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs b/core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs index 14351a759c..591d15e586 100644 --- a/core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs +++ b/core/src/Streamly/Internal/Data/Array/Generic/Mut/Type.hs @@ -28,10 +28,6 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type , writeWith , write , fromStreamN - , fromStream - - , fromListN - , fromList -- , writeRevN -- , writeRev @@ -97,7 +93,6 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type -- ** To containers , read - , readRev , toStreamK -- , toStreamKRev , toList @@ -533,11 +528,8 @@ 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@ -- --- We can try this if the unfold has any performance issues. +-- /Pre-release/ {-# INLINE_NORMAL read #-} read :: MonadIO m => MutArray a -> D.Stream m a read arr@MutArray{..} = @@ -556,13 +548,6 @@ toStreamK arr@MutArray{..} = K.unfoldrM step 0 x <- getIndexUnsafe i arr return $ Just (x, i + 1) -{-# INLINE_NORMAL readRev #-} -readRev :: MonadIO m => MutArray a -> D.Stream m a -readRev arr@MutArray{..} = - D.mapM (`getIndexUnsafe` arr) - $ D.enumerateFromThenToIntegral (arrLen - 1) (arrLen - 2) 0 - - ------------------------------------------------------------------------------- -- Folds ------------------------------------------------------------------------------- diff --git a/core/src/Streamly/Internal/FileSystem/File.hs b/core/src/Streamly/Internal/FileSystem/File.hs index 50550aa3f8..dc980c3edd 100644 --- a/core/src/Streamly/Internal/FileSystem/File.hs +++ b/core/src/Streamly/Internal/FileSystem/File.hs @@ -71,9 +71,9 @@ module Streamly.Internal.FileSystem.File , writeChunks -- ** Writing Streams - , putBytes - , putBytesWith - , putChunks + , fromBytes -- putBytes? + , fromBytesWith + , fromChunks -- ** Append To File , append @@ -383,10 +383,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. -- @@ -402,16 +402,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 -- @@ -421,9 +421,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 #-}