artyushov / idea-jmh-plugin

Plugin for Itellij IDEA to make benchmarking with JMH easier.
MIT License
442 stars 43 forks source link

Code intention: blackhole statement #35

Open stokito opened 4 years ago

stokito commented 4 years ago

This is a "good first issue" for newcomers.

Problem description

We have a simple benchmark to measure increment ++ operation:

    @Benchmark
    public void measureInc() {
        int i = 0;
        i++;
    }

Java compiler may eliminate the i variable because it is updated but never used. This called Dead-Code Elimination (DCE).

To avoid this JMH has a special class Blackhole that makes compiler think that the variable is used:

    @Benchmark
    public void measureInc(Blackhole bh) {
        int i = 0;
        i++;
        bh.consume(i);
    }

How plugin should help to solve it

We may inspect code to detect eliminated variables and propose to use the Blackhole. But such detection may be not so easy to implement or maybe we can just use "Unused variable" inspection. Not sure here.

But at least we may propose an intention for this. The intention may work like:

  1. Select variable e.g. var1
  2. Click on the intention "Throw into Black Hole"
  3. If not already exists then a new argument Blackhole bh
  4. Pass the variable to the bh: bh.consume(var1)

Intentions tutorial https://jetbrains.org/intellij/sdk/docs/tutorials/code_intentions.html Feel free to contact me directly by skype/tg stokito in case of any questions