Skip to content

[Java:Runtime] perf(runtime): Optimize thread safety and performance for DFA state management #4842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

dingxin-tech
Copy link

Hi, team

As mentioned in issue #2454, I also found a performance bottleneck in ANTLR caused by the lock on the DFA.states object. Due to lock contention, the performance degrades significantly under high concurrency.

In my test case — parsing a SQL file with 400,000 characters — a single parse took about 200ms. However, when I increased the concurrency to 32 threads, the per-request latency jumped to 5–10 seconds. While not all SQL statements are this long, and it's expected that longer queries take more time, the current implementation introduces a serious issue: a single long SQL parsing task can block many short ones due to the global lock, causing the latency of short SQL statements to increase dramatically beyond expectations.

This problem can be resolved with a very simple change: replacing the synchronized HashMap with a ConcurrentHashMap.

@dingxin-tech dingxin-tech changed the title perf(runtime): Optimize thread safety and performance for DFA state management [Java:Runtime] perf(runtime): Optimize thread safety and performance for DFA state management Jun 3, 2025
@jimidle
Copy link
Collaborator

jimidle commented Jun 10, 2025

It is very likely that your grammar is the root of the problem. What grammar are you using?

@ericvergnaud
Copy link
Contributor

@dingxin-tech can you touch this PR in order to re-run the CI ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants