Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions http/attoparsec/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.stack-work
dist-newstyle
tags
57 changes: 34 additions & 23 deletions http/attoparsec/src/Main.hs
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
{-# LANGUAGE OverloadedStrings #-}

import Criterion.Main (bench, bgroup, defaultMain, env, whnf)
import Criterion.Main (bench, defaultMain, env, whnf)
import qualified Data.ByteString as B

import Control.Applicative
import Data.Attoparsec.ByteString as P
import Data.Attoparsec.ByteString.Char8 (char8, endOfLine, isDigit_w8)
import Data.Attoparsec.ByteString.Char8 (isEndOfLine, isHorizontalSpace)
import Data.Attoparsec.ByteString.Char8 (char8, endOfLine, isDigit_w8, isEndOfLine, isHorizontalSpace)
import Data.ByteString (ByteString)
import Data.Word (Word8)

isToken :: Word8 -> Bool
isToken w = w <= 127 && notInClass "\0-\31()<>@,;:\\\"/[]?={} \t" w
isToken w =
w <= 127
&& w > 31 -- "\0-\31"
&& ( w /= 40 -- "("
&& w /= 41 -- ")"
&& w /= 60 -- "<"
&& w /= 62 -- ">"
&& w /= 64 -- "@"
&& w /= 44 -- ","
&& w /= 59 -- ";"
&& w /= 58 -- ":"
&& w /= 92 -- "\\"
&& w /= 34 -- "\""
&& w /= 91 -- "["
&& w /= 93 -- "]"
&& w /= 63 -- "?"
&& w /= 61 -- "="
&& w /= 123 -- "{"
&& w /= 125 -- "}"
&& w /= 32 -- " "
&& w /= 9 -- "\t")
)

skipSpaces :: Parser ()
skipSpaces = satisfy isHorizontalSpace *> skipWhile isHorizontalSpace

data Request = Request {
requestMethod :: ByteString
, requestUri :: ByteString
, requestVersion :: ByteString
requestMethod :: !ByteString
, requestUri :: !ByteString
, requestVersion :: !ByteString
} deriving (Eq, Ord, Show)

httpVersion :: Parser ByteString
Expand All @@ -31,15 +51,15 @@ requestLine = Request <$> (takeWhile1 isToken <* char8 ' ')
<*> (httpVersion <* endOfLine)

data Header = Header {
headerName :: ByteString
, headerValue :: [ByteString]
headerName :: !ByteString
, headerValue :: ![ByteString]
} deriving (Eq, Ord, Show)

messageHeader :: Parser Header
messageHeader = Header
<$> (P.takeWhile isToken <* char8 ':' <* skipWhile isHorizontalSpace)
<*> ((:) <$> (takeTill isEndOfLine <* endOfLine)
<*> (many $ skipSpaces *> takeTill isEndOfLine <* endOfLine))
<*> many ( skipSpaces *> takeTill isEndOfLine <* endOfLine))

request :: Parser (Request, [Header])
request = (,) <$> requestLine <*> many messageHeader <* endOfLine
Expand All @@ -53,19 +73,10 @@ smallFile = "../http-requests.txt"
biggerFile :: FilePath
biggerFile = "../bigger.txt"

setupEnv :: IO (ByteString, ByteString)
setupEnv = do
small <- B.readFile smallFile
bigger <- B.readFile biggerFile
return (small, bigger)

main :: IO ()
main = defaultMain
[
env setupEnv $ \ ~(small, bigger) ->
bgroup "IO"
[
bench "small" $ whnf (P.parseOnly allRequests) small
, bench "bigger" $ whnf (P.parseOnly allRequests) bigger
]
[ env (B.readFile smallFile) $ \small ->
bench "small" $ whnf (P.parseOnly allRequests) small,
env (B.readFile biggerFile) $ \big ->
bench "big" $ whnf (P.parseOnly allRequests) big
]