Skip to content

Reduce HQL parser cold-start allocations caused by ambiguous grammar alternatives #4326

Closed

Description

@araiprof

JpaQueryEnhancer.forHql(…) has high cold-start cost because several alternatives in Hql.g4 overlap. The most significant overlap is that primaryExpression accepts both syntacticDomainPath and function, while syntacticDomainPath itself accepts several alternatives beginning with function. ANTLR consequently performs expensive adaptive prediction and allocates heavily while its DFA is cold.

This is visible in application startup recordings when repositories contain many declared HQL queries. In a corpus of 534 non-native @Query and countQuery declarations, parsing on a fresh JVM averaged 2,182.9 ms and 3,742.8 MiB allocated on current main (054f8956e11b23212f5b0caf11bf2cf507d2a35a).

I have a grammar refactoring that removes the overlapping function path, inlines predicate helper rules, and aligns those sections with Hibernate ORM's current HQL grammar. The same corpus then averages 743.8 ms and 1,217.7 MiB allocated: 65.9% less time and 67.5% less allocation across ten fresh JVM forks.

I also created a self-contained JMH SingleShotTime benchmark using a synthetic HQL query, ten forks, no warmup, and -prof gc. On an Apple M5 Pro with OpenJDK 26.0.2 and ANTLR 4.13.2:

Time Allocation
Current main 417.710 ms/op 544,605,709 B/op
Refactored grammar 209.177 ms/op 176,776,378 B/op
Reduction 49.9% 67.5%

The refactoring preserves behavior across all 534 application queries. I compared rendered query text, alias and projection detection, constructor/select classification, sorted and unsorted rewrites, and count-query derivation; all results were byte-for-byte identical. The HQL test group (422 tests) and all 968 unit tests pass. The broader integration suite also passes except for the PostgreSQL vector tests that require an unavailable Docker daemon.

I will submit the patch with the reproducible cold-start benchmark and visitor updates required by the factored grammar.

Activity

  1. self-assigned this
    on Aug 17, 2026
  2. mp911de commented on Aug 17, 2026

    @mp911de
    Member

    Thanks, this is an interesting suggestion. How did you come across these cases?

    Could you also have a look at the EQL and JPQL parsers? I assume these changes may help with ambiguity resolution there as well. In JpaQueryEnhancer.parse(…), we fall back to LL(*) prediction mode after a ParseCancellationException.

  3. gregjotau commented on Aug 29, 2026

    @gregjotau
    Contributor

    I independently tested the exact PR patch on the 4.2.0-M1 tag and on a large Kotlin/Spring Boot application. The application has 231 Spring Data repositories and 729 @Query methods. Tests used JDK 26.0.2.1, a representative local PostgreSQL database, fresh JVMs, and random HTTP ports.

    For 10 interleaved full-application pairs:

    Baseline Patched
    Boot startup, mean 15.2566 s 13.7356 s
    Boot startup, median 15.0345 s 13.6035 s

    The paired improvement was 1.5210 s (9.97%), with a 95% CI of 1.1801–1.8619 s. All 10 pairs favored the patch.

    FlightRecorderApplicationStartup attribution on the same artifacts showed cumulative spring.data.repository.init falling from 4.1612 s to 2.8484 s (-31.6%), while total context refresh fell from 14.9860 s to 13.7016 s. In JFR hot-method samples, ParserATNSimulator.closure_ fell from 6.01% to 1.77%.

    I also reproduced the PR's JMH benchmark with 10 fresh forks (SingleShotTime, GC profiler):

    Baseline Patched
    Time 405.980 ± 26.846 ms/op 195.260 ± 14.589 ms/op
    Allocation 544,984,186 ± 101,613 B/op 176,592,053 ± 160,615 B/op

    That is 51.9% less time and 67.6% less allocation, with non-overlapping 99.9% confidence intervals.

    Regarding EQL and JPQL: I ran a separate 10-fork cold comparison using the same JPQL-compatible complex query on current main:

    Parser Time Allocation
    HQL 319.708 ± 23.748 ms 369.47 MB
    EQL 92.983 ± 4.018 ms 48.04 MB
    JPQL 82.110 ± 1.830 ms 41.22 MB

    Eql.g4 and Jpql.g4 do not contain the overlapping HQL primaryExpression -> syntacticDomainPath | function path changed by the PR. On this cold-query benchmark they are already 3.4–3.9x faster and allocate 7.7–9.0x less than HQL, so I do not see evidence for applying an analogous grammar change to them.

  4. gregjotau commented on Aug 29, 2026

    @gregjotau
    Contributor

    @mp911de sorry for answering a bit late here, I opened this PR from my wife's computer initially, forgot about it and did not get notification.

    I'm just playing with GPT 5.6 sol doing performance optimisations on our apps: reai.no (accounting platform) and a few other apps we have :)

  5. added this to the 4.2.0-M2 (2026.1.0) milestone on Sep 18, 2026
  6. added a commit that references this issue on Sep 18, 2026
    8388cbd
  7. added a commit that references this issue on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions