XPages: The Problem with DataContext Variables

There is a large problem with data context variables if they are bound dynamically.
They will be recomputed again and again, even when in Partial Execution mode and if they are not in use. Here is a small demo XPage:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

   <xp:this.dataContexts>
      <xp:dataContext var="dataContext">
         <xp:this.value>
            <![CDATA[#{javascript:
               // Sleep for 100ms
               java.lang.Thread.currentThread().sleep( 100 );
               print('-=> Recalc: ' + java.lang.System.currentTimeMillis());
               return true;
            }]]>
          </xp:this.value>
       </xp:dataContext>
    </xp:this.dataContexts>

   <xp:label id="labelRefreshMe">
      <xp:this.value>
         <![CDATA[#{javascript:
            java.lang.System.currentTimeMillis()
         }]]>
      </xp:this.value>
   </xp:label>

   <xp:button value="Refresh" id="buttonRefresh" >
      <xp:eventHandler event="onclick" submit="false"
         refreshMode="partial"
         refreshId="labelRefreshMe"
         execId="labelRefreshMe"
         execMode="partial" />
   </xp:button>

</xp:view>

If you are open the XPage, a dataContext variable will be recalculated for three times:

Clicking on the button will recalculate them up to eleven times:

Keep in mind: Partial Refresh & Partial Execution is enabled. That makes them only useable if their values are computed on page load.

Dieser Beitrag wurde unter Allgemein, Performance, XPages abgelegt und mit , , , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

6 Antworten zu XPages: The Problem with DataContext Variables

  1. if you wrap your code with a
    xp:panel id=“pnl1″ tagName=“div“
    you can reduce the amount of recalcs to at least 4

  2. Ulrich Krause sagt:

    not sure, if this can solve the dilemma, but it cuts down the number of recalcs to 1 on refresh . a dark hack it is …

    Recalc: ' + java.lang.System.currentTimeMillis());
    requestScope.calcDataContext = false
    return true;
    }
    }]]>

  3. Yeah, I remember running across this and being surprised about how frequently they’re re-computed. I like dataContext variables sometimes, but I tend to stick to ${}-binding for this reason.

  4. Paul Withers sagt:

    I’d definitely agree with using dataContexts for computed on page load.

    Your stats confused me a bit because you were getting more messages than I found when I did my session at BLUG, DanNotes and UKLUG. But I then found the reason. My dataContext was bound to a panel, not to the XPage itself. It cuts the calls from 3 to 2 on page load and by half on refresh. I don’t know why, and I wonder if there’s scope for better optimisation of the XPages runtime. (It’s probably working as designed at the moment, but maybe not as well as it could.)

    If you have the same rendered property on more than one component on an XPage, I’d recommend dataContexts. In a panel it performs fractionally worse than that same rendered property directly on the component. But it’s significantly better than the same rendered property on two controls and it’s also means one reference to update if changes are required.

  5. Pingback: In Ya Facet! | blog@hasselba.ch

Schreibe einen Kommentar zu Ulrich Krause Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.