]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/MainProgram.java
Avoid useless reallocation of empty TreeMaps and map iterators
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / MainProgram.java
index 8dea16d7fe73a5cc0234a6e00998ad964f184513..52be328eb87ea5de7d9b58fed20e1fe53439f727 100644 (file)
@@ -61,6 +61,7 @@ public class MainProgram implements Runnable, Closeable {
                }
        }
 
+       @SuppressWarnings("unchecked")
        MainProgram(GraphClientImpl2 client, ClusterManager clusters) {
 
                this.client = client;
@@ -69,7 +70,7 @@ public class MainProgram implements Runnable, Closeable {
                this.updateSchedules = new ArrayList[CLUSTER_THREADS];
                for(int i=0;i<clusterUpdateThreads.length;i++) {
                        clusterUpdateThreads[i] = Executors.newSingleThreadExecutor(new ClusterThreadFactory("Cluster Updater " + (i+1), false));
-                       updateSchedules[i] = new ArrayList<ClusterUpdateOperation>();
+                       updateSchedules[i] = new ArrayList<>();
                }
        }
 
@@ -92,26 +93,26 @@ public class MainProgram implements Runnable, Closeable {
 
                try {
 
+                       TreeMap<ClusterUID, List<ClusterUpdateOperation>> updates = new TreeMap<>(clusterComparator);
+
                        main:
                        while(alive) {
 
-                               TreeMap<ClusterUID, List<ClusterUpdateOperation>> updates = new TreeMap<ClusterUID, List<ClusterUpdateOperation>>(clusterComparator);
-
+                               if (!updates.isEmpty())
+                                       updates.clear();
                                operationQueue.pumpUpdates(updates);
 
                                if(updates.isEmpty()) {
 
-                                       long start = System.nanoTime();
-
-                                       operationQueue.waitFor();
+                                       long duration = operationQueue.waitFor();
 
                                        if (!alive)
                                                break main;
 
-                                       long duration = System.nanoTime()-start;
                                        if(duration > 4000000000L) {
                                                checkIdle();
                                        }
+                                       
                                }
 
 //                             long sss = System.nanoTime();
@@ -128,7 +129,7 @@ public class MainProgram implements Runnable, Closeable {
                                try {
                                        swapChunks();
                                } catch (AcornAccessVerificationException | IllegalAcornStateException e) {
-                                   e.printStackTrace();
+                                       LOGGER.error("cluster chunk swapping failed", e);
                                } finally {
                                        clusters.streamLRU.releaseMutex();
                                }
@@ -146,7 +147,7 @@ public class MainProgram implements Runnable, Closeable {
                        }
 
                } catch (Throwable t) {
-                       t.printStackTrace();
+                       LOGGER.error("FATAL: MainProgram died unexpectedly", t);
                } finally {
                        deathBarrier.release();
                }
@@ -166,6 +167,9 @@ public class MainProgram implements Runnable, Closeable {
                for(int i=0;i<CLUSTER_THREADS;i++)
                        updateSchedules[i].clear();
 
+               if (updates.isEmpty())
+                       return;
+
                final Semaphore s = new Semaphore(0);
 
                for(Map.Entry<ClusterUID, List<ClusterUpdateOperation>> entry : updates.entrySet()) {
@@ -356,7 +360,7 @@ public class MainProgram implements Runnable, Closeable {
                 executor.awaitTermination(500, TimeUnit.MILLISECONDS);
                 clusterUpdateThreads[i] = null;
             } catch (InterruptedException e) {
-                e.printStackTrace();
+                LOGGER.error("clusterUpdateThread[{}] termination interrupted", i, e);
             }
         }
     }