{-# LANGUAGE TemplateHaskell #-} module Main where import Criterion.Main import Data.Text.Glyphize import FileEmbedLzma (embedLazyText, embedByteString) import Data.FileEmbed (makeRelativeToProject) import System.FilePath (()) import qualified Data.Text.Foreign as Txt import qualified Data.Text.Lazy as Txt import Control.Parallel.Strategies (parMap, rdeepseq) import Data.Word (Word8) -- Benchmarking these as well... import Foreign.Marshal.Array (peekArray, copyArray) import Foreign.ForeignPtr (mallocForeignPtrArray, ForeignPtr) import Foreign.Ptr (Ptr) import Foreign.Storable (Storable(..)) import System.IO.Unsafe (unsafePerformIO) shapeStr txt = shape font defaultBuffer { text = txt } [] where font = createFont $ createFace $( makeRelativeToProject ("assets" "Lora-Regular.ttf") >>= embedByteString) 0 dracula = $(makeRelativeToProject ("bench" "dracula.txt") >>= embedLazyText) main = defaultMain [ bgroup "Dracula" [ bench "Week-Head" $ whnf shapeStr dracula, bench "Normal Form" $ nf shapeStr dracula, bench "Paragraphs" $ nf (map shapeStr) $ Txt.lines dracula, bench "Parallelised" $ nf (parMap rdeepseq shapeStr) $ Txt.lines dracula ], bgroup "building blocks" [ bench "peekArray (NF)" $ nfIO $ Txt.useAsPtr (Txt.toStrict dracula) $ \ptr l -> peekArray (fromEnum l) ptr, bench "peekArray" $ whnfIO $ Txt.useAsPtr (Txt.toStrict dracula) $ \ptr l -> peekArray (fromEnum l) ptr, bench "alloc foreign ptr" $ whnfIO (mallocForeignPtrArray $ fromEnum $ Txt.length dracula :: IO (ForeignPtr Word8)) ] ]