Repository navigation
GH-1324: bad performance when parsing huge literals - #4423
OyvindLGjesdal wants to merge 3 commits into
Conversation
|
I see that this is a something that can wait for a future release, if usable, since we are preparing for a new Jena version, but have been thinking about it. |
|
Will this be removed if/when the next JavaCC version is released? Or if Jena moves to JavaCC 8? |
d6cd74f to
dba571e
Compare
Yes, the idea was to fix the parsing now, by using the USER_CHAR_STREAM mechanism. However I think CC8 has the same growth problem and that is was only fixed for 7 (without a release). Note that I think this PR solves the common case, but still has some parsers with the same issue:
|
|
Please don't change the SPARQL 1.2 parser. Limit the changes to ARQ using ARQ is the default and normal parser. The SPARQL 1.2 parser ( The It is done this way so Javacc isn't needed to run the build and also so the generated files can be cleaned up. It would be good to call the implementation |
dba571e to
42745dd
Compare
957f280 to
c443beb
Compare
The SimpleCharStream that javacc generates grows its buffer by 2048 chars at a time, so reading a long token, such as a large literal, takes quadratic time. javacc's master branch grows the buffer exponentially since this commit, from javacc PR 303: javacc/javacc@3bb3dbf javacc/javacc#303 It uses bufsize >> 1, which halves the buffer: javacc/javacc#316 Open javacc PR 317 corrects it to bufsize << 1: javacc/javacc#317 Neither is in a javacc release; the latest is 7.0.13. For the ARQ parser only (#ifdef ARQ in main.jj), set the javacc option USER_CHAR_STREAM, so javacc no longer generates SimpleCharStream.java, and add the InputStream and Reader constructors and ReInit operations that javacc leaves out with this option. They create an ARQCharStream. ARQCharStream, in org.apache.jena.sparql.lang.arq, is based on the SimpleCharStream that javacc 7.0.12 generates from this template: https://github.com/javacc/javacc/blob/javacc-7.0.12/src/main/resources/templates/SimpleCharStream.template with these changes: - ExpandBuff doubles the buffer instead of adding 2048 chars, so reading a long token takes linear time. - The buffer grows to at most Integer.MAX_VALUE - 8 chars, the largest array length that is safe on common JVMs. A longer token is a TokenMgrError, which the query and update parsers report as a parse error. A buffer size below 1 is an IllegalArgumentException: such a buffer never grows. - Line and column are no longer stored for every buffered char. They are kept for the start of the token, and the end of the token is calculated when asked for. The buffer uses 2 bytes per char instead of 10. After a lexical error, they are right up to the error, where the parsers stop. - An exception from the Reader, checked or unchecked, is a TokenMgrError. The token manager takes an exception from the char stream as the end of the input, so the rest of the input was silently dropped: an update with a byte that is not valid UTF-8 (MalformedInputException) parsed as an empty or shortened update. Only the end of the input is an IOException, and the Reader, closed at the end of the input, is not read again. - adjustBeginLineColumn is left out: it is not part of CharStream and nothing calls it. ParserARQ and ParserARQUpdate report a failed read as a QueryParseException with the Reader's exception as its cause. An unchecked exception from the Reader, such as a cancellation, is not a parse error: they pass it on unchanged. TestARQCharStream checks that the buffer grows by doubling (8 expansions for a token of 1,000,000 chars after the buffer has wrapped round, where growing by 2048 chars takes 487), that a buffer size of 0 is rejected, that a failing Reader is an error, that an update with invalid UTF-8 is a parse error with the decoder's exception as its cause, that an unchecked exception from the Reader is passed on, that queries ending on a token the lexer reads past still parse, and the line and column of a parse error after a long multi-line literal. It also checks that a failed read of an update, executed in a write transaction as Fuseki does, commits nothing: a transfer whose connection drops after two of its four operations, and 300 operations with a Latin-1 byte after the decoder's first read. On main, both commit part of the update. The grammar script removes CharStream.java before running javacc, so that it is regenerated. The parser is regenerated in the next commit. This commit does not compile on its own: javacc generates the CharStream interface. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run jena-arq/Grammar/grammar with javacc 7.0.12 and GNU cpp after the USER_CHAR_STREAM change to main.jj. javacc now generates the CharStream interface for the ARQ parser, which ARQCharStream implements. ARQParser and its token manager use CharStream in place of SimpleCharStream, and the InputStream and Reader constructors come from the parser class in main.jj. The generated SimpleCharStream.java of the ARQ parser is removed. The script also regenerates the SPARQL 1.2 parser, which comes out the same as before. javacc names its lookahead methods after their line in the .jj file, and the lines added to main.jj move the grammar down, so most of the ARQParser.java diff is renamed methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TestParseLargeLiteral parses a query with one literal of 1, 2, 10, 100 and 250MB with the ARQ parser. The literal is WKT polygon coordinates: in the issue, a SERVICE request with a geometry literal of about 100MB stalled in the parser. The same queries are parsed with Jena 5.6.0 up to 100MB. Its SimpleCharStream grows the buffer by 2048 chars at a time, so the parse time grows with the square of the literal's length: one parse of 100MB takes about a quarter of an hour. Jena 5.6.0 runs separately, with fewer iterations and a 30 minute timeout per iteration. With the buffer doubling, the time grows linearly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
c443beb to
316f19f
Compare
|
Thanks for the review @afs Changed to only use 'USER_CHAR_STREAM' for ARQ parser and renamed the class to |
| JAVA_UNICODE_ESCAPE = false ; | ||
| UNICODE_INPUT = true ; | ||
| STATIC = false ; | ||
| // ARQCharStream (GH-1324): see the parser class below. |
There was a problem hiding this comment.
Minor nit that might not be a problem - I see the ticket number mentioned 5 times in the changes. There are a few places Jena has this, but it doesnt look like an established pattern.
GitHub issue resolved #1324, #4428
Pull request Description:
changes are in first commit, result from running javacc generation in second commit, benchmark in third commit.
Tests are fully created by AI, have not reviewed if they make sense.
By submitting this pull request, I acknowledge that I am making a contribution to the Apache Software Foundation under the terms and conditions of the Contributor's Agreement.