on master help sign in
Refactor getIndexUnsafe, putIndexUnsafe

Variable renaming, add an assert.

commit3d5cd85c6cace187f08e03ec336382430e6d036f authorHarendra Kumar <harendra@composewell.com> dateSun 10 Oct 2021, 20:41 (5 years ago) parent3b82705
1 file changed+14−9
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+14−9
src/Streamly/Internal/Data/Array/Foreign/Mut/Type.hs+14−9
@@ -317,13 +317,19 @@ unsafeWithNewArray count f = do
317317-- Mutation
318318-------------------------------------------------------------------------------
319319
320+-- | Write the given element to the given index of the array. Does not check if
321+-- the index is out of bounds of the array.
322+--
323+-- /Pre-release/
320324{-# INLINE putIndexUnsafe #-}
321325putIndexUnsafe :: forall m a. (MonadIO m, Storable a)
322326 => Array a -> Int -> a -> m ()
323−putIndexUnsafe Array {..} i x =
324− unsafeWithForeignPtrM aStart
325− $ \begin -> do
326− liftIO $ poke (begin `plusPtr` (i * sizeOf (undefined :: a))) x
327+putIndexUnsafe Array{..} i x =
328+ unsafeWithForeignPtrM aStart $ \ptr -> do
329+ let elemSize = sizeOf (undefined :: a)
330+ elemPtr = ptr `plusPtr` (elemSize * i)
331+ assert (i >= 0 && elemPtr `plusPtr` elemSize <= aEnd) (return ())
332+ liftIO $ poke elemPtr x
327333
328334-- Internal routine for when the array is being created. Appends one item at
329335-- the end of the array. Useful when sequentially writing a stream to the
@@ -421,12 +427,11 @@ shrinkToFit arr@Array{..} = do
421427{-# INLINE_NORMAL getIndexUnsafe #-}
422428getIndexUnsafe :: forall m a. (MonadIO m, Storable a) => Array a -> Int -> m a
423429getIndexUnsafe Array {..} i =
424− unsafeWithForeignPtrM aStart $ \p -> do
430+ unsafeWithForeignPtrM aStart $ \ptr -> do
425431 let elemSize = sizeOf (undefined :: a)
426− elemOff = p `plusPtr` (elemSize * i)
427− assert (i >= 0 && elemOff `plusPtr` elemSize <= aEnd)
428− (return ())
429− liftIO $ peek elemOff
432+ elemPtr = ptr `plusPtr` (elemSize * i)
433+ assert (i >= 0 && elemPtr `plusPtr` elemSize <= aEnd) (return ())
434+ liftIO $ peek elemPtr
430435
431436-------------------------------------------------------------------------------
432437-- Size