Skip to content

Commit

Permalink
Use unsafeWrite for speed
Browse files Browse the repository at this point in the history
  • Loading branch information
chreekat committed Sep 30, 2023
1 parent 11280ba commit 0888099
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Data/Text/Internal/Reverse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import GHC.Exts as Exts
import Control.Monad.ST.Unsafe (unsafeIOToST)
import Foreign.C.Types (CSize(..))
#else
import Control.Monad (when)
import Control.Monad.ST (ST)
import Data.Text.Internal.Encoding.Utf8 (utf8LengthByLeader)
#endif
Expand Down Expand Up @@ -49,8 +50,18 @@ reversePoints
reversePoints _ _ _ pOut | pOut < 0 = pure ()
reversePoints src pIn dest pOut =
let pointLength = utf8LengthByLeader (A.unsafeIndex src pIn)
pOut' = pOut - pointLength + 1
-- Repeated unsafeWrite is faster than copyI
copyByte i = A.unsafeWrite dest (pOut' + i) (A.unsafeIndex src (pIn + i))
in do
A.copyI pointLength dest (pOut - pointLength + 1) src pIn
-- Unrolled for speed
copyByte 0
when (pointLength > 1) $ do
copyByte 1
when (pointLength > 2) $ do
copyByte 2
when (pointLength > 3) $
copyByte 3
reversePoints src (pIn + pointLength) dest (pOut - pointLength)
#else
reverse (Text (A.ByteArray ba) off len) = runST $ do
Expand Down

0 comments on commit 0888099

Please sign in to comment.