Return-Path: X-Original-To: apmail-jackrabbit-oak-dev-archive@minotaur.apache.org Delivered-To: apmail-jackrabbit-oak-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3786810FB5 for ; Tue, 1 Oct 2013 03:08:19 +0000 (UTC) Received: (qmail 56157 invoked by uid 500); 1 Oct 2013 03:08:18 -0000 Delivered-To: apmail-jackrabbit-oak-dev-archive@jackrabbit.apache.org Received: (qmail 56046 invoked by uid 500); 1 Oct 2013 03:08:14 -0000 Mailing-List: contact oak-dev-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: oak-dev@jackrabbit.apache.org Delivered-To: mailing list oak-dev@jackrabbit.apache.org Received: (qmail 56037 invoked by uid 99); 1 Oct 2013 03:08:10 -0000 Received: from minotaur.apache.org (HELO minotaur.apache.org) (140.211.11.9) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Oct 2013 03:08:10 +0000 Received: from localhost (HELO mail-ie0-f179.google.com) (127.0.0.1) (smtp-auth username tripod, mechanism plain) by minotaur.apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Oct 2013 03:08:10 +0000 Received: by mail-ie0-f179.google.com with SMTP id e14so12678880iej.10 for ; Mon, 30 Sep 2013 20:08:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=75b+fpmMmXyBhHKL8cWk+fJTY20WjXD8KMayeniLw7g=; b=GFZw6WqO//L9fNh8s8sjynM9KK6VUi51tsvyCuQa2y3V/tUaE1G5q8JllSvu6aoP5W xdd5K6c8SHqHsT3fImYR5TrK4XfA56STaN8xJiPk1lS6SZo/iMxiT75Wa1w/5znVyyDT 2ER3+1lDFlHbCmSeDeFgwGMmMnRiP0UcTuNcDGaB1yhvsd4KQl5rmDXWgl1dPhgYHNFS eK7c8Ko18rNI3aP2tx3Qr4nSrvZ+g/zAdsH/1SN+7XLk4QvV6ikl6Tn+t8+crYN90Dm5 /pAG3ITvsk13a+PJYEOlSKIMx95QBwvrFvXqhoGpojYe5kum1jxCdD4UBLlJZpCBd1lf lMAA== X-Gm-Message-State: ALoCoQmwA5ttG2Ff3YmOenVmVAjepPbZ7bdX1sN3HnSOKdr6WKunCo1latwJrDclLnYKuSVHpaW8 MIME-Version: 1.0 X-Received: by 10.50.6.71 with SMTP id y7mr16026120igy.8.1380596889505; Mon, 30 Sep 2013 20:08:09 -0700 (PDT) Received: by 10.64.68.20 with HTTP; Mon, 30 Sep 2013 20:08:09 -0700 (PDT) In-Reply-To: References: Date: Mon, 30 Sep 2013 20:08:09 -0700 Message-ID: Subject: Re: Tree.hasChild() / Tree.getChild() From: Tobias Bocanegra To: oak-dev@jackrabbit.apache.org Content-Type: text/plain; charset=UTF-8 Hi, On Mon, Sep 30, 2013 at 6:56 PM, Jukka Zitting wrote: > Hi, > > On Mon, Sep 30, 2013 at 4:22 PM, Tobias Bocanegra wrote: >> I was looking at some spots where the code can be optimized, and I >> found some occurrences where a Tree.hasChild() is followed by a >> Tree.getChild(). I think this pattern is used commonly. > > Right. I think the pattern dates back to the time before we added the > Tree.exists() method and made Tree.getChild() return non-null every > time regardless of whether the identified child exists or is > accessible. > > Instead of the old pattern ... > > if (tree.hasChild(name)) { > Tree child = tree.getChild(name); > ...; > } else { > ...; > } > > ... a better pattern would now be ... > > Tree child = tree.getChild(name); > if (child.exists()) { > ...; > } else { > ...; > } > >> so I think that it could make sense to add an optimizable convenience method: >> [...] >> @Nullable >> Tree Tree.getChild(@Nonnull String name, boolean mustExist) > > I'm not sure if this helps much beyond the exists() mechanism. The > case of mustExists == false is equivalent to the existing getChild() > method, and the access pattern with the mustExist == true case would > be: > > Tree child = tree.getChild(name, true); > if (child != null) { > ...; > } else { > ...; > } > > The only difference is between "child.exists()" and "child != null", > and AFAICT any optimizations done in such an extra getChild() method > could also be used to optimize the exists() method. right. but depending on the persistence implementation and caching, it could result in fetching the child twice if using just the exists() check. It might be nit-picky, but each createChild() creates 2-3 objects. of course this optimization only makes sense, if that signature is propagated further down. OTOH, I looked at the places where hasChild/getChild is used again, and it's only in 3-4 places. so maybe not important to enough to change. regards, toby