'How to improve antlr4 runtime performance on c++ target?

My tool intention is calculate the results parsed from antlr4. The inputs had break into many lines, eg: 1+1, 3*4, ... Each line will init the parser and call the visitor. Therefore, if there is 1000 lines, it will call 1000 times of antlr4 parser. It run perfectly except the performances are slightly slow on the device. I hope to improve the performances even there is 1second faster.

I'm using antlr4.7 as runtime. The runtime target is on c++.

for (itFormulas = fMap - > begin(); itFormulas != fMap - > end(); itFormulas++) 
{
  auto initStart = std::chrono::high_resolution_clock::now();

  ANTLRInputStream input(itFormulas - > second);
  FormulaLexer lexer( & input);
  CommonTokenStream tokens( & lexer);
  FormulaParser parser( & tokens);
  FormulaParser::MainContext * mainContext = parser.main();

  auto initEnd = std::chrono::high_resolution_clock::now();
  std::chrono::duration < double > totalInit = initEnd - initStart;

  //calculate time
  totalInitTime += totalInit.count();

  auto visitStart = std::chrono::high_resolution_clock::now();

  EvaluatorVisitor visitor(formulaMap, resultMap, inputMaps);

  try {
    antlrcpp::Any result = visitor.visit(mainContext - > children[0]);

    auto visitEnd = std::chrono::high_resolution_clock::now();
    std::chrono::duration < double > totalElapsed = visitEnd - visitStart;

    //calculate time
    totalVisitTime += totalElapsed.count();

    resultMap - > insert(pair < string, double > (itFormulas - > first, v));
  } catch (EvaluationException ex) {
    string from = "Mistake" + string(" itFormulas->") + itFormulas - > first;
  }
}

If the total routine will took 5seconds The parser time will be around 1 second, and the visitor time will be 4seconds.

I expect the time could be shorter, and wish to know which part can be optimised.



Solution 1:[1]

The runtime update 4.9.3 should improve the performance thanks to https://github.com/antlr/antlr4/pull/3318

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 v.oddou