Skip to content

Commit

Permalink
Implement MutArray.Generic.unsafeSnoc similar 2 MutArray.unsafeSnoc
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Nov 29, 2024
1 parent 3fb5d02 commit d28a6de
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions core/src/Streamly/Internal/Data/MutArray/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ reallocWith label sizer reqSize arr = do
-- Snoc
-------------------------------------------------------------------------------

{-# INLINE snocNewEnd #-}
snocNewEnd :: MonadIO m => Int -> MutArray a -> a -> m (MutArray a)
snocNewEnd newEnd arr@MutArray{..} x = liftIO $ do
assert (newEnd <= arrBound) (return ())
putIndexUnsafeWith arrEnd arrContents# x
return $ arr {arrEnd = newEnd}

-- XXX Not sure of the behavior of writeArray# if we specify an index which is
-- out of bounds. This comment should be rewritten based on that.
-- | Really really unsafe, appends the element into the first array, may
Expand All @@ -418,12 +425,8 @@ reallocWith label sizer reqSize arr = do
--
-- /Internal/
{-# INLINE unsafeSnoc #-}
unsafeSnoc, snocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)
unsafeSnoc arr@MutArray {..} a = do
assert (arrEnd < arrBound) (return ())
let arr1 = arr {arrEnd = arrEnd + 1}
unsafePutIndex (length arr) arr1 a
return arr1
snocUnsafe, unsafeSnoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
unsafeSnoc arr = snocNewEnd (arrEnd arr + 1) arr

-- NOINLINE to move it out of the way and not pollute the instruction cache.
{-# NOINLINE snocWithRealloc #-}
Expand Down

0 comments on commit d28a6de

Please sign in to comment.