Commit d057bd74 authored by Bodigrim's avatar Bodigrim
Browse files

Use a primitive to resize MutableByteArray

parent 2e632506
...@@ -27,6 +27,7 @@ module Data.Text.Array ...@@ -27,6 +27,7 @@ module Data.Text.Array
Array(..) Array(..)
, MArray(..) , MArray(..)
-- * Functions -- * Functions
, resizeM
, copyM , copyM
, copyI , copyI
, empty , empty
...@@ -41,6 +42,7 @@ module Data.Text.Array ...@@ -41,6 +42,7 @@ module Data.Text.Array
) where ) where
#if defined(ASSERTS) #if defined(ASSERTS)
-- TODO employ resizeMutableByteArray# instead of cropping Text
import Control.Exception (assert) import Control.Exception (assert)
import GHC.Stack (HasCallStack) import GHC.Stack (HasCallStack)
#endif #endif
...@@ -159,6 +161,12 @@ run2 k = runST (do ...@@ -159,6 +161,12 @@ run2 k = runST (do
return (arr,b)) return (arr,b))
{-# INLINE run2 #-} {-# INLINE run2 #-}
resizeM :: MArray s -> Int -> ST s (MArray s)
resizeM (MutableByteArray ma) i@(I# i#) = ST $ \s1# ->
case resizeMutableByteArray# ma i# s1# of
(# s2#, newArr #) -> (# s2#, MutableByteArray newArr #)
{-# INLINE resizeM #-}
-- | Copy some elements of a mutable array. -- | Copy some elements of a mutable array.
copyM :: MArray s -- ^ Destination copyM :: MArray s -- ^ Destination
-> Int -- ^ Destination offset -> Int -- ^ Destination offset
......
...@@ -157,8 +157,7 @@ unstream (Stream next0 s0 len) = runText $ \done -> do ...@@ -157,8 +157,7 @@ unstream (Stream next0 s0 len) = runText $ \done -> do
{-# NOINLINE realloc #-} {-# NOINLINE realloc #-}
realloc !si !di = do realloc !si !di = do
let newlen = (maxi + 1) * 2 let newlen = (maxi + 1) * 2
arr' <- A.new newlen arr' <- A.resizeM arr newlen
A.copyM arr' 0 arr 0 di
outer arr' (newlen - 1) si di outer arr' (newlen - 1) si di
outer arr0 (mlen - 1) s0 0 outer arr0 (mlen - 1) s0 0
...@@ -299,8 +298,7 @@ mapAccumL f z0 (Stream next0 s0 len) = (nz, I.text na 0 nl) ...@@ -299,8 +298,7 @@ mapAccumL f z0 (Stream next0 s0 len) = (nz, I.text na 0 nl)
Yield x s' Yield x s'
| j >= top -> {-# SCC "mapAccumL/resize" #-} do | j >= top -> {-# SCC "mapAccumL/resize" #-} do
let top' = (top + 1) `shiftL` 1 let top' = (top + 1) `shiftL` 1
arr' <- A.new top' arr' <- A.resizeM arr top'
A.copyM arr' 0 arr 0 top
outer arr' top' z s i outer arr' top' z s i
| otherwise -> do d <- unsafeWrite arr i c | otherwise -> do d <- unsafeWrite arr i c
loop z' s' (i+d) loop z' s' (i+d)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment