Japid icon indicating copy to clipboard operation
Japid copied to clipboard

[Optimization] - Reuse of template instance within the same calling context

Open tunggad opened this issue 13 years ago • 1 comments

I see in the generated java codes that everywhere/everytime when a template is invoked, then a new instance of it is created, even when the same template is used twice within the same calling method (calling template). The following codes show it:

`set rightCol

        `String myName = new String("tunggad");`
        `Integer myNr = 80;`
<div>
    <div id="editor"></div>
    <div>
      `tag myTemplate (arg2=29, arg1="tunggad", arg3=null)
    </div>
    `tag templateWithBody ("kkkk", myName, myNr) | String name, Integer nr
      This is the body
      `tag myTemplate (arg1=name, arg3=true, arg2=nr)`
      `tag noParamsTemplate`
    `
</div>
`

  @Override protected void rightCol() {
final myTemplate _myTemplate4 = new myTemplate(getOut());
{ _myTemplate4.setActionRunners(getActionRunners()); }

final templateWithBody _templateWithBody5 = new templateWithBody(getOut());
{ _templateWithBody5.setActionRunners(getActionRunners()); }

final myTemplate _myTemplate6 = new myTemplate(getOut());
{ _myTemplate6.setActionRunners(getActionRunners()); }

final noParamsTemplate _noParamsTemplate7 = new noParamsTemplate(getOut());
{ _noParamsTemplate7.setActionRunners(getActionRunners()); }

    // line 8
            String myName = new String("tunggad");// line 10
            Integer myNr = 80;// line 11
p("    <div>\n" + 
"        <div id=\"editor\"></div>\n" + 
"        <div>\n" + 
"          ");// line 11
_myTemplate4.render(named("arg2", 29), named("arg1", "tunggad"), named("arg3", null));
// line 15
p("        </div>\n" + 
"        ");// line 15
_templateWithBody5.render("kkkk", myName, myNr, new templateWithBody.DoBody<String, Integer>(){
public void render(final String name, final Integer nr) {
// line 17
p("          This is the body\n" + 
"          ");// line 17
_myTemplate6.render(named("arg1", name), named("arg3", true), named("arg2", nr));
// line 19
          _noParamsTemplate7.render();
// line 20

}
}
);
// line 17
p("    </div>\n" + 
"    ");// line 21
;
  }

Actually, the tow instances _myTemplate4 and _myTemplate6 could/should be reused?

BTW: Im just thinking abit futher, we see very often, that some representation logics actually only do READ-access to the passed paremater, and it would be a futher enorm optimization if we had something like STATIC TEMPLATE. They dont have own states, --> should be Thread-safe. They only have a static render method, where one can pass the parameter into, just like a static utils/service method. So we can reuse such static templates globally --> save cpu cycle for instances creation, save ram. What do you think?

tunggad avatar Jun 15 '11 10:06 tunggad

Yes, there is room for surer for optimization. Given my tight schedule, they're not on my immediate plan.

The specific case you mentioned, it's not that bad. I only create an instance for each piece of code invoking the tag, not for each invocation. For example, a tag invoked in a for loop uses the same instance, no matter how many iterations of the loop.

branaway avatar Jun 15 '11 10:06 branaway