Commits

tibbe  committed 7e61752

Added benchmark for builder.

The benchmark shows that using the locally defined inlineInterleaveST
is 13-44% faster than using unsafeInterleaveST.

  • Participants
  • Parent commits ae8dba9

Comments (0)

Files changed (2)

File Data/Text/Builder.hs

     assert (sidx + count <= A.length src) .
     assert (didx + count <= A.length dest) $
     copy_loop sidx didx 0
-    where
-      copy_loop !i !j !c
-          | c >= count  = return ()
-          | otherwise = do A.unsafeWrite dest j (A.unsafeIndex src i)
-                           copy_loop (i+1) (j+1) (c+1)
+  where
+    copy_loop !i !j !c
+        | c >= count  = return ()
+        | otherwise = do A.unsafeWrite dest j (A.unsafeIndex src i)
+                         copy_loop (i+1) (j+1) (c+1)
 {-# INLINE unsafeCopy #-}

File tests/Benchmarks.hs

-{-# LANGUAGE GADTs, MagicHash #-}
+{-# LANGUAGE BangPatterns, GADTs, MagicHash #-}
 
 import qualified Data.ByteString.Char8 as BS
 import qualified Data.ByteString.Lazy.Char8 as BL
 import Control.Exception (evaluate)
 import Criterion.Main
 import Data.Char
+import Data.Monoid (mappend, mempty)
 import qualified Codec.Binary.UTF8.Generic as UTF8
 import qualified Data.Text as TS
+import qualified Data.Text.Builder as TB
 import qualified Data.Text.IO as TS
 import qualified Data.Text.Lazy as TL
 import qualified Data.Text.Lazy.IO as TL
         , bench "bl" $ nf (L.length . BL.zipWith min blb) bla
         , bench "l" $ nf (L.length . L.zipWith min lb) la
         ]
+      ],
+      bgroup "builder" [
+        bench "mappend char" $ nf (TL.length . TB.toLazyText . mappendNChar 'a') 10000,
+        bench "mappend text" $ nf (TL.length . TB.toLazyText . mappendNText short) 10000
       ]
     ]
   where
     f (C# c#) = C# (chr# (ord# c# +# 1#))
     len l _ = l + (1::Int)
     replicat n = concat . L.replicate n
+    short = TS.pack "short"
 
 chunksOf :: Int -> BS.ByteString -> [BS.ByteString]
 chunksOf k = go
     go t = case BS.splitAt k t of
              (a,b) | BS.null a -> []
                    | otherwise -> a : go b
+
+mappendNChar :: Char -> Int -> TB.Builder
+mappendNChar c n = go 0 mempty
+  where
+    go i !acc
+      | i < n     = go (i+1) (acc `mappend` TB.singleton c)
+      | otherwise = acc
+
+mappendNText :: TS.Text -> Int -> TB.Builder
+mappendNText t n = go 0 mempty
+  where
+    go i !acc
+      | i < n     = go (i+1) (acc `mappend` TB.fromText t)
+      | otherwise = acc