From: pho Date: Thu, 6 Sep 2007 04:25:08 +0000 (+0900) Subject: Slight speed improvement and bugfix X-Git-Tag: RELEASE-0_2_1~35 X-Git-Url: https://git.cielonegro.org/gitweb.cgi?a=commitdiff_plain;h=7b3c7c2c5be4fc05ee03008aa0af56fab798e1bb;p=Lucu.git Slight speed improvement and bugfix darcs-hash:20070906042508-62b54-d8543d628ab0ac13a2d88602d4376f3ef0a9682b.gz --- diff --git a/Lucu.cabal b/Lucu.cabal index 93381ab..22a64f0 100644 --- a/Lucu.cabal +++ b/Lucu.cabal @@ -53,9 +53,10 @@ Extra-Source-Files: examples/HelloWorld.hs examples/Makefile ghc-options: -fglasgow-exts -fwarn-missing-signatures -fwarn-unused-imports -funbox-strict-fields -O3 +--ghc-options: -fglasgow-exts --Executable: HelloWorld --Main-Is: HelloWorld.hs --Hs-Source-Dirs: ., examples ---ghc-options: -fglasgow-exts -O3 -prof -auto-all +--ghc-options: -fglasgow-exts -fwarn-missing-signatures -fwarn-unused-imports -funbox-strict-fields -O3 -prof -auto-all diff --git a/Network/HTTP/Lucu/Parser.hs b/Network/HTTP/Lucu/Parser.hs index 80d7707..cc12cd7 100644 --- a/Network/HTTP/Lucu/Parser.hs +++ b/Network/HTTP/Lucu/Parser.hs @@ -49,18 +49,24 @@ module Network.HTTP.Lucu.Parser ) where -import Control.Monad.State +import Control.Monad.State.Strict import qualified Data.ByteString.Lazy.Char8 as B import Data.ByteString.Lazy.Char8 (ByteString) + -- |@Parser a@ is obviously a parser which parses and returns @a@. newtype Parser a = Parser { runParser :: State ParserState (ParserResult a) } -type ParserState = (ByteString, IsEOFFatal) -type IsEOFFatal = Bool +data ParserState + = PST { + pstInput :: ByteString + , pstIsEOFFatal :: !Bool + } + deriving (Eq, Show) + data ParserResult a = Success !a | IllegalInput -- 受理出來ない入力があった @@ -70,14 +76,13 @@ data ParserResult a = Success !a -- (>>=) :: Parser a -> (a -> Parser b) -> Parser b instance Monad Parser where - p >>= f = Parser $! do saved@(_, isEOFFatal) <- get -- 失敗した時の爲に状態を保存 + p >>= f = Parser $! do saved <- get -- 失敗した時の爲に状態を保存 result <- runParser p case result of - Success a -> a `seq` runParser (f a) + Success a -> runParser (f a) IllegalInput -> do put saved -- 状態を復歸 return IllegalInput - ReachedEOF -> do unless isEOFFatal - $ put saved -- 状態を復歸 + ReachedEOF -> do put saved -- 状態を復歸 return ReachedEOF return x = x `seq` Parser $! return $! Success x fail _ = Parser $! return $! IllegalInput @@ -91,9 +96,9 @@ failP = fail undefined parse :: Parser a -> ByteString -> (ParserResult a, ByteString) parse p input -- input は lazy である必要有り。 = p `seq` - let (result, (input', _)) = runState (runParser p) (input, True) + let (result, state') = runState (runParser p) (PST input True) in - result `seq` (result, input') -- input' も lazy である必要有り。 + result `seq` (result, pstInput state') -- pstInput state' も lazy である必要有り。 -- |@'parseStr' p str@ packs @str@ and parses it. parseStr :: Parser a -> String -> (ParserResult a, ByteString) @@ -104,32 +109,32 @@ parseStr p input anyChar :: Parser Char anyChar = Parser $! - do (input, isEOFFatal) <- get + do state@(PST input _) <- get if B.null input then return ReachedEOF else - do put (B.tail input, isEOFFatal) + do put $! state { pstInput = B.tail input } return (Success $! B.head input) eof :: Parser () eof = Parser $! - do (input, _) <- get + do PST input _ <- get if B.null input then - return $ Success () + return $! Success () else return IllegalInput -- |@'allowEOF' p@ makes @p@ treat reaching EOF a normal failure. allowEOF :: Parser a -> Parser a allowEOF f = f `seq` - Parser $! do (input, isEOFFatal) <- get - put (input, False) + Parser $! do saved@(PST _ isEOFFatal) <- get + put $! saved { pstIsEOFFatal = False } result <- runParser f - (input', _) <- get - put (input', isEOFFatal) + state <- get + put $! state { pstIsEOFFatal = isEOFFatal } return result @@ -160,13 +165,13 @@ infixr 0 <|> (<|>) :: Parser a -> Parser a -> Parser a f <|> g = f `seq` g `seq` - Parser $! do saved@(_, isEOFFatal) <- get -- 状態を保存 + Parser $! do saved <- get -- 状態を保存 result <- runParser f case result of - Success a -> return $ Success a + Success a -> return $! Success a IllegalInput -> do put saved -- 状態を復歸 runParser g - ReachedEOF -> if isEOFFatal then + ReachedEOF -> if pstIsEOFFatal saved then return ReachedEOF else do put saved diff --git a/Network/HTTP/Lucu/RFC1123DateTime.hs b/Network/HTTP/Lucu/RFC1123DateTime.hs index 580691b..4d7aa15 100644 --- a/Network/HTTP/Lucu/RFC1123DateTime.hs +++ b/Network/HTTP/Lucu/RFC1123DateTime.hs @@ -37,7 +37,7 @@ formatRFC1123DateTime time fmtDec 2 (ctMin time) ++ ":" ++ fmtDec 2 (ctSec time) - ++ ":" ++ + ++ " " ++ id (ctTZName time)