Commits

Bryan O'Sullivan committed 591c645

Reduce pointer arithmetic for better speed.

Comments (0)

Files changed (1)

Data/Text/Encoding.hs

 import Foreign.C.Types (CSize)
 import Foreign.ForeignPtr (withForeignPtr)
 import Foreign.Marshal.Utils (with)
-import Foreign.Ptr (Ptr, plusPtr)
+import Foreign.Ptr (Ptr, minusPtr, plusPtr)
 import Foreign.Storable (peek, poke)
 import GHC.Base (MutableByteArray#)
 import System.IO.Unsafe (unsafePerformIO)
                   -- A single ASCII octet is likely to start a run of
                   -- them.  We see better performance when we
                   -- special-case this assumption.
-                  let ascii !t !u
-                        | t == offLen || u == size || v >= 0x80 = go t u
+                  let end = ptr `plusPtr` size
+                      ascii !t !u
+                        | t == offLen || u == end || v >= 0x80 = go t (u `minusPtr` ptr)
                         | otherwise = do
-                            poke (ptr `plusPtr` u) (fromIntegral v :: Word8)
-                            ascii (t+1) (u+1)
+                            poke u (fromIntegral v :: Word8)
+                            ascii (t+1) (u `plusPtr` 1)
                         where v = A.unsafeIndex arr t
-                  ascii (n+1) (m+1)
+                  ascii (n+1) (ptr `plusPtr` (m+1))
               | w <= 0x7FF -> ensure 2 $ do
                   poke8 m     $ (w `shiftR` 6) + 0xC0
                   poke8 (m+1) $ (w .&. 0x3f) + 0x80