Skip to content

Commit d28a6de

Browse files
committed
Implement MutArray.Generic.unsafeSnoc similar 2 MutArray.unsafeSnoc
1 parent 3fb5d02 commit d28a6de

File tree

1 file changed

+9
-6
lines changed
  • core/src/Streamly/Internal/Data/MutArray

1 file changed

+9
-6
lines changed

core/src/Streamly/Internal/Data/MutArray/Generic.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,13 @@ reallocWith label sizer reqSize arr = do
410410
-- Snoc
411411
-------------------------------------------------------------------------------
412412

413+
{-# INLINE snocNewEnd #-}
414+
snocNewEnd :: MonadIO m => Int -> MutArray a -> a -> m (MutArray a)
415+
snocNewEnd newEnd arr@MutArray{..} x = liftIO $ do
416+
assert (newEnd <= arrBound) (return ())
417+
putIndexUnsafeWith arrEnd arrContents# x
418+
return $ arr {arrEnd = newEnd}
419+
413420
-- XXX Not sure of the behavior of writeArray# if we specify an index which is
414421
-- out of bounds. This comment should be rewritten based on that.
415422
-- | Really really unsafe, appends the element into the first array, may
@@ -418,12 +425,8 @@ reallocWith label sizer reqSize arr = do
418425
--
419426
-- /Internal/
420427
{-# INLINE unsafeSnoc #-}
421-
unsafeSnoc, snocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)
422-
unsafeSnoc arr@MutArray {..} a = do
423-
assert (arrEnd < arrBound) (return ())
424-
let arr1 = arr {arrEnd = arrEnd + 1}
425-
unsafePutIndex (length arr) arr1 a
426-
return arr1
428+
snocUnsafe, unsafeSnoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
429+
unsafeSnoc arr = snocNewEnd (arrEnd arr + 1) arr
427430

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

0 commit comments

Comments
 (0)