Skip to content

Commit

Permalink
Sum re field of a Complex object in a Vector is the base benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Dec 21, 2024
1 parent 9567257 commit 6bfdbf9
Showing 1 changed file with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.enso.interpreter.bench.benchmarks.semantic;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.enso.common.MethodNames;
import org.enso.compiler.benchmarks.Utils;
import org.graalvm.polyglot.Value;
import org.openjdk.jmh.annotations.Benchmark;
Expand All @@ -23,8 +23,8 @@
*/
@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
@Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class MultiValueBenchmarks {
Expand All @@ -47,19 +47,25 @@ public void initializeBenchmark(BenchmarkParams params) throws Exception {
sum arr =
go acc i = if i >= arr.length then acc else
v = arr.at i : Float
v = arr.at i
sum = acc + v
@Tail_Call go sum i+1
go 0 0
sum_re arr =
go acc i = if i >= arr.length then acc else
v = arr.at i . re
sum = acc + v
@Tail_Call go sum i+1
go 0 0
make_vector type n =
Vector.new n i->
r = 3 + 5*i
case type of
0 -> r:Integer
1 -> r:Float
2 -> r:Complex
0 -> r:Complex
1 -> r:Integer
2 -> r:Float
3 ->
c = r:Complex&Float
c:Float
Expand All @@ -71,32 +77,48 @@ public void initializeBenchmark(BenchmarkParams params) throws Exception {
""";
var benchmarkName = SrcUtil.findName(params);
var src = SrcUtil.source(benchmarkName, code);
var module = ctx.eval(src);

this.self = module.invokeMember("get_associated_type");
Function<String, Value> getMethod = (name) -> module.invokeMember("get_method", self, name);

String test_builder;
int type = Integer.parseInt(benchmarkName.substring(benchmarkName.length() - 1));
this.arrayOfNumbers = getMethod.apply("make_vector").execute(self, type, length);
this.sum = getMethod.apply("sum");

var module = ctx.eval(src);
this.self = module.invokeMember(MethodNames.Module.GET_ASSOCIATED_TYPE);
var makeVector = module.invokeMember("get_method", self, "make_vector");
this.arrayOfNumbers = makeVector.execute(self, type, length);
this.sum =
module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, type == 0 ? "sum_re" : "sum");
}

/**
* The <b>base benchmark</b> for this suite. Measures how much it takes to access an Atom in a
* Vector, read {@code re:Float} field out of it and sum all of them together.
*/
@Benchmark
public void sumOverInteger0(Blackhole matter) {
public void sumOverComplexBaseBenchmark0(Blackhole matter) {
performBenchmark(matter);
}

/**
* Working with {@code Integer} should be the fastest. The plus operation on integer are faster
* than those on {@code Float} and moreover the {@code Vector} has a special representation when
* full of {@code long} values.
*/
@Benchmark
public void sumOverFloat1(Blackhole matter) {
public void sumOverInteger1(Blackhole matter) {
performBenchmark(matter);
}

/**
* Working with {@code Float} should be also fast. The {@code Vector} has a special representation
* when full of {@code double} values that increases cache locality.
*/
@Benchmark
public void sumOverComplexCast2(Blackhole matter) {
public void sumOverFloat2(Blackhole matter) {
performBenchmark(matter);
}

//
// Following benchmarks shall catch up with the base benchmark
//

@Benchmark
public void sumOverComplexFloatRecastedToFloat3(Blackhole matter) {
performBenchmark(matter);
Expand All @@ -118,7 +140,7 @@ public void sumOverFloatAndComplex6(Blackhole matter) {
}

private void performBenchmark(Blackhole matter) throws AssertionError {
var resultValue = sum.execute(self, arrayOfNumbers);
var resultValue = sum.execute(arrayOfNumbers);
if (!resultValue.fitsInLong()) {
throw new AssertionError("Shall be a long: " + resultValue);
}
Expand Down

0 comments on commit 6bfdbf9

Please sign in to comment.