Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improve table benchmark

  • Loading branch information...
commit 636437007483fe220314b623b230576420e1a818 1 parent 1a3b0d8
Gregory Collins gregorycollins authored
Showing with 40 additions and 22 deletions.
  1. +40 −22 snap-bench/src/table.hs
62 snap-bench/src/table.hs
View
@@ -1,40 +1,58 @@
+{-# LANGUAGE BangPatterns #-}
+
module Main where
import System
+import Blaze.ByteString.Builder
+import Blaze.ByteString.Builder.Char8
import Control.Applicative
import Control.Monad.Trans
import qualified Data.ByteString.Char8 as BS
+import Data.Monoid
import Snap.Http.Server
import Snap.Iteratee
import Snap.Types
import Snap.Util.FileServe
-tableRow :: Int -> Snap ()
-tableRow x
- | x <= 50 = do
- writeBS "<td>"
- writeBS (BS.pack (show x))
- writeBS "</td>"
- tableRow (x+1)
- | otherwise = return ()
-
-tableBody :: Int -> Snap ()
-tableBody x
- | x <= 1000 = do
- writeBS "<tr><td>"
- writeBS (BS.pack (show x))
- writeBS "</td>"
- tableRow 1
- writeBS "</tr>\n"
- tableBody (x+1)
- | otherwise = return ()
+tdOp, tdCl, trOp, trCl :: Builder
+tdOp = fromByteString "<td>"
+tdCl = fromByteString "</td>"
+trOp = fromByteString "<tr>"
+trCl = fromByteString "</tr>\n"
+
+
+tableRow :: Int -> Builder
+tableRow = go mempty
+ where
+ col !x = mconcat [ tdOp, fromShow x, tdCl ]
+
+ go !bldr !x | x <= 50 = let b' = bldr `mappend` col x
+ in go b' (x+1)
+ | otherwise = bldr
+
+
+tableBody :: Int -> Builder
+tableBody = go mempty
+ where
+ row !x = mconcat [ trOp
+ , tdOp
+ , fromShow x
+ , tdCl
+ , tableRow 1
+ , trCl ]
+
+ go !bldr !x | x <= 1000 = let b' = bldr `mappend` row x
+ in go b' (x+1)
+ | otherwise = bldr
+
tableServer :: Snap ()
tableServer = do
- writeBS "<html><body><table>\n"
- tableBody 1
- writeBS "</table></body></html>"
+ writeBuilder $ mconcat [ fromByteString "<html><body><table>\n"
+ , tableBody 1
+ , fromByteString "</table></body></html>" ]
+
main :: IO ()
main = do
Please sign in to comment.
Something went wrong with that request. Please try again.