UU-Parsinglib slowering drastically when some rules are enabled

123 views Asked by At

I'm writing a compiler using uu-parsinglib and I saw a very strange thing. I defined a pChoice combinator like:

pChoice    = foldr (<<|>) pFail

(notice, I'm using greedy <<|>).

Lets consider following code:

pFactor i = pChoice [ Expr.Var    <$> pVar
                    , Expr.Lit    <$> pLit True
                    , L.pParensed (pExpr i)
                    -- , Expr.Tuple  <$> pTuple (pOpE i)
                    -- , Expr.List   <$> pLst   (pListE i)
                    ]

Each element starts with different character - Expr.Var starts with a letter, Expr.Lit with a number, L.pParensed with parenthesis (, Expr.Tuple with brace { and Expr.List with bracket [.

I've got a big test code in which there are no tuples and no lists. The code parses in 0.15s. When I uncomment the above lines, the time increases to 0.65s. This is over 400% slowdown... How is it possible? I'm using only greedy operators and I'm sure parser is not haning in Tuple nor List section, because in the whole code there is no tuple nor list.

If you would need more code or definitions, I'll of course poste it.

1

There are 1 answers

0
Doaitse Swierstra On

I think the cause of the matter may lie in the fact that you have parameterised pFactor. This will cause each call to such a parser to build a new parser, which will take time. It is much better to create such parsers once and for all and share them in the actual parsing process. I cannot see how you are using this parser I cannot answer your questions any further.