branaway / Japid

A Java-based statically-typed fast template engine that can be used in any Java code. It has special adapter for use with the Play! Framework.
113 stars 18 forks source link

[optimization] - dynamically tracking of actual required size of each template to set optimal init-capacity of StringBuilder #21

Open tunggad opened 13 years ago

tunggad commented 13 years ago

Today i came across this blog http://kaioa.com/node/59, ther author was talking about the performance impact of setting init-capacity for StringBuilder. I quote here the important observation of the author:

"If you take a closer look you can also see that there is some kind of rhythm: the best initial capacities (local optimum) are always a power of two. And the worst results are always just before the next power of two. The perfect results are of course achieved if the required size is used from the very beginning (shown as dashed lines in the diagram) and no resizing happens at all."

And i come to the idea how we can optimize the performance of Japid abit futher. Now we hardcode init-capacity of StringBuilder in constructor of JapidTemplateBaseWithoutPlay. My idea is to have a global tracking mechanism of the actual required size of each template and after a warmup the JapidTemplateBaseWithoutPlay constructor will lookup the ideal init-capacity for each template here.

What do you think?

branaway commented 13 years ago

smart idea indeed. I had another idea before: each template class holds on to an instance of StringBuilder and each invocation of the same template reuses the same StringBuilder, so eventually the StringBuilder will have a capacity of the largest size it has ever served. There is no smart algorithm required. Of course I need have some mechanism to protect it from some extreme cases.

tunggad commented 13 years ago

Oh your idea, i think is still smarter. Very importan, no need of such a automagical algorithm for dynamic tracking, simple to implement -> more stable. Ok, why do you still hesitate to implement it? ^^