datastore: Keep a snapshot of the latest branch
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Tue, 21 Feb 2017 19:35:45 +0000 (14:35 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Thu, 2 Mar 2017 20:03:25 +0000 (15:03 -0500)
This avoids making immutable copies of the same branch over and over.

Also use the latest branch directly in the node insertion instead of
the getter.

This change alone removes all performance impact of the datastore
when building.

Change-Id: If85ae9bded364cfd6f1243416d025df6307cf909
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/91755
Reviewed-by: Hudson CI
Reviewed-by: Loic Prieur-Drevon <loic.prieur.drevon@ericsson.com>
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/historytree/AbstractHistoryTree.java

index 85df36becff52304d9e675b479eb396fa26b913b..b61152059ec06a16168d07ecaf17ac430c6a0da5 100644 (file)
@@ -118,6 +118,8 @@ public abstract class AbstractHistoryTree<E extends IHTInterval, N extends HTNod
     /* Lock used to protect the accesses to the HT_IO object */
     private final ReentrantReadWriteLock fRwl = new ReentrantReadWriteLock(false);
 
+    private transient @Nullable List<N> fLatestBranchSnapshot = null;
+
     /**
      * Create a new State History from scratch, specifying all configuration
      * parameters.
@@ -364,8 +366,15 @@ public abstract class AbstractHistoryTree<E extends IHTInterval, N extends HTNod
      * @return The immutable latest branch
      */
     @VisibleForTesting
-    List<N> getLatestBranch() {
-        return ImmutableList.copyOf(fLatestBranch);
+    final List<N> getLatestBranch() {
+        List<N> latestBranchSnapshot = fLatestBranchSnapshot;
+        if (latestBranchSnapshot == null) {
+            synchronized (fLatestBranch) {
+                latestBranchSnapshot = ImmutableList.copyOf(fLatestBranch);
+                fLatestBranchSnapshot = latestBranchSnapshot;
+            }
+        }
+        return latestBranchSnapshot;
     }
 
     /**
@@ -634,7 +643,7 @@ public abstract class AbstractHistoryTree<E extends IHTInterval, N extends HTNod
      *            insertion
      */
     protected final void tryInsertAtNode(E interval, int depth) {
-        N targetNode = getLatestBranch().get(depth);
+        N targetNode = fLatestBranch.get(depth);
         informInsertingAtDepth(depth);
 
         /* Verify if there is enough room in this node to store this interval */
@@ -710,6 +719,7 @@ public abstract class AbstractHistoryTree<E extends IHTInterval, N extends HTNod
     private final void addSiblingNode(int depth, long newNodeStartTime) {
         synchronized (fLatestBranch) {
             final long splitTime = fTreeEnd;
+            fLatestBranchSnapshot = null;
 
             if (depth >= fLatestBranch.size()) {
                 /*
This page took 0.025454 seconds and 5 git commands to generate.