Fix performance issue in EffectiveStatementBase 20/27720/3
authorTom Pantelis <tpanteli@brocade.com>
Thu, 1 Oct 2015 07:11:28 +0000 (03:11 -0400)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 1 Oct 2015 10:22:20 +0000 (10:22 +0000)
commit69dcbda23d8cf16be62233558d98b9d43ee618dd
treeacf426dab7febf980ec9216a29cda9df9de73507
parentd02a4b5555ec9139f448cb2ab4e041a3efb6f3c1
Fix performance issue in EffectiveStatementBase

While running some feature tests I took some thread dumps and noticed
the following stack trace in each:

java.lang.Thread.State: RUNNABLE
        at java.lang.Throwable.fillInStackTrace(Native Method)
        at java.lang.Throwable.fillInStackTrace(Throwable.java:783)
        - locked <0x00000000e5aff880> (a
          java.util.NoSuchElementException)
        at java.lang.Throwable.<init>(Throwable.java:250)
        at java.lang.Exception.<init>(Exception.java:41)
        at java.lang.RuntimeException.<init>(RuntimeException.java:51)
        at
java.util.NoSuchElementException.<init>(NoSuchElementException.java:47)
        at
com.google.common.collect.AbstractIterator.next(AbstractIterator.java:154)
        at com.google.common.collect.Iterators.find(Iterators.java:717)
        at com.google.common.collect.Iterables.find(Iterables.java:646)
        at
org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase.firstEffective(EffectiveStatementBase.java:105)

The problem is that Iterables.find throws NoSuchElementException if not
found which is expensive when called many times. I changed it to call
tryFind instead which returns an Optional and this significantly
improved performance. The test time went from 15 min to 3 min.

There may be other classes that call Iterables.find - I didn't check.
If so they can be fixed in subsequent patches.

Change-Id: I0b3d02979b6d7fc97752c4b2429720b0807b853b
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java