Poor StringTemplate Performance

I use StringTemplate to create some XML files from datasets. Sometimes I have over 100,000 records in a dataset that are listed in a template. It goes very slowly (15-20 seconds per operation), so performance is not suitable for me.

This is an example of how I use ST to display a report:

using (var sw = new StringWriter()) { st.Write(new StringTemplateWriter(sw)); return sw.ToString(); } 

StringTemplateWriter is a simple writer class derived from IStringTemplateWriter without indentation.

By the way, on the debug screen, I see a lot of such a strange message:
"The first exception of type" antlr.NoViableAltException "exception occurred in StringTemplate.DLL

deep in debugging, I found that it recursively parses my template and if something fails (I don’t know what exactly), it throws a NoViableAltException to return from the depth of the stack back to the surface, so I think the problem is there are too many try-catch-throw's in use.

Google did not find anything useful in this.

The main question: how to reduce this number of exceptions (except for rewriting the ST code) and increase the rendering performance of the template?

+4
source share
2 answers

ST parses ST patterns and groups with ANTLR. If you get syntax errors, your template has errors. All bets are disabled for performance because they generate an exception for each of them. ANTLR / ST is not to blame here;) Terence

+4
source

NoViableAltException sounds like a parser error. I'm not sure why ANTLRs are used (except that they belong to the same author), but the only thing I can think of is that the template language itself is parsed using ANTLRs. Maybe the template contains errors? In any case, ANTLR error handling is very slow (for example, it uses exceptions), so this is probably why template expansion is slow.

0
source

Source: https://habr.com/ru/post/1308237/


All Articles