changeset 1395:9d25bcca08b4

7901867: Blackhole.consume(Object) should additionally protect from retaining objects forever
author shade
date Thu, 15 Dec 2016 16:56:49 +0100
parents 8b06fb523516
children 9f121ca56cdb
files jmh-core/src/main/java/org/openjdk/jmh/infra/Blackhole.java
diffstat 1 files changed, 7 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/infra/Blackhole.java	Tue Dec 13 20:54:04 2016 +0100
+++ b/jmh-core/src/main/java/org/openjdk/jmh/infra/Blackhole.java	Thu Dec 15 16:56:49 2016 +0100
@@ -26,6 +26,7 @@
 
 import org.openjdk.jmh.util.Utils;
 
+import java.lang.ref.WeakReference;
 import java.util.Random;
 
 /*
@@ -73,7 +74,6 @@
     public float f2;
     public double d2;
     public volatile Object obj1;
-    public volatile Object[] objs1;
     public volatile BlackholeL2 nullBait = null;
     public int tlr;
     public volatile int tlrMask;
@@ -83,7 +83,6 @@
         tlr = r.nextInt();
         tlrMask = 1;
         obj1 = new Object();
-        objs1 = new Object[]{new Object()};
 
         b1 = (byte) r.nextInt(); b2 = (byte) (b1 + 1);
         bool1 = r.nextBoolean(); bool2 = !bool1;
@@ -221,7 +220,11 @@
      * with infinitesimal probability. Then again, smart compilers may skip from
      * generating the slow path, and apply the previous logic to constant-fold
      * the condition to "false". We are warming up the slow-path in the beginning
-     * to evade that effect.
+     * to evade that effect. Some caution needs to be exercised not to retain the
+     * captured objects forever: this is normally achieved by calling evaporate()
+     * regularly, but we also additionally protect with retaining the object on
+     * weak reference (contrary to phantom-ref, publishing object still has to
+     * happen, because reference users might need to discover the object).
      *
      * Observation (4) provides us with an opportunity to create a safety net in case
      * either (1), (2) or (3) fails. This is why Blackhole methods are prohibited from
@@ -291,7 +294,6 @@
             throw new IllegalStateException("Can't touch it.");
         }
         obj1 = null;
-        objs1 = null;
     }
 
     /**
@@ -304,22 +306,7 @@
         int tlr = (this.tlr = (this.tlr * 1664525 + 1013904223));
         if ((tlr & tlrMask) == 0) {
             // SHOULD ALMOST NEVER HAPPEN IN MEASUREMENT
-            this.obj1 = obj;
-            this.tlrMask = (tlrMask << 1) + 1;
-        }
-    }
-
-    /**
-     * Consume object. This call provides a side effect preventing JIT to eliminate dependent computations.
-     *
-     * @param objs objects to consume.
-     */
-    public final void consume(Object[] objs) {
-        int tlrMask = this.tlrMask; // volatile read
-        int tlr = (this.tlr = (this.tlr * 1664525 + 1013904223));
-        if ((tlr & tlrMask) == 0) {
-            // SHOULD ALMOST NEVER HAPPEN IN MEASUREMENT
-            this.objs1 = objs;
+            this.obj1 = new WeakReference<>(obj);
             this.tlrMask = (tlrMask << 1) + 1;
         }
     }