package org.graalvm.compiler.microbenchmarks.graal;
import java.util.HashMap;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;
import org.graalvm.compiler.graph.Node;
import org.graalvm.compiler.graph.NodeBitMap;
import org.graalvm.compiler.microbenchmarks.graal.util.GraalState;
import org.graalvm.compiler.microbenchmarks.graal.util.MethodSpec;
import org.graalvm.compiler.microbenchmarks.graal.util.NodesState;
import org.graalvm.compiler.microbenchmarks.graal.util.NodesState.NodePair;
import org.graalvm.compiler.nodes.ConstantNode;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.calc.AddNode;
import org.graalvm.compiler.nodes.util.GraphUtil;
public class NodeBenchmark extends GraalBenchmark {
@MethodSpec(declaringClass = String.class, name = "equals")
public static class StringEquals extends NodesState {
}
@MethodSpec(declaringClass = String.class, name = "equals")
public static class StringEqualsWithGraphCompression extends NodesState {
private static final int N = 100_000;
private int latch = N;
@Override
public void afterInvocation() {
super.afterInvocation();
if (--latch == 0) {
graph.maybeCompress();
latch = N;
}
}
}
@Benchmark
public int getNodeClass(StringEquals s) {
int sum = 0;
for (Node n : s.nodes) {
sum += n.getNodeClass().iterableId();
}
return sum;
}
@Benchmark
public void dataEquals(StringEquals s, Blackhole bh) {
for (Node n : s.nodes) {
bh.consume(n.getNodeClass().dataEquals(n, n));
}
}
@Benchmark
public void replaceFirstInput(StringEquals s, Blackhole bh) {
for (Node n : s.nodes) {
bh.consume(n.getNodeClass().replaceFirstInput(n, n, n));
}
}
@Benchmark
public void inputsEquals(StringEquals s, Blackhole bh) {
for (Node n : s.nodes) {
bh.consume(n.getNodeClass().equalInputs(n, n));
}
}
@Benchmark
public void inputs(StringEquals s, Blackhole bh) {
for (Node n : s.nodes) {
for (Node input : n.inputs()) {
bh.consume(input);
}
}
}
@Benchmark
public void acceptInputs(StringEquals s, Blackhole bh) {
Node.EdgeVisitor consumer = new Node.EdgeVisitor() {
@Override
public Node apply(Node t, Node u) {
bh.consume(u);
return u;
}
};
for (Node n : s.nodes) {
n.applyInputs(consumer);
}
}
@Benchmark
public void createAndDeleteAdd(StringEqualsWithGraphCompression s, Blackhole bh) {
AddNode addNode = new AddNode(ConstantNode.forInt(40), ConstantNode.forInt(2));
s.graph.addOrUniqueWithInputs(addNode);
GraphUtil.killWithUnusedFloatingInputs(addNode);
bh.consume(addNode);
}
@Benchmark
public void createAndDeleteConstant(StringEqualsWithGraphCompression s, Blackhole bh) {
ConstantNode constantNode = ConstantNode.forInt(42);
s.graph.addOrUnique(constantNode);
GraphUtil.killWithUnusedFloatingInputs(constantNode);
bh.consume(constantNode);
}
@Benchmark
public void usages(StringEquals s, Blackhole bh) {
for (Node n : s.nodes) {
for (Node input : n.usages()) {
bh.consume(input);
}
}
}
@Benchmark
public void nodeBitmap(StringEquals s, @SuppressWarnings("unused") GraalState g) {
NodeBitMap bitMap = s.graph.createNodeBitMap();
for (Node node : s.graph.getNodes()) {
if (!bitMap.isMarked(node)) {
bitMap.mark(node);
}
}
for (Node node : s.graph.getNodes()) {
if (bitMap.isMarked(node)) {
bitMap.clear(node);
}
}
}
@MethodSpec(declaringClass = HashMap.class, name = "computeIfAbsent")
public static class HashMapComputeIfAbsent extends NodesState {
}
@Benchmark
public int valueEquals_STRING_EQUALS(StringEquals s) {
int result = 0;
for (NodePair np : s.valueEqualsNodePairs) {
if (np.n1.valueEquals(np.n2)) {
result += 27;
} else {
result += 31;
}
}
return result;
}
@Benchmark
public int valueEquals_HASHMAP_COMPUTE_IF_ABSENT(HashMapComputeIfAbsent s) {
int result = 0;
for (NodePair np : s.valueEqualsNodePairs) {
if (np.n1.valueEquals(np.n2)) {
result += 27;
} else {
result += 31;
}
}
return result;
}
@Benchmark
public int valueNumberLeaf_HASHMAP_COMPUTE_IF_ABSENT(HashMapComputeIfAbsent s) {
int result = 0;
for (Node n : s.valueNumberableLeafNodes) {
result += (n.getNodeClass().isLeafNode() ? 1 : 0);
}
return result;
}
@Benchmark
public int valueNumberLeaf_STRING_EQUALS(StringEquals s) {
int result = 0;
for (Node n : s.valueNumberableLeafNodes) {
result += (n.getNodeClass().isLeafNode() ? 1 : 0);
}
return result;
}
}