You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptimizingSluggishUI.asciidoc 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ---
  2. title: Optimizing Sluggish UI
  3. order: 13
  4. layout: page
  5. ---
  6. [[optimizing-sluggish-ui]]
  7. = Optimizing sluggish UI
  8. Is your Vaadin application becoming sluggish? Yes, this can happen - it
  9. is no secret. This can happen for every application, with every
  10. programming language, with every UI library and with all hardware
  11. platforms. Make it a web application and it is not even hard. For end
  12. users this is not acceptable, especially when building applications for
  13. frequent use.
  14. All developers have heard the phrase _premature optimization is the root
  15. of all evil_, coined by software guru
  16. http://www.google.com/search?&rls=en&q=premature+optimization+is+the+root+of+all+evil&ie=UTF-8&oe=UTF-8[Donald
  17. Knuth]. There is a wisdom in that clause. Still I want to motivate you
  18. (Vaadin developer) to read this article, even if you currently have no
  19. performance issues. I'd say it is not that bad to know what will
  20. inevitably make your application slow. You might subconsciously avoid
  21. the worst pitfalls (but still not be subjected to premature
  22. optimization) and avoid optimization task totally.
  23. Resolving performance issues in Vaadin-based applications may be a bit
  24. tricky in some situations. Performance issues are one of the most common
  25. issues why project managers in IT Mill come and disturb our "peace" in
  26. the RnD team. Usually we'll end up modifying the application, not
  27. Vaadin. Vaadin abstracts away the browser environment, and the
  28. abstraction may make it hard to figure out what is the actual cause for
  29. a slow UI.
  30. The first step is to detect whether to optimize the server side or the
  31. client side. You can use all standard profiling tools with Vaadin apps
  32. like Firebug for the client side and JProfiler for the server side. For
  33. a quick look for what is taking so long it is easy to use "?debug" query
  34. parameter in application. It will show you a small floating console in
  35. the browser. Inspecting messages there, one can see server visit time
  36. (includes both network latency and server processing time) and the
  37. actual time spent handling the response in client.
  38. If the problem is on server side, it is most commonly in the back-end
  39. system or how it is connected to Vaadin components. The server side code
  40. of Vaadin is pretty well optimized by the JVM. If the server side is
  41. your problem, I'd bet you will end up optimizing SQL queries. Optimizing
  42. tricks for server side are very similar to any other Java application.
  43. If it is the client side processing that takes a long time, optimizing
  44. methods are more Vaadin specific. There are several tricks one can
  45. perform to optimize the client side processing time. Some of them are
  46. more or less generic to ajax applications in common, others are purely
  47. Vaadin specific tricks. If you belong to the large group of Java
  48. developers who hate browser programming, you don't need to get worried
  49. at this point. Although the processing time is long on client, you will
  50. be mostly modifying the pure server side Java code when optimizing your
  51. application.
  52. [[best-tricks-to-makekeep-your-vaadin-apps-ui-responsive]]
  53. Best tricks to make/keep your Vaadin apps UI responsive
  54. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. [[render-less-components]]
  56. #1 Render less components
  57. ^^^^^^^^^^^^^^^^^^^^^^^^^
  58. The most common cause for slow rendering time is that you are rendering
  59. a lot of components.It's easy to forget that the browser is always the
  60. browser, and too much complicated stuff will cause slowness. The
  61. JavaScript engines have been optimized a lot lately, but the actual
  62. rendering is still a bottleneck.
  63. * consider if you can use one component instead of many (eg. instead of
  64. using one label per line, use a label with multiple lines using html :
  65. label.setContentMode(Label.CONTENT_XHTML) )
  66. * hide rarely used features (this also improves usability) when possible
  67. [[try-to-keep-component-tree-simple-flat]]
  68. #2 Try to keep component tree simple (flat)
  69. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  70. When component tree (the hierarchy of components, not the 'Tree'
  71. component) gets deeper (more components inside others) the rendering
  72. task gets heavier for browsers. As the growth is more than linear, I
  73. have split this from previous hint. To really show you the importance of
  74. this tip, I made a small example application. Try it at:
  75. http://uilder.virtuallypreinstalled.com/run/deepcomponenttrees/?restartApplication&debug
  76.  Tips for keeping the component tree simple:
  77. * Avoid for example using vertical layouts inside another vertical
  78. layouts when possible
  79. * Do you need to extend CustomComponent in your server side composition
  80. or could you just extend some layout? This will result having one
  81. component less in the component tree. You might sometimes be arguing
  82. against this because of architectural reasons (CustomComponent has a
  83. fewer methods than VerticalLayout), but on the other hand Java has
  84. interfaces to deal the issue in a cleaner manner.
  85. * Maybe you have an  extra layout inside your Panel or Window (see
  86. setContent method)? This is a common pitfall for Vaadin newcomers.
  87. [[use-the-right-component]]
  88. #3 Use the right component
  89. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  90. Some components in Vaadin are just faster to render than others. For
  91. example our standard VerticalLayout and HorizontaLayout have a huge
  92. feature set supporting for example spacing, margin, alignments and
  93. expand ratios. Supporting all these comes with a price of performance
  94. hit. Rendering a lot of simple components into CssLayout (which does not
  95. support all those features), is often several times faster than into the
  96. default layouts.
  97. So favor simpler components in your application if you don't need all
  98. those features. This will be essential in your frequently recycled
  99. server side compositions. So consider if you could use:
  100. * Vertical/HorizontalLayout instead of GridLayout.
  101. * single GridLayout instead of multiple nested
  102. Vertical/HorizontalLayouts.
  103. * CssLayout (available in standard distribution since 6.1) instead of
  104. full featured HorizontalLayout.
  105. * GridLayout (or even FastGrid from FastLayouts incubator project)
  106. instead of Table. Table is meant for displaying tabular data, GridLayout
  107. is meant for laying out components.
  108. In some extreme cases it may be a viable option to build optimized
  109. client side component instead of using pure server side composition. It
  110. is not the easiest path to take as you need to work in browser
  111. environment too, but you then have a full control of what is happening.
  112. With custom client side component one can also more easily optimize also
  113. the data transferred between client and server. Refer to manual for more
  114. information.
  115. [[use-table-efficiently]]
  116. #4 Use Table efficiently
  117. ^^^^^^^^^^^^^^^^^^^^^^^^
  118. Table is one of the most optimized component in Vaadin, both server and
  119. client side. Still it is very easy to put both client and server on its
  120. knees with it. Common things to check if you have performance issues
  121. with Table:
  122. * make sure the container used in table loads data lazily from back-end
  123. if you have huge amounts of data
  124. * using the editable mode or ColumnGenerator can make a huge amount of
  125. components to be rendered on client. Especially if table size is
  126. maximized. Consider using lighter components in Table (like putting
  127. complex property editor into PopupView instead of straight to table
  128. cell)
  129. * Don't overuse layouts in table, use CssLayout instead of others when
  130. possible
  131. * Use lazy-loading of rows, don't render all rows at once
  132. * Minimize caching with setCacheRate function, if you have heavy table
  133. body (like large editable table with several columns).
  134. [[avoid-complex-custom-layouts]]
  135. #5 Avoid complex custom layouts
  136. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  137. With CustomLayouts it is common to use html tables to build complex
  138. layout. Using html tables as a layout has several drawbacks like heavy
  139. rendering and browser differences. Rendering Vaadin components into a
  140. complex table based dom structure may be much slower than into a simple
  141. div based layout. The core reason for this optimization is the same as
  142. in trick #2 : the rendering is more more expensive in complex dom
  143. structures.
  144. [[use-a-light-theme]]
  145. #6 Use a light theme
  146. ^^^^^^^^^^^^^^^^^^^^
  147. The effect of theme may be radical in some cases. Believe it or not, but
  148. I have seen cases where rendering time triples just by using a different
  149. themes. Heavy theme combined with a deep component tree is something
  150. that will really test the speed of browsers rendering engine. For
  151. optimizing theme you can google some generic instructions.  Minify,
  152. gzip, use simple (and fewer) selectors, optimize images, use
  153. transparency moderately.
  154.  Sizing components can be done in both server side Java code and in
  155. themes css. Both approaches have some good and bad features. Don't use
  156. both methods at the same time for the same component, it may render
  157. improperly and add an extra performance hit.
  158. [[use-generic-component-features-moderately]]
  159. #7 Use generic component features moderately
  160. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  161. All Vaadin components have captions, icons and error indicators. All of
  162. them adds extra burden to client side rendering engine, just like extra
  163. components. As captions, icons and errors are also packed with
  164. surprisingly wide set of features (see ticket
  165. http://dev.vaadin.com/ticket/1710[#1710] in trac), in some cases it may
  166. even be faster to use extra Label or Embedded instead of them.