Tiny在上周上线TinyUiEnterprise的http://www.tinygroup.org/组件,经过反馈测试发现模板引擎的性能一直有问题,请查看jprofile
?当然很多性能问题,我们正在完善中,诸如:非递归DFS、大对象生命周期、异步调用......
但是放在我面前的却是把原先ClassLoader加载后,进行调用执行的方式变更为使用anltr的解释性语言去执行。
通过一周左右的攻克,总算把最难啃的骨头给啃掉了,这离不开beetl的贡献,这里有些运用了其中核心代码,通过bytewrite的方式进行调用
本文不在过多论述anltr到底是什么玩意,我感觉这类文章在网上搜索一大堆,推荐去阅读http://blog.csdn.net/dc_726/article/details/45399371
核心代码
}
//语法分析
BeetlLexer lexer = new BeetlLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(syntaxError);
//token就是语法树上节点,递归下降解析器
CommonTokenStream tokens = new CommonTokenStream(lexer);
//语法树
BeetlParser parser = new BeetlParser(tokens);
// 测试代码
ProgContext tree = parser.prog();
// begin parsing at init rule
AntlrProgramBuilder pb = new AntlrProgramBuilder(gt);
ProgramMetaData data = pb.build(tree);
?判断ASTNode的节点类型
if (node instanceof TinyTemplateParser.BlockContext) {
TinyTemplateParser.BlockContext bc = (TinyTemplateParser.BlockContext) node;
int count = bc.getChildCount();
for (int i = 0; i < count; i++) {
String str = bc.getChild(i).getText();
int position = 0;
if (!this.gt.getConf().directByteOutput) {
StaticTextASTNode textNode = new StaticTextASTNode(
position, null);
return textNode;
} else {
StaticTextByteASTNode textNode = new StaticTextByteASTNode(
position, null);
return textNode;
}
}
Statement block = parseBlock(bc.value(), node);
return block;
}
?本案例中采用最简单的模板,里面就是读取变量,没有用到自定义宏
那么如何不通过classloader,而是直接write出来呢?
ctx.byteWriter.write((char[]) ctx.staticTextArray[textIndex]);
?
?
原文:http://cywhoyi.iteye.com/blog/2225792