cakephp/cakephp — pull request #3722 by lorenzo (2014-06-16 14:57:11+00:00)
Title: 3.0 optimize string template

I baked some view for a table that contained around 30 fields and I noticed that forms were rendering quite slow. A little profiling showed that StringTemplate was the problem. This set of changes fix the problem by making StringTemplate twice as fast when formatting the same string more than once.

The technique that I used was to pre-compile templates before using them by replacing the placeholders with `sprintf` markers and then using that function for doing the actual replacement.

This is the output of my micro benchmark:

```
Original
=> 1.8427798748016
Compile Each Time
=> 1.8326361179352
Already Compiled
=> 0.90882086753845
```
It shows that in the case of having to compile templates each time (no templates are reused) we have not lost any performance, but most importantly, it shows that when reusing templates the function is twice as fast.

Gotchas:

Any percentage sign inside a template will need to be escaped by the developer or they will get an error from sprintf. I find this to be a fair tradeoff as escaping percentage signs is easy to do, rare to actually come up with a case, and straight forward to document.

Thoughts?