antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

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

Open dingxin-tech opened this issue 5 months ago • 1 comments
trafficstars

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 avatar Jun 03 '25 06:06 dingxin-tech