Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tunggad opened this issue Jun 15, 2011 · 1 comment

Comments

@tunggad
Copy link

tunggad commented Jun 15, 2011

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?

@branaway
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants