Skip to content

Commit

Permalink
Add benchmark vs blaze-html
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorycollins committed May 26, 2012
1 parent 17bbd9c commit 9178514
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Text/XmlHtml/HTML/Render.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -233,10 +233,16 @@ ambiguousAmpersand _ = False
-- | Function for rendering HTML nodes without the overhead of creating a -- | Function for rendering HTML nodes without the overhead of creating a
-- Document structure. -- Document structure.
renderHtmlFragment :: Encoding -> [Node] -> Builder renderHtmlFragment :: Encoding -> [Node] -> Builder
renderHtmlFragment _ [] = mempty renderHtmlFragment e =
renderHtmlFragment e (n:ns) = case e of
firstNode e n `mappend` (mconcat $ map (node e) ns) UTF8 -> goUtf8
_ -> goUtf16


where
goUtf8 = foldr (mappend . utf8Node) mempty

goUtf16 = foldr (mappend . utf16Node e) mempty
{-# INLINE renderHtmlFragment #-}


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
-- | HTML allows & so long as it is not "ambiguous" (i.e., looks like an -- | HTML allows & so long as it is not "ambiguous" (i.e., looks like an
Expand Down
14 changes: 14 additions & 0 deletions test/benchmark/Benchmark.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import Criterion.Main
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
import Blaze.ByteString.Builder import Blaze.ByteString.Builder
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.List
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import Text.XmlHtml import Text.XmlHtml
import Text.Blaze.Html.Renderer.Utf8
import Text.Blaze.Html (Html)
------------------------------------------------------------------------------
import BlazeExample


------------------------------------------------------------------------------ ------------------------------------------------------------------------------
main :: IO () main :: IO ()
Expand All @@ -21,6 +27,7 @@ main = do


defaultMain [ defaultMain [
bench "renderHtml" $ renderHtmlBenchmark exampleHTML bench "renderHtml" $ renderHtmlBenchmark exampleHTML
, bench "renderBlaze" $ renderBlazeBenchmark blazeHtmlExample
] ]




Expand All @@ -34,3 +41,10 @@ parseExample = do
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
renderHtmlBenchmark :: Document -> Pure renderHtmlBenchmark :: Document -> Pure
renderHtmlBenchmark = whnf (toByteString . render) renderHtmlBenchmark = whnf (toByteString . render)


------------------------------------------------------------------------------
renderBlazeBenchmark :: Html -> Pure
renderBlazeBenchmark = whnf (touch . renderHtml)
where
touch l = foldl' seq "" (L.toChunks l) `seq` ()
Loading

0 comments on commit 9178514

Please sign in to comment.