Beetl1.2M1将模板的文本先以Byte方式输出,性能又大幅提高(更新) beetl freemarker
程序员文章站
2022-06-06 15:01:38
...
beetl是一个语法类似javascript的模板语言,可以用于代码生成和Web开发,易用性和性能非常好,功能也很全 ,如下是在性能上最新优化成功
Beetl1.2M1字节优化成果
还是以以前的模板为准(大小6K),循环渲染50000次,需要时间(毫秒为单位)如下
Beetl1.2M1 解释执行方式(普通模式),运行三次,分别 1356,1365,1348
Beetl1.2M1 编译执行方式(普通模式),运行三次,分别 913,922,905
Freemarker 分别是 1155,1130,1122
Beetl1.2M1 允许字节流优化,解释方式执行三次,分别是 587,605,610
Beetl1.2M1 允许字节流优化,编译方式执行三次,分别是 385,355,370
如下式一个采用节流优化,编译方式执行 的class代码
行17 输出V0,此处借鉴了Freemarker合并多行,一次性输出的功能,此优化对性能影响也很大
行18-32 是对includeFileTemplate 标签的处理,类似JSP的include
行39-42 是将模板中的文本预先转化成字节数组
行48 是行映射,也就是在渲染出错的时候,可以将java报错的行数与模板文件行数对应起来,以方便的查看错误原因,如19=3 表示java代码19行如果抛错,则对应的是模板中的第三行错误
package ext; import java.util.*; import java.util.Map.Entry; import java.math.*; import java.io.*; import org.bee.tl.core.*; import org.bee.tl.core.io.*; import org.bee.tl.core.compile.*; /* /ext/master_template.html is generated by beetl on 2012-06-22 10:42:38 */ public class master_template_html extends CompiledClass{ public void service(ByteWriter writer,Context ctx) throws IOException,BeeException{ ctx.set("__group",group); ctx.set("__this",template); CompileTemplateWriter out = new CompileTemplateWriter(writer,this); ctx.set("__pw",out); try{ out.write(__V0); ByteSupportTag includeFileTemplate_18_19 = (ByteSupportTag)this.getTag("includeFileTemplate"); includeFileTemplate_18_19.setParas(new Object[]{"/ext/child_template.html"}); includeFileTemplate_18_19.setContext(ctx.getNewContext()); if(includeFileTemplate_18_19.requriedInput()){ out= out.getTempWriter(); ctx.set("__pw",out); { out.write(__V1); } //还原输出流 includeFileTemplate_18_19.setByteInput(out.getTempByte()); out = out.getParent(); ctx.set("__pw",out); } out.write(includeFileTemplate_18_19.getOutputAsByte()); out.write(__V2); }catch(Exception ex){ throw getException(ex,lineMap); } out.flush(); } private static final byte[] __V1 = new byte[]{0x68,0x65,0x6c,0x6c,0x6f,0x2c,0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,0x74,0x65,0x6d,0x70,0x20,0x63,0x68,0x69,0x6c,0x64,0xd,0xa}; private static final byte[] __V0 = new byte[]{0xffffffd5,0xffffffe2,0xffffffca,0xffffffc7,0x6d,0x61,0x73,0x74,0x65,0x72,0xffffffd2,0xffffffb3,0xffffffc3,0xffffffe6,0xd,0xa,0xffffffcf,0xffffffd6,0xffffffd4,0xffffffda,0xffffffcf,0xffffffd4,0xffffffca,0xffffffbe,0x63,0x68,0x69,0x6c,0x64,0xffffffd2,0xffffffb3,0xffffffc3,0xffffffe6,0xffffffd4,0xffffffda,0xffffffcf,0xffffffc2,0xffffffc3,0xffffffe6,0xd,0xa}; private static final byte[] __V2 = new byte[]{0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0x3d,0xd,0xa,0xffffffcf,0xffffffd6,0xffffffd4,0xffffffda,0xffffffca,0xffffffc7,0x6d,0x61,0x73,0x74,0x65,0x72,0xffffffd2,0xffffffb3,0xffffffc3,0xffffffe6}; private static final byte[] __VCR = new byte[]{0xd,0xa}; /* 原模板Cached标示*/ public long getVersion(){ return 1337956717968l; } /* 行映射*/ protected String lineMap = "-32=3-19=3-21=3-"; }
可见,字节流优化对性能影响很大,最快方式是beetl编译方式,且允许字节流优化。超过了Freemarker4倍。
对于使用者来说,只需俩行命令就可以了
group.enableOptimize(); //编译成class运行
group.enableDirectOutputByte(); //使用字节流输出