antlr4 parser re-use and warm up
Asked Answered
G

1

8

In my use case I have to parse several thousand small and independent expressions into a tree representation using a Visitor on the generated parse trees. Currently new streams, lexer and parser instances are created for each parse operation.

I assume this might not be optimal. Which object instances could be re-used in such a setup to make use of the warm-up property of ANTLR4? How about thread safety - which of these instances should be thread local? Is a reset of some kind required to re-use a lexer or parser instance?

Genu answered 6/3, 2013 at 0:16 Comment(0)
S
11

In the early days of ANTLR 4 (many months before its initial release), the adaptive DFA cache was created on a per-instance basis, so the use of Lexer.setInputStream or Parser.setInputStream was essential for achieving good performance.

This is no longer the case. The background cache is now shared among all parser instances and is thread safe. The methods of the Lexer and Parser classes are not thread safe, so if you want to parse on multiple threads, you will need to create multiple instances of your lexer and parser.

Sitting answered 6/3, 2013 at 2:40 Comment(1)
Does this include all the runtimes and targets as well?Hocker

© 2022 - 2024 — McMap. All rights reserved.