From 6bfdbf94113b785d27667925bdcff38cb9b4466b Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Sat, 21 Dec 2024 07:49:12 +0100 Subject: [PATCH] Sum re field of a Complex object in a Vector is the base benchmark --- .../semantic/MultiValueBenchmarks.java | 60 +++++++++++++------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/engine/runtime-benchmarks/src/main/java/org/enso/interpreter/bench/benchmarks/semantic/MultiValueBenchmarks.java b/engine/runtime-benchmarks/src/main/java/org/enso/interpreter/bench/benchmarks/semantic/MultiValueBenchmarks.java index 883f33951e9d..d5040e2a5dbb 100644 --- a/engine/runtime-benchmarks/src/main/java/org/enso/interpreter/bench/benchmarks/semantic/MultiValueBenchmarks.java +++ b/engine/runtime-benchmarks/src/main/java/org/enso/interpreter/bench/benchmarks/semantic/MultiValueBenchmarks.java @@ -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; @@ -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 { @@ -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 @@ -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 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 base benchmark 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); @@ -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); }