Use Compiled Java Codes"

From Documentation
m
Line 1: Line 1:
{{ZKDevelopersGuidePageHeader}}
+
{{ZKDevelopersReferencePageHeader}}
  
 
__TOC__
 
__TOC__
 
+
= Not to Use zscript for Better Performance =
=== Not to Use zscript for Better Performance ===
 
  
 
It is convenient to use zscript in ZUML, but it comes with a price: slower performance. The degradation varies from one application from another. For large website, it is suggested not to use zscript for better performance. It can be done easily by use of ZK MVC (by implementing a composer to register event listeners and so on).
 
It is convenient to use zscript in ZUML, but it comes with a price: slower performance. The degradation varies from one application from another. For large website, it is suggested not to use zscript for better performance. It can be done easily by use of ZK MVC (by implementing a composer to register event listeners and so on).
  
<source lang="xml">
+
<syntax lang="xml">
 
  <window apply="foo.MyComposer">
 
  <window apply="foo.MyComposer">
 
//omitted
 
//omitted
</source>
+
</syntax>
  
 
Then, you can handle all events in <code>foo.MyComposer</code>. By use of [[ZK Component Reference/Common Operations/Event Handling/Event Listening#Composer_and_Event_Listener_Autowiring | auto-wiring]], the handling of events is straightforward.
 
Then, you can handle all events in <code>foo.MyComposer</code>. By use of [[ZK Component Reference/Common Operations/Event Handling/Event Listening#Composer_and_Event_Listener_Autowiring | auto-wiring]], the handling of events is straightforward.
  
==== Event Handler Is Zscript ====
+
== Event Handler Is Zscript ==
  
 
In additions to <tt>zscript</tt>, [[ZK Component Reference/Common Operations/Event Handling/Event Listening#Declare_an_Event_Handler_in_a_ZUL_page | the event handler declared in a ZUL page] is also interpreted at the runtime. For example,
 
In additions to <tt>zscript</tt>, [[ZK Component Reference/Common Operations/Event Handling/Event Listening#Declare_an_Event_Handler_in_a_ZUL_page | the event handler declared in a ZUL page] is also interpreted at the runtime. For example,
  
<source lang="xml">
+
<syntax lang="xml">
 
   <button label="OK" onClick="doSomething()"/>
 
   <button label="OK" onClick="doSomething()"/>
</source>
+
</syntax>
  
 
where <tt>doSomething()</tt> is interpreted as <tt>zscript</tt>. Thus, for better performance, they shall be replaced too.
 
where <tt>doSomething()</tt> is interpreted as <tt>zscript</tt>. Thus, for better performance, they shall be replaced too.
  
=== Use the deferred Attribute ===
+
= Use the deferred Attribute =
 
If you still need to write zscript codes, you can specify the <tt>deferred</tt> attribute to defer the evaluation of zscript codes as follows.
 
If you still need to write zscript codes, you can specify the <tt>deferred</tt> attribute to defer the evaluation of zscript codes as follows.
  
<source lang="xml" >
+
<syntax lang="xml" >
 
<zscript deferred="true">
 
<zscript deferred="true">
 
</zscript>
 
</zscript>
</source>
+
</syntax>
  
 
By specifying the <tt>deferred</tt> attribute, the zscript codes it contains will not be evaluated when ZK renders a page. It means the interpreter won't be loaded when ZK renders a page. It saves memory and speeds up the page rendering.
 
By specifying the <tt>deferred</tt> attribute, the zscript codes it contains will not be evaluated when ZK renders a page. It means the interpreter won't be loaded when ZK renders a page. It saves memory and speeds up the page rendering.
Line 36: Line 35:
 
In the following example, the interpreter is loaded only when the button is clicked:
 
In the following example, the interpreter is loaded only when the button is clicked:
  
<source lang="xml" >
+
<syntax lang="xml" >
 
<window id="w">
 
<window id="w">
 
     <zscript deferred="true">
 
     <zscript deferred="true">
Line 45: Line 44:
 
     <button label="Add" onClick="addMore()"/>
 
     <button label="Add" onClick="addMore()"/>
 
</window>
 
</window>
</source>
+
</syntax>
  
==== The deferred Attribute and the onCreate Event ====
+
== The deferred Attribute and the onCreate Event ==
 
It is worth to notice that, if the <tt>onCreate</tt> event listener is written in zscript, the deferred option mentioned in the previous second becomes ''useless''. It is because the <tt>onCreate</tt> event is sent when the page is loaded. In other words, all deferred zscript will be evaluated when the page is loaded if the <tt>onCreate</tt> event listener is written in zscript as shown below.
 
It is worth to notice that, if the <tt>onCreate</tt> event listener is written in zscript, the deferred option mentioned in the previous second becomes ''useless''. It is because the <tt>onCreate</tt> event is sent when the page is loaded. In other words, all deferred zscript will be evaluated when the page is loaded if the <tt>onCreate</tt> event listener is written in zscript as shown below.
  
<source lang="xml" >
+
<syntax lang="xml" >
 
<window onCreate="init()">
 
<window onCreate="init()">
</source>
+
</syntax>
  
 
Rather, it is better to rewrite it as
 
Rather, it is better to rewrite it as
  
<source lang="xml" >
+
<syntax lang="xml" >
 
<window use="my.MyWindow">
 
<window use="my.MyWindow">
</source>
+
</syntax>
  
 
Then, prepare <tt>MyWindow.java</tt> as shown below.
 
Then, prepare <tt>MyWindow.java</tt> as shown below.
  
<source lang="java" >
+
<syntax lang="java" >
 
  package my;
 
  package my;
 
  public class MyWindow extends Window {
 
  public class MyWindow extends Window {
 
     public void onCreate() { //to process the onCreate event
 
     public void onCreate() { //to process the onCreate event
 
  ...
 
  ...
</source>
+
</syntax>
 
   
 
   
 
If you prefer to do the initialization right after the component (and all its children) is created, you can implement the <javadoc type="interface">org.zkoss.zk.ui.ext.AfterCompose</javadoc> interface as shown below. Note: the <tt>afterCompose</tt> method of the <tt>AfterCompose</tt> interface is evaluated at the Component Creation phase, while the <tt>onCreate</tt> event is evaluated in the Event Processing Phase.
 
If you prefer to do the initialization right after the component (and all its children) is created, you can implement the <javadoc type="interface">org.zkoss.zk.ui.ext.AfterCompose</javadoc> interface as shown below. Note: the <tt>afterCompose</tt> method of the <tt>AfterCompose</tt> interface is evaluated at the Component Creation phase, while the <tt>onCreate</tt> event is evaluated in the Event Processing Phase.
  
<source lang="java" >
+
<syntax lang="java" >
 
  package my;
 
  package my;
 
  public class MyWindow extends Window implements org.zkoss.zk.ui.ext.AfterCompose {
 
  public class MyWindow extends Window implements org.zkoss.zk.ui.ext.AfterCompose {
 
     public void afterCompose() { //to initialize the window
 
     public void afterCompose() { //to initialize the window
 
  ...
 
  ...
</source>
+
</syntax>
  
=== Use the forward Attribute ===
+
= Use the forward Attribute =
 
To simplify the event flow, ZK components usually send the events to the component itself, rather than the parent or other targets. For example, when an user clicks a button, the <tt>onClick</tt> event is sent to the button. Developers usually forward the event to the window by use of the <tt>onClick</tt> event listener as follows.
 
To simplify the event flow, ZK components usually send the events to the component itself, rather than the parent or other targets. For example, when an user clicks a button, the <tt>onClick</tt> event is sent to the button. Developers usually forward the event to the window by use of the <tt>onClick</tt> event listener as follows.
  
<source lang="xml" >
+
<syntax lang="xml" >
 
<window id="w">
 
<window id="w">
 
     <button label="OK" onClick="w.onOK"/>
 
     <button label="OK" onClick="w.onOK"/>
</source>
+
</syntax>
 
 
 
As suggested in the previous sections, the performance can be improved by ''not'' using zscript at all. Thus, you can rewrite the above code snippet either with <tt>EventListener</tt> or by specifying the <tt>forward</tt> attribute as follows.
 
As suggested in the previous sections, the performance can be improved by ''not'' using zscript at all. Thus, you can rewrite the above code snippet either with <tt>EventListener</tt> or by specifying the <tt>forward</tt> attribute as follows.
  
<source lang="xml" >
+
<syntax lang="xml" >
 
<window>
 
<window>
 
     <button label="OK" forward="onOK"/>
 
     <button label="OK" forward="onOK"/>
</source>
+
</syntax>
  
=== GenericForwardComposer and GenericAutowireComposer ===
+
= GenericForwardComposer and GenericAutowireComposer =
  
 
If <tt>zscript</tt> is ever used, the autowiring of <javadoc>org.zkoss.zk.ui.util.GenericForwardComposer</javadoc> and <javadoc>org.zkoss.zk.ui.util.GenericAutowireComposer</javadoc> might be slow because it has to look for the variables defined in <tt>zscript</tt>.
 
If <tt>zscript</tt> is ever used, the autowiring of <javadoc>org.zkoss.zk.ui.util.GenericForwardComposer</javadoc> and <javadoc>org.zkoss.zk.ui.util.GenericAutowireComposer</javadoc> might be slow because it has to look for the variables defined in <tt>zscript</tt>.
Line 99: Line 98:
 
Unless you really declare a data member in your composer that shall be wired to a <tt>zscript</tt> variable, it is safe to turn the wiring of <tt>zscript</tt> variables off. It can be done by passing false to the second and third arguments of <javadoc method="GenericAutowireComposer(char, boolean, boolean)">org.zkoss.zk.ui.util.GenericAutowireComposer</javadoc>. For example,
 
Unless you really declare a data member in your composer that shall be wired to a <tt>zscript</tt> variable, it is safe to turn the wiring of <tt>zscript</tt> variables off. It can be done by passing false to the second and third arguments of <javadoc method="GenericAutowireComposer(char, boolean, boolean)">org.zkoss.zk.ui.util.GenericAutowireComposer</javadoc>. For example,
  
<source lang="java">
+
<syntax lang="java">
 
public class MyComposer extends GenericForwardComposer {
 
public class MyComposer extends GenericForwardComposer {
 
     public MyComposer() {
 
     public MyComposer() {
Line 106: Line 105:
 
     }
 
     }
 
}
 
}
</source>
+
</syntax>
  
 
<blockquote>
 
<blockquote>
Line 113: Line 112:
 
</blockquote>
 
</blockquote>
  
=== See Also ===
+
=Version History=
From ZK forum:
+
 
*[http://www.zkoss.org/forum/listComment/12593 GenericForwardComposer slow when used with ZScript ]
+
{| border='1px' | width="100%"
*[http://www.zkoss.org/forum/listComment/6251/ ZK App run slow in a Citrix like environment]
+
! Version !! Date !! Content
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D6255%3BcategoryId%3D14%3B Slow page]
+
|-
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D5208%3BcategoryId%3D14%3B slow performance when dynamic paging]
+
| &nbsp;
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D6208%3BcategoryId%3D14%3B Best practice and performance for menu tree | zul vs. java]
+
| &nbsp;
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D5934%3BcategoryId%3D14%3B zkmax - performance enhancement version of ZUL - what does it mean?]
+
| &nbsp;
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D5270%3BcategoryId%3D14%3B How to testify zk's performance in big project? ]
+
|}
  
{{ ZKDevelopersGuidePageFooter}}
+
{{ ZKDevelopersReferencePageFooter}}

Revision as of 11:22, 17 September 2010


Use Compiled Java Codes


Not to Use zscript for Better Performance

It is convenient to use zscript in ZUML, but it comes with a price: slower performance. The degradation varies from one application from another. For large website, it is suggested not to use zscript for better performance. It can be done easily by use of ZK MVC (by implementing a composer to register event listeners and so on).

<syntax lang="xml">

<window apply="foo.MyComposer">

//omitted </syntax>

Then, you can handle all events in foo.MyComposer. By use of auto-wiring, the handling of events is straightforward.

Event Handler Is Zscript

In additions to zscript, [[ZK Component Reference/Common Operations/Event Handling/Event Listening#Declare_an_Event_Handler_in_a_ZUL_page | the event handler declared in a ZUL page] is also interpreted at the runtime. For example,

<syntax lang="xml">

 <button label="OK" onClick="doSomething()"/>

</syntax>

where doSomething() is interpreted as zscript. Thus, for better performance, they shall be replaced too.

Use the deferred Attribute

If you still need to write zscript codes, you can specify the deferred attribute to defer the evaluation of zscript codes as follows.

<syntax lang="xml" > <zscript deferred="true"> </zscript> </syntax>

By specifying the deferred attribute, the zscript codes it contains will not be evaluated when ZK renders a page. It means the interpreter won't be loaded when ZK renders a page. It saves memory and speeds up the page rendering.

In the following example, the interpreter is loaded only when the button is clicked:

<syntax lang="xml" > <window id="w">

   <zscript deferred="true">
    void addMore() {
        new Label("More").setParent(w);
    }
   </zscript>
   <button label="Add" onClick="addMore()"/>

</window> </syntax>

The deferred Attribute and the onCreate Event

It is worth to notice that, if the onCreate event listener is written in zscript, the deferred option mentioned in the previous second becomes useless. It is because the onCreate event is sent when the page is loaded. In other words, all deferred zscript will be evaluated when the page is loaded if the onCreate event listener is written in zscript as shown below.

<syntax lang="xml" > <window onCreate="init()"> </syntax>

Rather, it is better to rewrite it as

<syntax lang="xml" > <window use="my.MyWindow"> </syntax>

Then, prepare MyWindow.java as shown below.

<syntax lang="java" >

package my;
public class MyWindow extends Window {
    public void onCreate() { //to process the onCreate event
...

</syntax>

If you prefer to do the initialization right after the component (and all its children) is created, you can implement the AfterCompose interface as shown below. Note: the afterCompose method of the AfterCompose interface is evaluated at the Component Creation phase, while the onCreate event is evaluated in the Event Processing Phase.

<syntax lang="java" >

package my;
public class MyWindow extends Window implements org.zkoss.zk.ui.ext.AfterCompose {
    public void afterCompose() { //to initialize the window
...

</syntax>

Use the forward Attribute

To simplify the event flow, ZK components usually send the events to the component itself, rather than the parent or other targets. For example, when an user clicks a button, the onClick event is sent to the button. Developers usually forward the event to the window by use of the onClick event listener as follows.

<syntax lang="xml" > <window id="w">

   <button label="OK" onClick="w.onOK"/>

</syntax>

As suggested in the previous sections, the performance can be improved by not using zscript at all. Thus, you can rewrite the above code snippet either with EventListener or by specifying the forward attribute as follows.

<syntax lang="xml" > <window>

   <button label="OK" forward="onOK"/>

</syntax>

GenericForwardComposer and GenericAutowireComposer

If zscript is ever used, the autowiring of GenericForwardComposer and GenericAutowireComposer might be slow because it has to look for the variables defined in zscript.

Unless you really declare a data member in your composer that shall be wired to a zscript variable, it is safe to turn the wiring of zscript variables off. It can be done by passing false to the second and third arguments of GenericAutowireComposer.GenericAutowireComposer(char, boolean, boolean). For example,

<syntax lang="java"> public class MyComposer extends GenericForwardComposer {

   public MyComposer() {
       super('$', false, false);
       //omitted
   }

} </syntax>


Notice that, since it is rarely needed to look for a zscript variable, the default constructor (GenericAutowireComposer.GenericAutowireComposer()) assumes false in ZK 5.5. However, they are default to true in ZK 5.0 for better backward compatibility.

Version History

Version Date Content
     



Last Update : 2010/09/17

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.