Fix parser performance#2
Closed
LennartSpitzner wants to merge 9 commits into
Closed
Conversation
hopefully makes the history easier to consume
Tests expect other option for ambiguous parses (nested parens with a select -> Should be WithParensSelectWithParens)
Author
|
Replaced by #8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey, thanks for maintaining this package! And for considering this non-trivial PR :)
This PR fixes some bad parsing behaviour that leads to exponential time usage.
This PR looks big, but note that I applied an auto-formatter on
Parsing.hsbefore I started to work on it. If this is a blocker just tell me and I can rebase the changes on a non-auto-formatted branch. To see the changes beyond auto-formatting see proda-ai#2, which is everything but the first commit here.Testcase
Parsing this does terminate, but you definitely start noticing the cpu time, and if you profile you can see some suspiciously large values. We had some larger queries that did not terminate, but with the same underlying problem.
Analysis
There are a couple of parsers around
aExpr,customizedAExpr,cExpr,customizedCExprthat have alternatives with common prefixes and with ambiguous parses.customizedAExpr/suffix)aExpris a prefix of bothInParensCExprorImplicitRowCExprcolIdis a prefix of bothFuncCExprandColumnrefCExprOn their own these might be harmless, but once such cases nest they can create a problem. I think the last of the above is just a constant-factor problem (so rather harmless); still, I cleaned these up in the PR because a) it was relatively simple to do b) It is hard to diagnose which common prefixes lead to problems, so it makes debugging the whole thing easier if they get cleaned up.
both should correspond to something like
((SELECT …)).The ambiguity might be known because the hedgehog tests explicitly generate only one of those two options (see
postgresql-syntax/hedgehog-test/Main/Gen.hs
Line 475 in 6caff75
Approach
I follow a simple process: Take any alternative (
asum [..]) where alternatives have common prefixes, remove those alternatives and replace with a single new one that first matches the common prefix, then branches again. As an example, I replacedwith
It is not always this simple, for example if the
inParensappears deeper in the call stack then this gets messier, with multiple newXTailbindings.Notes
I am not sure if I applied
endHeadandwrapHeadcorrectly everywhere. The tests pass, but I am not sure that the tests cover the readability of the error-messages (iiuc the "HeadParser" also has the purpose of helping with those).There are some cases still where the order of the parsers (in
asums) matters. I don't think that is ideal, nor should it be necessary. As I understand it this might be due to committing to some branch (viaendHead) too soon. But it is also harmless given that the tests pass.I added https://github.com/proda-ai/postgresql-syntax/blob/e0450870e35dcbfd4a11d6757bcb54c4bd65cc10/library/PostgresqlSyntax/Parsing.hs#L1288-L1301 after resolving the ambiguity-problem: The tests expect nested
WithParensSelectWithParens, so the parser simply convertsInParensCExpr (CExprAExpr …)to that where necessary. Could instead fix the tests, but maybe the slightly flatter trees for nested selects are worth this cost. I have no idea why exactly the AST types are defined as they are, to be honest.I feel like the code now combines several different approaches to ensure good performance:
customizedCExprapproach to parameterise by some child-parserI am not proud of my addition as it makes the whole logic harder to follow. It might be cleaner make use of continuations, but still that does not improve things much. But I don't see an easy-to-read/maintain approach with good performance ..
At least I only had to touch the "innermost loop" when parsing expressions, so maybe it is worth it.
the fact that this
postgresql-syntax/library/PostgresqlSyntax/Parsing.hs
Line 1025 in 6caff75
aExprinstead ofbasemeans that a) you get a more "left-leaning" output tree but also b) that if parsing fails, you might end up failing again and again for every level of a nestedaExpr. Same forbExpr. I left a note about this in the code. Could be fixed by the same "parse different tree (usingbase), then post-process to restructure the tree" approach that I used for nested selects.might be nice to have performance tests included. There should be some simple trivial expression like
((((((((((a+b))))))))))that where parsing does not terminate on master but is instant with this PR. Might need a bit more nesting. But yeah, perf tests are a bit annoying at times because they can depend on the performance of the system running the tests.