views:

270

answers:

2

I'm using StringTemplate to generate some xml files from datasets. Sometimes I have more than 100,000 records in the dataset that is enumerated by a loop in a template. It goes very slow (15-20 secs per operation) so performance is not good for me.

This is an example how I use ST to render 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, in the debug screen I see a lot of such weird message:
"A first chance exception of type 'antlr.NoViableAltException' occurred in StringTemplate.DLL"

in a deep of debug I found that it parses my template recursively and if something failed (don't know what exactly) it throws NoViableAltException exception to return from a deep of stack back to a surface, so I guess the problem is in using of too much try-catch-throw's.

Google found nothing useful on this.

Main question: how to decrease this number of exceptions (except rewriting the code of ST) and improve performance of template rendering?

A: 

NoViableAltException sounds like a parser error. I am not sure why ANTLR is being used (except that they come from the same author), but the only guess I can come up with is that the template language itself is parsed using ANTLR. Maybe the template contains errors? Anyway ANTLR's error handling is really slow (for one, it uses exceptions) so that's probably why your template expansion is slow.

Krumelur
yes, this is seems to be a bad performance of ANTLR. so do you have any recommendation about yet another string template engine with better performance? and thanx for your response.
Genius
Just to clarify, I did not say that StringTemplate is slow, only that the default error handler implementation of ANTLR is slow (and not very helpful either). I think that once you fix the underlying errors, you will notice that performance will improve a lot.
Krumelur
+2  A: 

Hi gang. ST parses ST templates and groups with ANTLR. If you are getting syntax errors, your template(s) have errors. All bets are off for performance as it throws an exception for each one. ANTLR/ST not at fault here ;) Terence

Terence Parr
thanks. my templates do not give any errors during initialization (so they work properly), but probably you're right - the problem is in some minor error in templates. it is hard to find the reason since there are a lot of templates.
Genius