Skip to content

Commit

Permalink
Type Switch scalability issue
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Oct 11, 2023
1 parent 4dc7bdc commit ac38cd0
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>19</source>
<target>19</target>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package red.hat.puzzles.polymorphism;

import org.openjdk.jmh.annotations.*;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import java.util.function.IntSupplier;

@State(Scope.Benchmark)
@Measurement(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Warmup(iterations = 15, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Fork(2)
public class TypeSwitchScalabilityBenchmark {

private static final class SerializableIntGarbage implements IntSupplier, Serializable {
private final int value;

public SerializableIntGarbage(int value) {
this.value = value;
}

@Override
public int getAsInt() {
return value;
}
}

private static final class SerializableInt implements IntSupplier, Serializable {
private final int value;

public SerializableInt(int value) {
this.value = value;
}

@Override
public int getAsInt() {
return value;
}
}

@CompilerControl(CompilerControl.Mode.DONT_INLINE)
private static boolean serialize(Serializable o) {
// NO-OP, we live dangerously here :P
return true;
}

static boolean serialize(Object o) {
return switch (o) {
case Serializable value -> serialize(value);
default -> false;
};
}

static int getAsInt(Object o) {
return switch (o) {
case IntSupplier value -> value.getAsInt();
default -> -1;
};
}

private Object o;
@Param({"false", "true"})
private boolean typePollution;

@Setup
public void init() {
if (typePollution) {
o = new SerializableIntGarbage(0);
for (int i = 0; i < 11000; i++) {
doSerialize();
doGetAsInt();
}
o = new SerializableInt(0);
for (int i = 0; i < 11000; i++) {
doSerialize();
doGetAsInt();
}
}
o = new SerializableInt(0);

}

@Benchmark
@Group
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public boolean doSerialize() {
return serialize(o);
}

@Benchmark
@Group
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public int doGetAsInt() {
return getAsInt(o);
}

}

0 comments on commit ac38cd0

Please sign in to comment.