Use Compiled Java Codes"

From Documentation
m (remove empty version history (via JWB))
 
(42 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{ZKDevelopersGuidePageHeader}}
+
{{ZKDevelopersReferencePageHeader}}
  
 
__TOC__
 
__TOC__
 +
= 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 if possible. Furthermore, GenericForwardComposer and GenericAutowireComposer may be very slow when mixing with zscript, see the link in the [[#See Also]] section.
+
It is convenient to use zscript in ZUML, but it comes with a price: slower performance. The degradation varies from one application to another. It is suggested to use zscript only for fast prototyping, POC, or small projects. For large websites, it is suggested to use [[ZK Developer's Reference/MVC|ZK MVC]]/[http://books.zkoss.org/zk-mvvm-book/8.0/index.html ZK MVVM] instead. For example, with ZK MVC
  
=== Use the deferred Attribute ===
+
<syntaxhighlight line lang="xml">
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.
+
<window apply="foo.MyComposer">
 +
//omitted
 +
</syntaxhighlight>
  
<source lang="xml" >
+
You can handle all events and components in <code>foo.MyComposer</code>. By the use of [[ZK Component Reference/Common Operations/Event Handling/Event Listening#Composer_and_Event_Listener_Autowiring | auto-wiring]], it is straightforward to handle events and components.
 +
 
 +
== Event Handler Is zscript ==
 +
 
 +
In addition to the <code>zscript</code> element, [[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,
 +
 
 +
<syntaxhighlight line lang="xml">
 +
  <button label="OK" onClick="doSomething()"/>
 +
</syntaxhighlight>
 +
 
 +
where <code>doSomething()</code> is interpreted as <code>zscript</code>. Thus, for better performance, they should be replaced too.
 +
 
 +
==Turn off the use of zscript==
 +
{{versionSince|5.0.8}}
 +
 
 +
If you decide not to use zscript at all, you could turn on [[ZK Configuration Reference/zk.xml/The system-config Element/The disable-zscript Element|the disable-script configuration]] as follows, such that an exception will be thrown if zscript is used.
 +
 
 +
<source lang="xml">
 +
<system-config>
 +
<disable-zscript>true</disable-zscript>
 +
</system-config>
 +
</source>
 +
 
 +
= Use the deferred Attribute =
 +
If you still need to write zscript codes, you can specify the <code>deferred</code> attribute to defer the evaluation of zscript codes as follows.
 +
 
 +
<syntaxhighlight line lang="xml" >
 
<zscript deferred="true">
 
<zscript deferred="true">
</source>
+
</zscript>
 +
</syntaxhighlight>
  
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 <code>deferred</code> attribute, the zscript codes it contains will not be evaluated when ZK renders a page. It means that the interpreter won't be loaded when ZK renders a page. This saves memory and speeds up page rendering.
  
 
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" >
+
<syntaxhighlight line lang="xml" >
 
<window id="w">
 
<window id="w">
 
     <zscript deferred="true">
 
     <zscript deferred="true">
Line 25: Line 55:
 
     <button label="Add" onClick="addMore()"/>
 
     <button label="Add" onClick="addMore()"/>
 
</window>
 
</window>
</source>
+
</syntaxhighlight>
  
=== 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 <code>onCreate</code> event listener is written in zscript, the deferred option mentioned in the previous section becomes ''useless''. It is because the <code>onCreate</code> event is sent when the page is loaded. In other words, all deferred zscript will be evaluated when the page is loaded if the <code>onCreate</code> event listener is written in zscript as shown below.
  
<source lang="xml" >
+
<syntaxhighlight line lang="xml" >
 
<window onCreate="init()">
 
<window onCreate="init()">
</source>
+
</syntaxhighlight>
  
 
Rather, it is better to rewrite it as
 
Rather, it is better to rewrite it as
  
<source lang="xml" >
+
<syntaxhighlight line lang="xml" >
 
<window use="my.MyWindow">
 
<window use="my.MyWindow">
</source>
+
</syntaxhighlight>
  
Then, prepare <tt>MyWindow.java</tt> as shown below.
+
Then, prepare <code>MyWindow.java</code> as shown below.
  
<source lang="java" >
+
<syntaxhighlight line 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>
+
</syntaxhighlight>
 
   
 
   
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 <code>afterCompose</code> method of the <code>AfterCompose</code> interface is evaluated at the Component Creation phase, while the <code>onCreate</code> event is evaluated in the Event Processing Phase.
  
<source lang="java" >
+
<syntaxhighlight line 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>
+
</syntaxhighlight>
+
 
=== 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 a user clicks a button, the <code>onClick</code> event is sent to the button. However, developers may need to forward the event to the window component by the use of the <code>onClick</code> event listener as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
<window id="w">
+
<window id="w" onOK='alert("on OK")'>
     <button label="OK" onClick="w.onOK"/>
+
     <button label="OK" onClick='Events.postEvent("onOK", w, null)'/>
 +
</window>
 
</source>
 
</source>
+
 
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 <code>EventListener</code> or by specifying the <code>forward</code> attribute as follows.
  
 
<source lang="xml" >
 
<source lang="xml" >
<window>
+
<window apply="foo.MyComposer">
 
     <button label="OK" forward="onOK"/>
 
     <button label="OK" forward="onOK"/>
 +
</window>
 
</source>
 
</source>
  
== See Also ==
 
Discussion of this article: [[Talk:Performance_tip]]
 
 
From ZK smalltalk:
 
*[http://www.zkoss.org/smalltalks/loadondemand/ Use Load-On-Demand to Handle Huge Data]
 
  
From ZK forum:
 
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D6251%3BcategoryId%3D14%3B ZK App run slow in a Citrix like environment]
 
*[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]
 
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D6208%3BcategoryId%3D14%3B Best practice and performance for menu tree | zul vs. java]
 
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D5934%3BcategoryId%3D14%3B zkmax - performance enhancement version of ZUL - what does it mean?]
 
*[http://www.zkoss.org/forum/index.zul#path%3DlistComment%3BdiscussionId%3D5270%3BcategoryId%3D14%3B How to testify zk's performance in big project? ]
 
*[http://www.zkoss.org/forum/listComment/12593 GenericForwardComposer slow when used with ZScript ]
 
  
{{ ZKDevelopersGuidePageFooter}}
+
{{ZKDevelopersReferencePageFooter}}

Latest revision as of 10:24, 5 February 2024


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 to another. It is suggested to use zscript only for fast prototyping, POC, or small projects. For large websites, it is suggested to use ZK MVC/ZK MVVM instead. For example, with ZK MVC

1  <window apply="foo.MyComposer">
2 //omitted

You can handle all events and components in foo.MyComposer. By the use of auto-wiring, it is straightforward to handle events and components.

Event Handler Is zscript

In addition to the zscript element, the event handler declared in a ZUL page is also interpreted at the runtime. For example,

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

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

Turn off the use of zscript

Since 5.0.8

If you decide not to use zscript at all, you could turn on the disable-script configuration as follows, such that an exception will be thrown if zscript is used.

<system-config>
	<disable-zscript>true</disable-zscript>
</system-config>

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.

1 <zscript deferred="true">
2 </zscript>

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

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

1 <window id="w">
2     <zscript deferred="true">
3      void addMore() {
4          new Label("More").setParent(w);
5      }
6     </zscript>
7     <button label="Add" onClick="addMore()"/>
8 </window>

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 section 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.

1 <window onCreate="init()">

Rather, it is better to rewrite it as

1 <window use="my.MyWindow">

Then, prepare MyWindow.java as shown below.

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

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.

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

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 a user clicks a button, the onClick event is sent to the button. However, developers may need to forward the event to the window component by the use of the onClick event listener as follows.

<window id="w" onOK='alert("on OK")'>
    <button label="OK" onClick='Events.postEvent("onOK", w, null)'/>
</window>

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.

<window apply="foo.MyComposer">
    <button label="OK" forward="onOK"/>
</window>




Last Update : 2024/02/05

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