Skip to content

Commit

Permalink
Improve parsing of string literals (#592)
Browse files Browse the repository at this point in the history
This adds a fast path for bulk parsing of both double-quoted and
single-quoted string literals, which improves performance ~10x on
double-quoted string literals and ~100x on single-quoted string
literals.
  • Loading branch information
Gabriella439 committed Sep 19, 2018
1 parent b60f8f6 commit 4e11992
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
2 changes: 2 additions & 0 deletions benchmark/parser/Main.hs
Expand Up @@ -69,5 +69,7 @@ main = do
]
, benchExprFromText "Long variable names" (T.replicate 1000000 "x")
, benchExprFromText "Large number of function arguments" (T.replicate 10000 "x ")
, benchExprFromText "Long double-quoted strings" ("\"" <> T.replicate 1000000 "x" <> "\"")
, benchExprFromText "Long single-quoted strings" ("''" <> T.replicate 1000000 "x" <> "''")
, benchParser prelude
]
31 changes: 23 additions & 8 deletions src/Dhall/Parser/Expression.hs
Expand Up @@ -469,7 +469,8 @@ doubleQuotedChunk :: Parser a -> Parser (Chunks Src a)
doubleQuotedChunk embedded =
choice
[ interpolation
, unescapedCharacter
, unescapedCharacterFast
, unescapedCharacterSlow
, escapedCharacter
]
where
Expand All @@ -479,14 +480,19 @@ doubleQuotedChunk embedded =
_ <- Text.Parser.Char.char '}'
return (Chunks [(mempty, e)] mempty)

unescapedCharacter = do
c <- Text.Parser.Char.satisfy predicate
return (Chunks [] (Data.Text.singleton c))
unescapedCharacterFast = do
t <- Text.Megaparsec.takeWhile1P Nothing predicate
return (Chunks [] t)
where
predicate c =
('\x20' <= c && c <= '\x21' )
( ('\x20' <= c && c <= '\x21' )
|| ('\x23' <= c && c <= '\x5B' )
|| ('\x5D' <= c && c <= '\x10FFFF')
) && c /= '$'

unescapedCharacterSlow = do
_ <- Text.Megaparsec.single '$'
return (Chunks [] "$")

escapedCharacter = do
_ <- Text.Parser.Char.char '\\'
Expand Down Expand Up @@ -545,7 +551,8 @@ singleQuoteContinue embedded =
, interpolation
, escapeInterpolation
, endLiteral
, unescapedCharacter
, unescapedCharacterFast
, unescapedCharacterSlow
, tab
, endOfLine
]
Expand All @@ -571,12 +578,20 @@ singleQuoteContinue embedded =
_ <- Text.Parser.Char.text "''"
return mempty

unescapedCharacter = do
unescapedCharacterFast = do
a <- Text.Megaparsec.takeWhile1P Nothing predicate
b <- singleQuoteContinue embedded
return (Chunks [] a <> b)
where
predicate c =
('\x20' <= c && c <= '\x10FFFF') && c /= '$' && c /= '\''

unescapedCharacterSlow = do
a <- satisfy predicate
b <- singleQuoteContinue embedded
return (Chunks [] a <> b)
where
predicate c = '\x20' <= c && c <= '\x10FFFF'
predicate c = c == '$' || c == '\''

endOfLine = do
a <- "\n" <|> "\r\n"
Expand Down

0 comments on commit 4e11992

Please sign in to comment.