You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
Activity
mp911de commentedon Aug 17, 2026
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 toLL(*)prediction mode after aParseCancellationException.gregjotau commentedon Aug 29, 2026
I independently tested the exact PR patch on the
4.2.0-M1tag and on a large Kotlin/Spring Boot application. The application has 231 Spring Data repositories and 729@Querymethods. Tests used JDK 26.0.2.1, a representative local PostgreSQL database, fresh JVMs, and random HTTP ports.For 10 interleaved full-application pairs:
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.
FlightRecorderApplicationStartupattribution on the same artifacts showed cumulativespring.data.repository.initfalling 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):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:
Eql.g4andJpql.g4do not contain the overlapping HQLprimaryExpression -> syntacticDomainPath | functionpath 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.gregjotau commentedon Aug 29, 2026
@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 :)
Polishing.
Consolidate JPQL/EQL/HQL tests.
Polishing.
Reduce HQL parser cold-start allocation.
Update hql query rendering.