Looking for a library to help me I chose ASM, because it
- is small with no dependencies
- has been released lately
- has decent documentation
- has a tool (Eclipse plugin) that generates ASM API calls from existing byte code
Especially no 4 inspired me to do the following:
- Create a fistful of hand crafted Java classes that do the same as some selected templates
- Create ASM code that generates the same byte code as those Java classes by the tool described in no 4
- Create a recursive descent compiler that calls the generalized ASM code derived from step 2 that generates the desired byte code
- Load and cache the classes at runtime
It turned out to be a pretty viable approach and allowed me to finish my task without too much knowledge of Java byte code. And what I had to learn anyway was quite enlightening :)
Even though a lot of code remains to be dynamic, because I do not know what will be in the model at run time, the caliper micro benchmarks comparing uncompiled reference code against the compiled version shows a 2.5x - 10x speedup:
benchmark ns logarithmic runtime
SimpleExpressionReference 3367 ================
SimpleExpressionCompiled 963 ==
ComplexExpressionReference 6721 =======================
ComplexExpressionCompiled 1331 ======
If 7067 =======================
IfCompiled 805 =
Foreach 12796 =============================
ForeachCompiled 5275 ====================
1 comment:
JAXB used an approach where they'd actually ship a class template and "pipe" it through ASM, replacing the field/class/whatever identifiers on the way.
Post a Comment