I'm just taking a look into the code for the tree component and I see the
following in org.apache.tapestry5.corelib.components.Tree:

    if (node.isLeaf())
        e.addClassName("t-leaf-node");
    else if (!node.getHasChildren())
        e.addClassName("t-empty-node");

    boolean hasChildren = !node.isLeaf() && node.getHasChildren();
    boolean expanded = hasChildren && expansionModel.isExpanded(node);

As you can see here, isLeaf() and getHasChildren() can be called multiple
times. If the tree is backed by a database, it's likely that isLeaf() and
getHasChildren() both cost a hit to the database. There is also a chance
that isLeaf() and getHasChildren() call the exact same query. With the code
above, there is therefore a chance that 4 hits to the database are made
instead of one.

If the code were changed as follows, it's a quick win to reduce database
hits without the need to create a TreeModelAdapter with a messy threadLocal.

   boolean isLeaf = node.isLeaf();
   boolean hasChildren = !isLeaf && node.getHasChildren();
   if (isLeaf)
      e.addClassName("t-leaf-node");
   else if (!hasChildren)
      e.addClassName("t-empty-node");

   boolean expanded = hasChildren && expansionModel.isExpanded(node);

Reply via email to