Skip to content

GH-1324: bad performance when parsing huge literals - #4423

Open
OyvindLGjesdal wants to merge 3 commits into
apache:mainfrom
OyvindLGjesdal:GH-1324-bad-performance-when-parsing-huge-literals
Open

OyvindLGjesdal wants to merge 3 commits into
apache:mainfrom
OyvindLGjesdal:GH-1324-bad-performance-when-parsing-huge-literals

Conversation

@OyvindLGjesdal

@OyvindLGjesdal OyvindLGjesdal commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

GitHub issue resolved #1324, #4428

Pull request Description:

The SimpleCharStream that javacc generates grows its buffer by 2048 chars at a time, so parsing a long token, such as a 100MB literal, takes quadratic time. The javacc fix (javacc/javacc#303, corrected by the still open javacc/javacc#317) is not in a release.

  • The ARQ grammar uses USER_CHAR_STREAM (#ifdef ARQ in main.jj) with ARQCharStream, based on the SimpleCharStream from javacc 7.0.12. It doubles the buffer, up to Integer.MAX_VALUE - 8 chars (a longer token is a parse error), and no longer stores line and column for every buffered char (2 bytes per buffered char instead of 10). The SPARQL 1.2 parser is unchanged.
  • The ARQ parser is regenerated with the grammar script (javacc 7.0.12) in a separate commit.
  • An error from the Reader, such as invalid UTF-8 in an update request, is a parse error with the Reader's exception as its cause. The token manager took it as the end of the input, so on main the rest of the update is silently dropped. An unchecked exception from the Reader, such as a cancellation, is passed on unchanged.
  • API: org.apache.jena.sparql.lang.arq.javacc.SimpleCharStream is no longer generated, and the generated ARQParserTokenManager takes a CharStream. Code that builds the token manager directly uses new ARQCharStream(...). The ARQParser constructors are unchanged.
  • TestARQCharStream covers buffer growth, the buffer size, reader errors, the end of the input, and line and column after a long literal.
  • TestParseLargeLiteral in jena-benchmarks-jmh parses a query with a literal of 1MB to 250MB with the ARQ parser, and with Jena 5.6.0 up to 100MB.

JMH, ms per parse (this PR: average of 4; Jena 5.6.0: average of 2):

Literal Jena 5.6.0 This PR, ARQ
1MB 290 142
2MB 821 247
10MB 9,657 1,149
100MB 855,886 (14 min) 11,351
250MB 27,791

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.

  • Tests are included.
  • Documentation change and updates are provided for the Apache Jena website
  • Commits have been squashed to remove intermediate development commit messages.
  • Key commit messages start with the issue number (GH-xxxx)

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.

@OyvindLGjesdal OyvindLGjesdal changed the title Gh 1324 bad performance when parsing huge literals GH-1324: bad performance when parsing huge literals Nov 16, 2026
@OyvindLGjesdal OyvindLGjesdal added the AI Assisted Assisted or generated by AI label Nov 16, 2026
@OyvindLGjesdal
OyvindLGjesdal marked this pull request as draft November 16, 2026 21:33
@OyvindLGjesdal

Copy link
Copy Markdown
Contributor Author

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.

@ThomasThelen

ThomasThelen commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Will this be removed if/when the next JavaCC version is released? Or if Jena moves to JavaCC 8?

@OyvindLGjesdal
OyvindLGjesdal force-pushed the GH-1324-bad-performance-when-parsing-huge-literals branch 2 times, most recently from d6cd74f to dba571e Compare November 17, 2026 08:02
@OyvindLGjesdal

Copy link
Copy Markdown
Contributor Author

Will this be removed if/when the next JavaCC version is released? Or if Jena moves to JavaCC 8?

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:

  • SPARQL 1.1 parser: jena-arq/.../sparql/lang/sparql_11/javacc/JavaCharStream.java (only when chosen)
  • SPARQL 1.0 parser: jena-arq/.../sparql/lang/sparql_10/javacc/JavaCharStream.java (only when chosen)
  • SSE parser: jena-arq/.../sparql/sse/lang/javacc/SimpleCharStream.java (algebra)
  • CDT literal parser: jena-arq/.../cdt/javacc/SimpleCharStream.java (could be large lists and sets in CDTS?)
  • javacc Turtle parser: jena-arq/.../riot/lang/turtlejcc/javacc/SimpleCharStream.java
  • SHACL compact syntax: jena-shacl/.../shacl/compact/reader/javacc/SimpleCharStream.java (on by default)
  • ShEx compact syntax: jena-shex/.../shex/parser/javacc/SimpleCharStream.java (on by default)

@OyvindLGjesdal
OyvindLGjesdal marked this pull request as ready for review November 17, 2026 10:13
@afs

afs commented Nov 17, 2026

Copy link
Copy Markdown
Member

Please don't change the SPARQL 1.2 parser. Limit the changes to ARQ using #ifdef.

ARQ is the default and normal parser.

The SPARQL 1.2 parser (sparql_12.jj,sparql_12.txt) is used to make the grammar in the SPARQL 1.2 Query spec.
(Ditto 1.1 and 1.0 which are frozen copies.)

The grammar script needs to be run.

It is done this way so Javacc isn't needed to run the build and also so the generated files can be cleaned up.
Then the generated code is checked in and the build does not need javacc installed.

It would be good to call the implementation ARQCharStream to avoid the JavaCC name.

@OyvindLGjesdal
OyvindLGjesdal force-pushed the GH-1324-bad-performance-when-parsing-huge-literals branch from dba571e to 42745dd Compare November 17, 2026 18:10
@OyvindLGjesdal
OyvindLGjesdal marked this pull request as draft November 17, 2026 18:19
@OyvindLGjesdal
OyvindLGjesdal force-pushed the GH-1324-bad-performance-when-parsing-huge-literals branch 3 times, most recently from 957f280 to c443beb Compare November 17, 2026 20:47
OyvindLGjesdal and others added 3 commits November 17, 2026 22:17
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>
@OyvindLGjesdal
OyvindLGjesdal force-pushed the GH-1324-bad-performance-when-parsing-huge-literals branch from c443beb to 316f19f Compare November 17, 2026 21:17
@OyvindLGjesdal
OyvindLGjesdal marked this pull request as ready for review November 17, 2026 22:02
@OyvindLGjesdal

Copy link
Copy Markdown
Contributor Author

Thanks for the review @afs

Changed to only use 'USER_CHAR_STREAM' for ARQ parser and renamed the class to TestARQCharStream and trying to make the changes small. Second commit contains the output from run. The benchmark could also be kept imo since it isn't run unless specifically called, but it could also be removed and just be for testing during the PR.

Comment thread jena-arq/Grammar/arq.jj
JAVA_UNICODE_ESCAPE = false ;
UNICODE_INPUT = true ;
STATIC = false ;
// ARQCharStream (GH-1324): see the parser class below.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Assisted Assisted or generated by AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SPARQL Update: a failed read can commit part of the request Poor performance when parsing huge literal in query (e.g. 100MB)

3 participants