From: Robert Varga Date: Thu, 19 Mar 2015 11:58:48 +0000 (+0100) Subject: Optimize ResolveDataChangeEventsTask object allocation X-Git-Tag: release/lithium~372^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=26a633e9cc7382bb90fbfbd07e5ad9443f8ca0dd Optimize ResolveDataChangeEventsTask object allocation ResolveDataChangeEventsTask queried before/afterData methods multiple times. The default implementation in yangtools instantiates an object on each call, which leads to sub-optimal performance. Cache the returned object and use it locally, which actually makes the code more readable. As a further change, perform the same caching with modification type, as that may end up being computed on access. Change-Id: I43aa4824c1a5788060035cd419400d86b0116de7 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/ResolveDataChangeEventsTask.java b/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/ResolveDataChangeEventsTask.java index 14d565c1d0..a62f1ed0f2 100644 --- a/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/ResolveDataChangeEventsTask.java +++ b/opendaylight/md-sal/sal-inmemory-datastore/src/main/java/org/opendaylight/controller/md/sal/dom/store/impl/ResolveDataChangeEventsTask.java @@ -101,43 +101,46 @@ final class ResolveDataChangeEventsTask { * @return True if the subtree changed, false otherwise */ private boolean resolveAnyChangeEvent(final ResolveDataChangeState state, final DataTreeCandidateNode node) { - if (node.getModificationType() != ModificationType.UNMODIFIED && - !node.getDataAfter().isPresent() && !node.getDataBefore().isPresent()) { + final Optional> maybeBefore = node.getDataBefore(); + final Optional> maybeAfter = node.getDataAfter(); + final ModificationType type = node.getModificationType(); + + if (type != ModificationType.UNMODIFIED && !maybeAfter.isPresent() && !maybeBefore.isPresent()) { LOG.debug("Modification at {} has type {}, but no before- and after-data. Assuming unchanged.", - state.getPath(), node.getModificationType()); + state.getPath(), type); return false; } // no before and after state is present - switch (node.getModificationType()) { + switch (type) { case SUBTREE_MODIFIED: return resolveSubtreeChangeEvent(state, node); case MERGE: case WRITE: - Preconditions.checkArgument(node.getDataAfter().isPresent(), - "Modification at {} has type {} but no after-data", state.getPath(), node.getModificationType()); - if (!node.getDataBefore().isPresent()) { + Preconditions.checkArgument(maybeAfter.isPresent(), + "Modification at {} has type {} but no after-data", state.getPath(), type); + if (!maybeBefore.isPresent()) { @SuppressWarnings({ "unchecked", "rawtypes" }) - final NormalizedNode afterNode = (NormalizedNode)node.getDataAfter().get(); + final NormalizedNode afterNode = (NormalizedNode)maybeAfter.get(); resolveSameEventRecursivelly(state, afterNode, DOMImmutableDataChangeEvent.getCreateEventFactory()); return true; } - return resolveReplacedEvent(state, node.getDataBefore().get(), node.getDataAfter().get()); + return resolveReplacedEvent(state, maybeBefore.get(), maybeAfter.get()); case DELETE: - Preconditions.checkArgument(node.getDataBefore().isPresent(), - "Modification at {} has type {} but no before-data", state.getPath(), node.getModificationType()); + Preconditions.checkArgument(maybeBefore.isPresent(), + "Modification at {} has type {} but no before-data", state.getPath(), type); @SuppressWarnings({ "unchecked", "rawtypes" }) - final NormalizedNode beforeNode = (NormalizedNode)node.getDataBefore().get(); + final NormalizedNode beforeNode = (NormalizedNode)maybeBefore.get(); resolveSameEventRecursivelly(state, beforeNode, DOMImmutableDataChangeEvent.getRemoveEventFactory()); return true; case UNMODIFIED: return false; } - throw new IllegalStateException(String.format("Unhandled node state %s at %s", node.getModificationType(), state.getPath())); + throw new IllegalStateException(String.format("Unhandled node state %s at %s", type, state.getPath())); } private boolean resolveReplacedEvent(final ResolveDataChangeState state, @@ -257,8 +260,11 @@ final class ResolveDataChangeEventsTask { } private boolean resolveSubtreeChangeEvent(final ResolveDataChangeState state, final DataTreeCandidateNode modification) { - Preconditions.checkArgument(modification.getDataBefore().isPresent(), "Subtree change with before-data not present at path %s", state.getPath()); - Preconditions.checkArgument(modification.getDataAfter().isPresent(), "Subtree change with after-data not present at path %s", state.getPath()); + final Optional> maybeBefore = modification.getDataBefore(); + final Optional> maybeAfter = modification.getDataAfter(); + + Preconditions.checkArgument(maybeBefore.isPresent(), "Subtree change with before-data not present at path %s", state.getPath()); + Preconditions.checkArgument(maybeAfter.isPresent(), "Subtree change with after-data not present at path %s", state.getPath()); if (!state.needsProcessing()) { LOG.trace("Not processing modified subtree {}", state.getPath()); @@ -288,8 +294,8 @@ final class ResolveDataChangeEventsTask { } } - final NormalizedNode before = modification.getDataBefore().get(); - final NormalizedNode after = modification.getDataAfter().get(); + final NormalizedNode before = maybeBefore.get(); + final NormalizedNode after = maybeAfter.get(); if (scope != null) { DOMImmutableDataChangeEvent one = DOMImmutableDataChangeEvent.builder(scope).addUpdated(state.getPath(), before, after).build();