Skip to content

Instantly share code, notes, and snippets.

@RichardHightower
Created April 26, 2014 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardHightower/11326741 to your computer and use it in GitHub Desktop.
Save RichardHightower/11326741 to your computer and use it in GitHub Desktop.
Explanation why dead code optimization is not done by Boon JSON Test
The benchmark uses JMH. JMH is what the OpenJDK uses for benchmarking.
```java
@GenerateMicroBenchmark
@OutputTimeUnit( TimeUnit.SECONDS)
public void actionLabel(BlackHole bh) throws Exception {
bh.consume(parse(STR_ACTION_LABEL_BYTES));
}
```
Every method is passed through to a BlackHole.
[BlackHole From JMH](http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-core/src/main/java/org/openjdk/jmh/logic/BlackHole.java).
```java
...
/**
* Black Hole.
* <p/>
* Black hole "consumes" the values, conceiving no information to JIT whether the
* value is actually used afterwards. This can save from the dead-code elimination
* of the computations resulting in the given values.
*
* @author aleksey.shipilev@oracle.com
*/
...
/**
* Consume object. This call provides a side effect preventing JIT to eliminate dependent computations.
*
* @param obj object to consume.
*/
public final void consume(Object obj) {
int tlr = (this.tlr = (this.tlr * 1664525 + 1013904223));
if ((tlr & tlrMask) == 0) {
// SHOULD ALMOST NEVER HAPPEN IN MEASUREMENT
this.obj1 = obj;
this.tlrMask = (this.tlrMask << 1) + 1;
}
}
```
See [JSON BenchMark Using Boon and JMH](https://github.com/RichardHightower/json-parsers-benchmark/blob/master/src/main/java/io/gatling/jsonbenchmark/string/MainBoonBenchmark.java)
```java
@State
public class MainBoonBenchmark {
private final JsonParser parser = new JsonParserFactory ().create ();
private Object parse(String str) throws Exception {
return parser.parse ( str );
}
@GenerateMicroBenchmark
@OutputTimeUnit(TimeUnit.SECONDS)
public void actionLabel(BlackHole bh) throws Exception {
bh.consume(parse(STR_ACTION_LABEL_BYTES));
}
@GenerateMicroBenchmark
@OutputTimeUnit(TimeUnit.SECONDS)
public void citmCatalog(BlackHole bh) throws Exception {
bh.consume(parse(STR_CITM_CATALOG_BYTES));
}
...
```
I think it is very unlikely that the benchmarks I used are "flawed benchmark with tons of potential dead code elimination."
Maybe earlier benchmarks, but not the JMH based ones. But then again. I am always learning something new so teach me.
@RichardHightower
Copy link
Author

The benchmark uses JMH. JMH is what the OpenJDK uses for benchmarking.

    @GenerateMicroBenchmark
    @OutputTimeUnit( TimeUnit.SECONDS)
    public void actionLabel(BlackHole bh) throws Exception {
        bh.consume(parse(STR_ACTION_LABEL_BYTES));
    }

Every method is passed through to a BlackHole.

BlackHole From JMH.

...
/**
 * Black Hole.
 * <p/>
 * Black hole "consumes" the values, conceiving no information to JIT whether the
 * value is actually used afterwards. This can save from the dead-code elimination
 * of the computations resulting in the given values.
 *
 * @author aleksey.shipilev@oracle.com
 */

...

    /**
     * Consume object. This call provides a side effect preventing JIT to eliminate dependent computations.
     *
     * @param obj object to consume.
     */
    public final void consume(Object obj) {
        int tlr = (this.tlr = (this.tlr * 1664525 + 1013904223));
        if ((tlr & tlrMask) == 0) {
            // SHOULD ALMOST NEVER HAPPEN IN MEASUREMENT
            this.obj1 = obj;
            this.tlrMask = (this.tlrMask << 1) + 1;
        }
    }

See JSON BenchMark Using Boon and JMH

@State
public class MainBoonBenchmark {

    private final JsonParser parser = new JsonParserFactory ().create ();

    private Object parse(String str) throws Exception {
        return parser.parse ( str );
    }

    @GenerateMicroBenchmark
    @OutputTimeUnit(TimeUnit.SECONDS)
    public void actionLabel(BlackHole bh) throws Exception {
        bh.consume(parse(STR_ACTION_LABEL_BYTES));
    }

    @GenerateMicroBenchmark
    @OutputTimeUnit(TimeUnit.SECONDS)
    public void citmCatalog(BlackHole bh) throws Exception {
        bh.consume(parse(STR_CITM_CATALOG_BYTES));
    }

...

I think it is very unlikely that the benchmarks I used are "flawed benchmark with tons of potential dead code elimination."

Maybe earlier benchmarks, but not the JMH based ones. But then again. I am always learning something new so teach me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment