StringTemplate 性能不佳
我正在使用 StringTemplate 从数据集生成一些 xml 文件。有时,我的数据集中有超过 100,000 条记录,这些记录由模板中的循环枚举。它运行得很慢(每次操作 15-20 秒),所以性能对我来说不好。
这是我如何使用 ST 呈现报告的示例:
using (var sw = new StringWriter())
{
st.Write(new StringTemplateWriter(sw));
return sw.ToString();
}
StringTemplateWriter 是一个从 IStringTemplateWriter 派生的简单编写器类,没有缩进。
顺便说一句,在调试屏幕中我看到很多这样奇怪的消息:
“StringTemplate.DLL 中发生了类型‘antlr.NoViableAltException’的第一次机会异常”
在深度调试中,我发现它会递归地解析我的模板,如果出现失败(不知道到底是什么)它抛出 NoViableAltException 异常以从堆栈深处返回到表面,所以我猜问题在于使用了太多的 try-catch- throw 。
谷歌没有发现任何有用的东西。
主要问题:如何减少这个异常数量(除了重写ST的代码)并提高模板渲染的性能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ST 使用 ANTLR 解析 ST 模板和组。如果您遇到语法错误,则表明您的模板有错误。所有的赌注都取决于性能,因为它会抛出一个异常。 ANTLR/ST 没有错;)
特伦斯
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
NoViableAltException 听起来像是解析器错误。我不确定为什么使用 ANTLR(除非它们来自同一作者),但我能想到的唯一猜测是模板语言本身是使用 ANTLR 解析的。也许模板包含错误?无论如何,ANTLR 的错误处理确实很慢(一方面,它使用异常),所以这可能就是你的模板扩展很慢的原因。
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.