changeset 260:c0db37918128

Runners: do not resize results open-address so often, and for no reason.
author shade
date Thu, 19 May 2016 15:43:53 +0300
parents 064eb636dbea
children d1ed559a1b55
files jcstress-core/src/main/java/org/openjdk/jcstress/util/OpenAddressHashCounter.java
diffstat 1 files changed, 24 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/jcstress-core/src/main/java/org/openjdk/jcstress/util/OpenAddressHashCounter.java	Thu May 19 14:15:43 2016 +0300
+++ b/jcstress-core/src/main/java/org/openjdk/jcstress/util/OpenAddressHashCounter.java	Thu May 19 15:43:53 2016 +0300
@@ -48,14 +48,16 @@
      *     minuscule;
      */
 
-    static final int MAX_TRIES = 3;
+    private static final int RECIPROCAL_LOAD_FACTOR = 10;
+    private static final int INITIAL_CAPACITY = 64;
 
-    Object[] keys;
-    long[] counts;
-    int length;
+    private Object[] keys;
+    private long[] counts;
+    private int length;
+    private int keyCount;
 
     public OpenAddressHashCounter() {
-        this(16);
+        this(INITIAL_CAPACITY);
     }
 
     public OpenAddressHashCounter(int len) {
@@ -69,28 +71,15 @@
 
     @Override
     public void record(R result) {
-        recordWithTries(result, 1, MAX_TRIES);
+        record(result, 1);
     }
 
     @Override
     public void record(R result, long count) {
-        recordWithTries(result, count, MAX_TRIES);
-    }
-
-    @Override
-    public void merge(Counter<R> other) {
-        for (R key : other.elementSet()) {
-            record(key, other.count(key));
-        }
-    }
-
-    private void recordWithTries(R result, long count, int maxTries) {
         int idx = result.hashCode() & (length - 1);
 
-        int tryCount = 0;
         Object k = keys[idx];
         while (k != null) {
-
             // hit the bucket, update and exit
             if (k.equals(result)) {
                 counts[idx] += count;
@@ -100,22 +89,30 @@
             // trying the next bucket
             idx = (idx + 1) & (length - 1);
             k = keys[idx];
+        }
 
-            // whoops, map is overloaded, resize to make up
-            // the space, try again (succeeding), and exit;
-            // we might want to resize early
-            if (tryCount++ > maxTries) {
-                resize();
-                recordWithTries(result, count, Integer.MAX_VALUE);
-                return;
-            }
+        // whoops, map is overloaded, resize to make up
+        // the space, try again (succeeding), and exit;
+        // we might want to resize early
+        if (keyCount * RECIPROCAL_LOAD_FACTOR > length) {
+            resize();
+            record(result, count);
+            return;
         }
 
         // completely new key, insert, and exit
+        keyCount++;
         keys[idx] = decouple(result);
         counts[idx] = count;
     }
 
+    @Override
+    public void merge(Counter<R> other) {
+        for (R key : other.elementSet()) {
+            record(key, other.count(key));
+        }
+    }
+
     private void resize() {
         Object[] prevKeys = keys;
         long[] prevCounts = counts;