From 08880997711d8785ebf901bec51a3995b8e215a6 Mon Sep 17 00:00:00 2001 From: Bryan Richter Date: Sat, 30 Sep 2023 22:15:49 +0300 Subject: [PATCH] Use unsafeWrite for speed --- src/Data/Text/Internal/Reverse.hs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Data/Text/Internal/Reverse.hs b/src/Data/Text/Internal/Reverse.hs index 884b31b2..8b40ccea 100644 --- a/src/Data/Text/Internal/Reverse.hs +++ b/src/Data/Text/Internal/Reverse.hs @@ -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 @@ -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