[JSF PERF] do NOT add code in getter methods!

UI Components for JSF
Post Reply
smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

06 Apr 2013, 16:35

Recently, I have been obsessed evaluating the performance of my app, tuning JPA queries, replacing dynamic SQL queries with named queries, and just this morning, I recognized that a getter method was more of a HOT SPOT in Java Visual VM than the rest of my code (or majority of my code).

Getter method: PageNavigationController.getGmapsAutoComplete()
Referenced by ui:include in <h:head> in index.xhtml

Below, you will see that PageNavigationController.getGmapsAutoComplete() is a HOT SPOT (performance issue) in Java Visual VM. If you look further down, on the screen capture, you will see that getLazyModel(), PrimeFaces lazy datatable getter method, is a hot spot too, only when enduser is doing a lot of 'lazy datatable' type of stuff/operations/tasks in the app. :)

Image

See (original) code below.

Code: Select all

    public Boolean getGmapsAutoComplete() {
        switch (page) {
            case "/orders/pf_Add.xhtml":
            case "/orders/pf_Edit.xhtml":
            case "/orders/pf_EditDriverVehicles.xhtml":
                gmapsAutoComplete = true;
                break;
            default:
                gmapsAutoComplete = false;
                break;
        }
        return gmapsAutoComplete;
    }
Referenced by the following in index.xhtml:

Code: Select all

    <h:head>
        <ui:include src="#{pageNavigationController.gmapsAutoComplete ? '/head_gmapsAutoComplete.xhtml' : (pageNavigationController.gmaps ? '/head_gmaps.xhtml' : '/head_default.xhtml')}"/>
    </h:head>
Solution: since this is a 'getter' method, move code and assign value to gmapsAutoComplete prior to method being called; see code below.

Code: Select all

    /*
     * 2013-04-06 moved switch {...} to updateGmapsAutoComplete()
     *            because performance = 115ms (hot spot) while
     *            navigating through web app
     */
    public Boolean getGmapsAutoComplete() {
        return gmapsAutoComplete;
    }

    /*
     * ALWAYS call this method after "page = ..."
     */
    private void updateGmapsAutoComplete() {
        switch (page) {
            case "/orders/pf_Add.xhtml":
            case "/orders/pf_Edit.xhtml":
            case "/orders/pf_EditDriverVehicles.xhtml":
                gmapsAutoComplete = true;
                break;
            default:
                gmapsAutoComplete = false;
                break;
        }
    }
Test results: PageNavigationController.getGmapsAutoComplete() is no longer a HOT SPOT in Java Visual VM (doesn't even show up anymore)

Sharing this topic, since many of the expert users have advised junior JSF developers to NOT add code in 'getter' methods. :)
Last edited by smithh032772 on 06 Apr 2013, 16:46, edited 3 times in total.
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

kukeltje
Expert Member
Posts: 9340
Joined: 17 Jun 2010, 13:34
Location: Netherlands

06 Apr 2013, 16:39

I would suspect that if you converted the strings to statics, the hotspot might be gone as well...

But was this realy a bottleneck,vor just a relative hotspot...?
Ronald van Kuijk
______________________________
PrimeFaces 6.1, PrimeFaces plus 0.0.2 | JbossWildfly 10.1| Mojarra 2.2.13
Fedora 61, Firefox 'most recent'
Read the forum posting rules
Beginners: https://jsf.zeef.com/bauke.scholtz

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

06 Apr 2013, 16:45

Just a 'noticeable' hot spot and definitely not a bottleneck (before or after the code changes).

'static' is not something i use. of course, pageNavigationController is CDI @SessionScoped for each user session. I have seen many people use 'static', but honestly, I usually remove 'static' whenever I copy/paste code that others have provided. Feel free to enlighten me on the pros/cons of the usage of 'static'. I know, i know, google is my friend. LOL

EDIT: I just recognized that I already have some 'static' strings in the bean, and I added them as 'static', myself, so they will be instantiated and added to memory 'once' after the bean is instantiated.

Code: Select all

    private static String defaultCSS = "css/mcms.css";
    private static String browseCSS = "css/mcmsBrowse.css";
Last edited by smithh032772 on 06 Apr 2013, 16:51, edited 1 time in total.
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

06 Apr 2013, 16:47

kukeltje wrote:I would suspect that if you converted the strings to statics, the hotspot might be gone as well...
oops, my bad, now I know what you mean, now, after re-reading your post. I will definitely add those strings as static to the bean. Thanks Ronald!
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: NOTiFY and 9 guests