diff --git a/pom.xml b/pom.xml
index ba3f38b8b3..1d807129af 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,10 +27,6 @@ We use these goals frequently to keep the dependencies and plugins up-to-date:
Vavr (formerly called Javaslang) is an object-functional language extension to Java 8+.vavr
- vavr-benchmark
- vavr-match
- vavr-match-processor
- vavr-testhttps://vavr.io
diff --git a/vavr-benchmark/pom.xml b/vavr-benchmark/pom.xml
deleted file mode 100644
index 3b2cbf6a5f..0000000000
--- a/vavr-benchmark/pom.xml
+++ /dev/null
@@ -1,128 +0,0 @@
-
- 4.0.0
-
- io.vavr
- vavr-parent
- 0.10.6-SNAPSHOT
- ../pom.xml
-
- vavr-benchmark
- Vavr Benchmark
- Benchmarks for Vavr.
- https://vavr.io
-
-
- The Apache Software License, Version 2.0
- http://www.apache.org/licenses/LICENSE-2.0.txt
- repo
-
-
-
-
- ${project.groupId}
- vavr
- ${project.version}
-
-
- org.scalaz
- scalaz-core_2.12
- 7.3.8
-
-
-
- org.clojure
- clojure
- 1.12.0
-
-
- org.pcollections
- pcollections
- 4.0.2
-
-
- org.functionaljava
- functionaljava-java8
- 4.6
-
-
- org.eclipse.collections
- eclipse-collections
- 11.1.0
-
-
- io.usethesource
- capsule
- 0.6.1
-
-
- org.openjdk.jol
- jol-core
- 0.17
-
-
- org.jetbrains
- annotations
- 26.0.1
-
-
- org.junit.jupiter
- junit-jupiter
- test
-
-
- org.openjdk.jmh
- jmh-generator-annprocess
- test
-
-
-
-
-
- org.codehaus.mojo
- build-helper-maven-plugin
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${maven.surefire.version}
-
-
- **/*Benchmark.java
-
- true
-
-
-
- org.apache.maven.plugins
- maven-deploy-plugin
-
- true
-
-
-
- org.apache.maven.plugins
- maven-install-plugin
-
- true
-
-
-
- org.sonatype.plugins
- nexus-staging-maven-plugin
-
- true
-
-
-
-
-
-
- usethesource
- https://releases.usethesource.io/maven/
-
-
-
diff --git a/vavr-benchmark/src/test/java/io/vavr/BenchmarkPerformanceReporter.java b/vavr-benchmark/src/test/java/io/vavr/BenchmarkPerformanceReporter.java
deleted file mode 100644
index 300c457a61..0000000000
--- a/vavr-benchmark/src/test/java/io/vavr/BenchmarkPerformanceReporter.java
+++ /dev/null
@@ -1,514 +0,0 @@
-/* ____ ______________ ________________________ __________
- * \ \/ / \ \/ / __/ / \ \/ / \
- * \______/___/\___\______/___/_____/___/\___\______/___/\___\
- *
- * Copyright 2014-2025 Vavr, https://vavr.io
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.vavr;
-
-import io.vavr.collection.Array;
-import io.vavr.collection.CharSeq;
-import io.vavr.collection.Map;
-import io.vavr.control.Option;
-import org.openjdk.jmh.infra.BenchmarkParams;
-import org.openjdk.jmh.results.BenchmarkResult;
-import org.openjdk.jmh.results.Result;
-import org.openjdk.jmh.results.RunResult;
-import org.openjdk.jmh.util.ListStatistics;
-
-import java.text.DecimalFormat;
-import java.util.Comparator;
-import java.util.function.BiFunction;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-
-public class BenchmarkPerformanceReporter {
- private static final Comparator TO_STRING_COMPARATOR = Comparator.comparing(String::length).thenComparing(Function.identity());
- private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,##0.00");
- private static final DecimalFormat PERFORMANCE_FORMAT = new DecimalFormat("#0.00");
- private static final DecimalFormat PCT_FORMAT = new DecimalFormat("0.00%");
-
- private final Array includeNames;
- private final Array benchmarkClasses;
- private final Array runResults;
- private final String targetImplementation;
- private final double outlierLowPct;
- private final double outlierHighPct;
-
- public static BenchmarkPerformanceReporter of(Array includeNames, Array benchmarkClasses, Array runResults) {
- return of(includeNames, benchmarkClasses, runResults, "vavr", 0.3, 0.05);
- }
-
- public static BenchmarkPerformanceReporter of(Array includeNames, Array benchmarkClasses, Array runResults, String targetImplementation, double outlierLowPct, double outlierHighPct) {
- return new BenchmarkPerformanceReporter(includeNames, benchmarkClasses, runResults, targetImplementation, outlierLowPct, outlierHighPct);
- }
-
- /**
- * This class prints performance reports about the execution of individual tests, comparing their performance
- * against other implementations as required.
- *
- * @param benchmarkClasses The benchmarked source class names
- * @param runResults The results
- * @param targetImplementation The target implementation we want to focus on in the Ratio report.
- * It is case insensitive. If we enter "vavr", it will match "VaVr" and "va_vr".
- * @param outlierLowPct The percentage of samples on the lower end that will be ignored from the statistics
- * @param outlierHighPct The percentage of samples on the higher end that will be ignored from the statistics
- */
- private BenchmarkPerformanceReporter(Array includeNames, Array benchmarkClasses, Array runResults, String targetImplementation, double outlierLowPct, double outlierHighPct) {
- this.includeNames = includeNames;
- this.benchmarkClasses = benchmarkClasses;
- this.runResults = runResults;
- this.targetImplementation = targetImplementation;
- this.outlierLowPct = outlierLowPct;
- this.outlierHighPct = outlierHighPct;
- }
-
- /**
- * Prints all performance reports
- */
- public void print() {
- printDetailedPerformanceReport();
- printRatioPerformanceReport();
- }
-
- /**
- * Prints the detail performance report for each individual test.
- *
- * For each test it prints out:
- *
- *
Group
- *
Test Name
- *
Implementation - tests can have different implementations, e.g. Scala, Java, Vavr
- *
Parameters
- *
Score
- *
Error - 99% confidence interval expressed in % of the Score
- *
Unit - units for the Score
- *
Alternative implementations - compares performance of this test against alternative implementations
- *
- */
- public void printDetailedPerformanceReport() {
- final Array results = mapToTestExecutions();
- if (results.isEmpty()) {
- return;
- }
- new DetailedPerformanceReport(results).print();
- }
-
- /**
- * Prints the performance ratio report for each test, and compares the performance against different implementations
- * of the same operation.
- *
- * For each test it prints out:
- *
- *
Group
- *
Test Name
- *
Ratio - A/B means implementation A is compared against base implementation B
- *
Results - How many times faster implementation A is compared with B
- *
- */
- public void printRatioPerformanceReport() {
- final Array results = mapToTestExecutions();
- if (results.isEmpty()) {
- return;
- }
- new RatioPerformanceReport(results, targetImplementation).print();
- }
-
- private Array mapToTestExecutions() {
- Array executions = Array.empty();
- for (RunResult runResult : runResults) {
- executions = executions.append(TestExecution.of(runResult.getAggregatedResult(), outlierLowPct, outlierHighPct));
- }
- return sort(executions, includeNames);
- }
-
- private Array sort(Array results, Array includeNames) {
- final Comparator comparator = Comparator
- . comparing(t -> benchmarkClasses.indexWhere(c -> c.endsWith(t.getOperation())))
- .thenComparing(t -> includeNames.indexWhere(i -> t.getImplementation().startsWith(i)));
- return results.sorted(comparator);
- }
-
- private String padLeft(String str, int size) {
- return str + CharSeq.repeat(' ', size - str.length());
- }
-
- private String padRight(String str, int size) {
- return CharSeq.repeat(' ', size - str.length()) + str;
- }
-
- private class DetailedPerformanceReport {
- private final Array results;
- private final Map> resultsByKey;
- private final int paramKeySize;
- private final int groupSize;
- private final int nameSize;
- private final int implSize;
- private final int countSize;
- private final int scoreSize;
- private final int errorSize;
- private final int unitSize;
- private final Array alternativeImplementations;
-
- public DetailedPerformanceReport(Array results) {
- this.results = results;
- resultsByKey = results.groupBy(TestExecution::getTestNameParamKey);
- paramKeySize = Math.max(results.map(r -> r.getParamKey().length()).max().get(), 10);
- groupSize = Math.max(results.map(r -> r.getTarget().length()).max().get(), 10);
- nameSize = Math.max(results.map(r -> r.getOperation().length()).max().get(), 10);
- implSize = Math.max(results.map(r -> r.getImplementation().length()).max().get(), 10);
- countSize = Math.max(results.map(r -> Long.toString(r.getSampleCount()).length()).max().get(), 5);
- scoreSize = Math.max(results.map(r -> r.getScoreFormatted().length()).max().get(), 15);
- errorSize = Math.max(results.map(r -> r.getScoreErrorPct().length()).max().get(), 10);
- unitSize = Math.max(results.map(r -> r.getUnit().length()).max().get(), 7);
-
- alternativeImplementations = results.map(TestExecution::getImplementation).distinct();
- }
-
- public void print() {
- printHeader();
- printDetails();
- }
-
- private void printHeader() {
- final String alternativeImplHeader = alternativeImplementations.map(altImpl -> padRight(altImpl, altImplColSize(altImpl))).mkString(" ");
- final String header = String.format("%s %s %s %s %s %s ±%s %s %s",
- padLeft("Target", groupSize),
- padLeft("Operation", nameSize),
- padLeft("Impl", implSize),
- padRight("Params", paramKeySize),
- padRight("Count", countSize),
- padRight("Score", scoreSize),
- padRight("Error", errorSize),
- padRight("Unit", unitSize),
- alternativeImplHeader
- );
-
- System.out.println("\n\n\n");
- System.out.println("Detailed Performance Execution Report");
- System.out.println(CharSeq.of("=").repeat(header.length()));
- System.out.println(" (Error: ±99% confidence interval, expressed as % of Score)");
- if (outlierLowPct > 0.0 && outlierHighPct > 0.0) {
- System.out.println(String.format(" (Outliers removed: %s low end, %s high end)", PCT_FORMAT.format(outlierLowPct), PCT_FORMAT.format(outlierHighPct)));
- }
- if (!alternativeImplementations.isEmpty()) {
- System.out.println(String.format(" (%s: read as current row implementation is x times faster than alternative implementation)", alternativeImplementations.mkString(", ")));
- }
- System.out.println();
- System.out.println(header);
- }
-
- private void printDetails() {
- for (TestExecution result : results) {
- System.out.println(String.format("%s %s %s %s %s %s ±%s %s %s",
- padLeft(result.getTarget(), groupSize),
- padLeft(result.getOperation(), nameSize),
- padLeft(result.getImplementation(), implSize),
- padRight(result.getParamKey(), paramKeySize),
- padRight(Long.toString(result.getSampleCount()), countSize),
- padRight(result.getScoreFormatted(), scoreSize),
- padRight(result.getScoreErrorPct(), errorSize),
- padRight(result.getUnit(), unitSize),
- calculatePerformanceStr(result, alternativeImplementations, resultsByKey)
- ));
- }
- System.out.println("\n");
- }
-
- private int altImplColSize(String name) {
- return Math.max(5, name.length());
- }
-
- private String calculatePerformanceStr(TestExecution result, Array alternativeImplementations, Map> resultsByKey) {
- final String aggregateKey = result.getTestNameParamKey();
- final Array alternativeResults = resultsByKey.get(aggregateKey).getOrElse(Array::empty);
-
- return alternativeImplementations.map(altImpl -> Tuple.of(altImpl, alternativeResults.find(r -> altImpl.equals(r.getImplementation()))))
- .map(alt -> Tuple.of(alt._1, calculateRatioStr(result, alt._2)))
- .map(alt -> padRight(alt._2, altImplColSize(alt._1)))
- .mkString(" ");
- }
-
- private String calculateRatioStr(TestExecution baseResult, Option alternativeResult) {
- if (!alternativeResult.isDefined()) {
- return "";
- }
- final double alternativeScore = alternativeResult.get().getScore();
- if (alternativeScore == 0.0) {
- return "";
- }
- final double ratio = baseResult.getScore() / alternativeScore;
- return ratio == 1.0 ? "" : PERFORMANCE_FORMAT.format(ratio) + "×";
- }
- }
-
- private class RatioPerformanceReport {
- private final Map> resultsByKey;
- private final int groupSize;
- private final int nameSize;
- private final Array paramKeys;
- private final int paramKeySize;
- private final Array alternativeImplementations;
- private final int alternativeImplSize;
- private final int ratioSize;
- private final Array targetImplementations;
- private final String targetImplementation;
-
- public RatioPerformanceReport(Array results, String targetImplementation) {
- this.targetImplementation = targetImplementation;
-
- resultsByKey = results.groupBy(TestExecution::getTestNameKey);
- groupSize = Math.max(results.map(r -> r.getTarget().length()).max().get(), 10);
- nameSize = Math.max(results.map(r -> r.getOperation().length()).max().get(), 9);
-
- paramKeys = results.map(TestExecution::getParamKey).distinct().sorted(TO_STRING_COMPARATOR);
- paramKeySize = Math.max(results.map(r -> r.getParamKey().length()).max().get(), 8);
-
- alternativeImplementations = results.map(TestExecution::getImplementation).distinct();
- targetImplementations = alternativeImplementations.filter(i -> i.toLowerCase().contains(targetImplementation.toLowerCase()));
- alternativeImplSize = Math.max(alternativeImplementations.map(String::length).max().getOrElse(0), 10);
-
- final int targetImplSize = Math.max(targetImplementations.map(String::length).max().getOrElse(0), 10);
- ratioSize = Math.max(targetImplSize + 1 + alternativeImplSize, 10);
- }
-
- public void print() {
- printHeader();
- printReport();
- }
-
- private void printHeader() {
- System.out.println("\n\n");
- System.out.println("Performance Ratios");
- System.out.println(CharSeq.of("=").repeat(ratioHeaderNumerator().length()));
- if (outlierLowPct > 0.0 && outlierHighPct > 0.0) {
- System.out.println(String.format(" (Outliers removed: %s low end, %s high end)", PCT_FORMAT.format(outlierLowPct), PCT_FORMAT.format(outlierHighPct)));
- }
- }
-
- private String ratioHeaderNumerator() {
- final String paramKeyHeader = paramKeys.map(type -> padRight(type, paramKeySize)).mkString(" ");
- return String.format("%s %s %s %s ",
- padLeft("Target", groupSize),
- padLeft("Operation", nameSize),
- padLeft("Ratio", ratioSize),
- paramKeyHeader
- );
- }
-
- private String ratioHeaderDenominator() {
- final String paramKeyHeader = paramKeys.map(type -> padRight(type, paramKeySize)).mkString(" ");
- return String.format("%s %s %s %s ",
- padLeft("Target", groupSize),
- padLeft("Operation", nameSize),
- padRight("Ratio", ratioSize),
- paramKeyHeader
- );
- }
-
- private void printReport() {
- if (alternativeImplementations.size() < 2) {
- System.out.println("(nothing to report, you need at least two different implementation)");
- return;
- }
-
- printTargetInDenominator();
- printTargetInNumerator();
-
- System.out.println("\n");
- }
-
- @SuppressWarnings("Convert2MethodRef")
- private void printTargetInNumerator() {
- System.out.println(String.format("\nRatios %s / ", targetImplementation));
- System.out.println(ratioHeaderNumerator());
- for (String targetImpl : targetImplementations) {
- for (Tuple2> execution : resultsByKey) {
- printRatioForBaseType(targetImpl, execution._2,
- (baseImpl, alternativeImpl) -> padLeft(String.format("%s/%s", baseImpl, alternativeImpl), ratioSize),
- (baseExec, alternativeExec) -> calculateRatios(baseExec, alternativeExec));
- }
- }
- }
-
- private void printTargetInDenominator() {
- System.out.println(String.format("\nRatios / %s", targetImplementation));
- System.out.println(ratioHeaderDenominator());
- for (String targetImpl : targetImplementations) {
- for (Tuple2> execution : resultsByKey) {
- printRatioForBaseType(targetImpl, execution._2,
- (baseImpl, alternativeImpl) -> padRight(String.format("%s/%s", alternativeImpl, baseImpl), ratioSize),
- (baseExec, alternativeExec) -> calculateRatios(alternativeExec, baseExec));
- }
- }
- }
-
- private void printRatioForBaseType(String baseType, Array testExecutions,
- BiFunction ratioNamePrinter,
- BiFunction, Array, String> ratioCalculator) {
- final Array baseImplExecutions = testExecutions.filter(e -> e.getImplementation().equals(baseType));
- if (baseImplExecutions.isEmpty()) {
- return;
- }
- final TestExecution baseTypeExecution = baseImplExecutions.head();
-
- for (String alternativeImpl : alternativeImplementations) {
- if (alternativeImpl.equals(baseType)) {
- continue;
- }
- final Array alternativeExecutions = testExecutions.filter(e -> e.getImplementation().equals(alternativeImpl));
- if (alternativeExecutions.isEmpty()) {
- continue;
- }
- System.out.println(String.format("%s %s %s %s",
- padLeft(baseTypeExecution.getTarget(), groupSize),
- padLeft(baseTypeExecution.getOperation(), nameSize),
- ratioNamePrinter.apply(baseType, alternativeImpl),
- ratioCalculator.apply(baseImplExecutions, alternativeExecutions)));
- }
- System.out.println();
- }
-
- private String calculateRatios(Array alternativeExecutions, Array baseImplExecutions) {
- Array ratioStings = Array.empty();
- for (String paramKey : paramKeys) {
- final Option alternativeExecution = alternativeExecutions.find(e -> e.getParamKey().equals(paramKey));
- final Option baseExecution = baseImplExecutions.find(e -> e.getParamKey().equals(paramKey));
- final String paramRatio = alternativeExecution.isEmpty() || baseExecution.isEmpty() || baseExecution.get().getScore() == 0.0
- ? ""
- : PERFORMANCE_FORMAT.format(alternativeExecution.get().getScore() / baseExecution.get().getScore()) + "×";
- ratioStings = ratioStings.append(padRight(paramRatio, paramKeySize));
- }
- return ratioStings.mkString(" ");
- }
- }
-
- public static class TestExecution implements Comparable {
- private static double outlierLowPct;
- private static double outlierHighPct;
- private final String paramKey;
- private final String fullName;
- private final String target;
- private final String operation;
- private final String implementation;
- private final long sampleCount;
- private final double score;
- private final double scoreError;
- private final String unit;
-
- public static TestExecution of(BenchmarkResult benchmarkResult, double outlierLowPct, double outlierHighPct) {
- TestExecution.outlierLowPct = outlierLowPct;
- TestExecution.outlierHighPct = outlierHighPct;
- return new TestExecution(benchmarkResult);
- }
-
- public TestExecution(BenchmarkResult benchmark) {
- final Result> primaryResult = benchmark.getPrimaryResult();
- fullName = benchmark.getParams().getBenchmark();
- target = extractPart(fullName, 2);
- operation = extractPart(fullName, 1);
- implementation = extractPart(fullName, 0);
- paramKey = getParameterKey(benchmark);
-
- final ListStatistics statistics = createStatisticsWithoutOutliers(benchmark, outlierLowPct, outlierHighPct);
- sampleCount = statistics.getN();
- score = statistics.getMean();
- scoreError = statistics.getMeanErrorAt(0.999);
- unit = primaryResult.getScoreUnit();
- }
-
- private ListStatistics createStatisticsWithoutOutliers(BenchmarkResult benchmark, double outlierLowPct, double outlierHighPct) {
- Array results = benchmark.getIterationResults().stream()
- .map(r -> r.getPrimaryResult().getScore())
- .collect(Array.collector());
- final int size = results.size();
- final int outliersLow = (int) (size * outlierLowPct);
- final int outliersHigh = (int) (size * outlierHighPct);
- results = results.drop(outliersLow).dropRight(outliersHigh);
- return new ListStatistics(results.toJavaList().stream().mapToDouble(r -> r).toArray());
- }
-
- private String getParameterKey(BenchmarkResult benchmarkResult) {
- final BenchmarkParams params = benchmarkResult.getParams();
- return params.getParamsKeys().stream().map(params::getParam).collect(Collectors.joining(";"));
- }
-
- public String getTestNameParamKey() {
- return target + ":" + operation + ":" + unit + ":" + paramKey;
- }
-
- public String getTestNameKey() {
- return target + ":" + operation + ":" + unit;
- }
-
- private String extractPart(String fullyQualifiedName, int indexFromLast) {
- final String[] parts = fullyQualifiedName.split("\\.");
- return parts.length > indexFromLast ? parts[parts.length - indexFromLast - 1] : "";
- }
-
- public String getParamKey() {
- return paramKey;
- }
-
- public String getTarget() {
- return target;
- }
-
- public String getOperation() {
- return operation;
- }
-
- public String getImplementation() {
- return implementation;
- }
-
- public long getSampleCount() {
- return sampleCount;
- }
-
- public double getScore() {
- return score;
- }
-
- public String getScoreFormatted() {
- return DECIMAL_FORMAT.format(score);
- }
-
- public String getScoreErrorPct() {
- return PCT_FORMAT.format(score == 0 ? 0 : scoreError / score);
- }
-
- public String getUnit() {
- return unit;
- }
-
- @Override
- public String toString() {
- return String.format("%s %s %s %s -> %s (± %s)", paramKey, target, operation, implementation, getScoreFormatted(), getScoreErrorPct());
- }
-
- Comparator comparator = Comparator
- .comparing(TestExecution::getUnit)
- .thenComparing(TestExecution::getTarget)
- .thenComparing(TestExecution::getParamKey)
- .thenComparing(TestExecution::getOperation)
- .thenComparing(TestExecution::getImplementation);
-
- @Override
- public int compareTo(TestExecution o) {
- return comparator.compare(this, o);
- }
- }
-}
diff --git a/vavr-benchmark/src/test/java/io/vavr/JmhRunner.java b/vavr-benchmark/src/test/java/io/vavr/JmhRunner.java
deleted file mode 100644
index 1a55acf277..0000000000
--- a/vavr-benchmark/src/test/java/io/vavr/JmhRunner.java
+++ /dev/null
@@ -1,226 +0,0 @@
-/* ____ ______________ ________________________ __________
- * \ \/ / \ \/ / __/ / \ \/ / \
- * \______/___/\___\______/___/_____/___/\___\______/___/\___\
- *
- * Copyright 2014-2025 Vavr, https://vavr.io
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.vavr;
-
-import io.vavr.collection.*;
-import io.vavr.control.LazyBenchmark;
-import io.vavr.idiom.ForBenchmark;
-import io.vavr.idiom.PatternMatchingBenchmark;
-import io.vavr.idiom.TryBenchmark;
-import io.vavr.idiom.TupleBenchmark;
-import org.openjdk.jmh.annotations.Mode;
-import org.openjdk.jmh.results.RunResult;
-import org.openjdk.jmh.runner.Runner;
-import org.openjdk.jmh.runner.RunnerException;
-import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
-import org.openjdk.jmh.runner.options.OptionsBuilder;
-import org.openjdk.jmh.runner.options.TimeValue;
-import org.openjdk.jmh.runner.options.VerboseMode;
-
-import java.util.Collection;
-import java.util.Random;
-import java.util.concurrent.TimeUnit;
-
-import static io.vavr.API.Array;
-
-public class JmhRunner {
- /**
- * Runs all the available benchmarks in precision mode.
- * Note: it takes about 3 hours.
- */
- public static void main(String[] args) {
- final Array> CLASSES = Array(
- ArrayBenchmark.class,
- BitSetBenchmark.class,
- CharSeqBenchmark.class,
- HashSetBenchmark.class,
- ListBenchmark.class,
- MapBenchmark.class,
- PriorityQueueBenchmark.class,
- VectorBenchmark.class,
-
- LazyBenchmark.class,
-
- ForBenchmark.class,
- PatternMatchingBenchmark.class,
- TryBenchmark.class,
- TupleBenchmark.class
- );
- runDebugWithAsserts(CLASSES);
- runSlowNoAsserts(CLASSES);
- }
-
- public enum Includes {
- JAVA("java"),
- FUNCTIONAL_JAVA("fjava"),
- PCOLLECTIONS("pcollections"),
- ECOLLECTIONS("ecollections"),
- CAPSULE("capsule"),
- CLOJURE("clojure"),
- SCALAZ("scalaz"),
- SCALA("scala"),
- VAVR("vavr");
-
- private final String name;
-
- Includes(String name) { this.name = name; }
-
- @Override
- public String toString() { return name; }
- }
-
- /** enables debugging and assertions for benchmarks and production code - the speed results will be totally unreliable */
- public static void runDebugWithAsserts(Array> groups, Includes... includes) {
- run(0, 1, 1, ForkJvm.DISABLE, VerboseMode.SILENT, Assertions.ENABLE, PrintInlining.DISABLE, groups, includes);
- MemoryUsage.printAndReset();
- }
-
- @SuppressWarnings("unused")
- public static void runQuickNoAsserts(Array> groups, Includes... includes) {
- run(5, 5, 10, ForkJvm.ENABLE, VerboseMode.NORMAL, Assertions.DISABLE, PrintInlining.DISABLE, groups, includes).print();
- }
-
- @SuppressWarnings("unused")
- public static void runNormalNoAsserts(Array> groups, Includes... includes) {
- run(7, 7, 300, ForkJvm.ENABLE, VerboseMode.NORMAL, Assertions.DISABLE, PrintInlining.DISABLE, groups, includes).print();
- }
-
- @SuppressWarnings("unused")
- public static void runSlowNoAsserts(Array> groups, Includes... includes) {
- run(15, 15, 400, ForkJvm.ENABLE, VerboseMode.EXTRA, Assertions.DISABLE, PrintInlining.DISABLE, groups, includes).print();
- }
-
- private static BenchmarkPerformanceReporter run(int warmupIterations, int measurementIterations, int millis, ForkJvm forkJvm, VerboseMode silent, Assertions assertions, PrintInlining printInlining, Array> groups, Includes[] includes) {
- final Array includeNames = Array.of(includes.length == 0 ? Includes.values() : includes).map(Includes::toString);
- final Array classNames = groups.map(Class::getCanonicalName);
- final Array results = run(warmupIterations, measurementIterations, millis, forkJvm, silent, assertions, printInlining, classNames, includeNames);
- return BenchmarkPerformanceReporter.of(includeNames, classNames, results);
- }
-
- private static Array run(int warmupIterations, int measurementIterations, int millis, ForkJvm forkJvm, VerboseMode verboseMode, Assertions assertions, PrintInlining printInlining, Array classNames, Array includeNames) {
- try {
- final ChainedOptionsBuilder builder = new OptionsBuilder()
- .shouldDoGC(true)
- .verbosity(verboseMode)
- .shouldFailOnError(true)
- .mode(Mode.Throughput)
- .timeUnit(TimeUnit.SECONDS)
- .warmupTime(TimeValue.milliseconds(millis))
- .warmupIterations(warmupIterations)
- .measurementTime(TimeValue.milliseconds(millis))
- .measurementIterations(measurementIterations)
- .forks(forkJvm.forkCount)
- /* We are using 4Gb and setting NewGen to 100% to avoid GC during testing.
- Any GC during testing will destroy the iteration (i.e. introduce unreliable noise in the measurement), which should get ignored as an outlier */
- .jvmArgsAppend("-XX:+UseG1GC", "-Xss100m", "-Xms4g", "-Xmx4g", "-XX:MaxGCPauseMillis=1000", "-XX:+UnlockExperimentalVMOptions", "-XX:G1NewSizePercent=100", "-XX:G1MaxNewSizePercent=100", assertions.vmArg);
-
- final String includePattern = includeNames.mkString("\\..*?\\b(", "|", ")_");
- classNames.forEach(name -> builder.include(name + includePattern));
-
- if (printInlining == PrintInlining.ENABLE) {
- builder.jvmArgsAppend("-XX:+UnlockDiagnosticVMOptions", "-XX:+PrintInlining"); /* might help in deciding when the JVM is properly warmed up - or where to optimize the code */
- }
-
- return Array.ofAll(new Runner(builder.build()).run());
- } catch (RunnerException e) {
- throw new RuntimeException(e);
- }
- }
-
- /* Options */
- private enum ForkJvm {
- ENABLE(1),
- DISABLE(0);
-
- final int forkCount;
-
- ForkJvm(int forkCount) {
- this.forkCount = forkCount;
- }
- }
-
- private enum Assertions {
- ENABLE("-enableassertions"),
- DISABLE("-disableassertions");
-
- final String vmArg;
-
- Assertions(String vmArg) {
- this.vmArg = vmArg;
- }
- }
-
- private enum PrintInlining {
- ENABLE,
- DISABLE
- }
-
- /* Helper methods */
-
- public static Integer[] getRandomValues(int size, int seed) {
- return getRandomValues(size, seed, false);
- }
-
- public static Integer[] getRandomValues(int size, int seed, boolean nonNegative) {
- return getRandomValues(size, nonNegative, new Random(seed));
- }
-
- public static Integer[] getRandomValues(int size, boolean nonNegative, Random random) {
- final Integer[] results = new Integer[size];
- for (int i = 0; i < size; i++) {
- final int value = random.nextInt(size) - (nonNegative ? 0 : (size / 2));
- results[i] = value;
- }
- return results;
- }
-
- /** Randomly mutate array positions */
- public static int[] shuffle(int[] array, Random random) {
- for (int i = array.length; i > 1; i--) {
- swap(array, i - 1, random.nextInt(i));
- }
- return array;
- }
-
- static void swap(int[] array, int i, int j) {
- final int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
-
- /** used for dead code elimination and correctness assertion inside the benchmarks */
- public static int aggregate(int x, int y) {
- return x ^ y;
- }
-
- /** simplifies construction of immutable collections - with assertion and memory benchmarking */
- public static , R> R create(Function1 function, T source, Function1 assertion) {
- return create(function, source, source.size(), assertion);
- }
-
- @SuppressWarnings("unchecked")
- public static R create(Function1 function, T source, int elementCount, Function1 assertion) {
- final R result = function.apply(source);
- assert assertion.apply(result);
-
- MemoryUsage.storeMemoryUsages(elementCount, result);
-
- return result;
- }
-}
diff --git a/vavr-benchmark/src/test/java/io/vavr/MemoryUsage.java b/vavr-benchmark/src/test/java/io/vavr/MemoryUsage.java
deleted file mode 100644
index 421d63678a..0000000000
--- a/vavr-benchmark/src/test/java/io/vavr/MemoryUsage.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/* ____ ______________ ________________________ __________
- * \ \/ / \ \/ / __/ / \ \/ / \
- * \______/___/\___\______/___/_____/___/\___\______/___/\___\
- *
- * Copyright 2014-2025 Vavr, https://vavr.io
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.vavr;
-
-import io.vavr.collection.*;
-import org.openjdk.jol.info.GraphLayout;
-
-import java.text.DecimalFormat;
-import java.util.function.Predicate;
-import java.util.regex.Pattern;
-
-import static java.lang.Math.max;
-
-public class MemoryUsage {
- private static final DecimalFormat FORMAT = new DecimalFormat("#,##0");
- private static Map>> memoryUsages = TreeMap.empty(); // if forked, this will be reset every time
-
- /** Calculate the occupied memory of different internals */
- static void printAndReset() {
- for (Tuple2>> entry : memoryUsages) {
- final Seq columnSizes = columnSizes(entry._1);
- System.out.println(String.format("\nfor `%d` elements", entry._1));
- for (Seq stats : entry._2) {
- final String format = String.format(" %s → %s bytes",
- stats.get(0).padTo(columnSizes.get(0), ' '),
- stats.get(1).leftPadTo(columnSizes.get(1), ' ')
- );
- System.out.println(format);
- }
- }
-
- memoryUsages = memoryUsages.take(0); // reset
- }
- private static Seq columnSizes(int size) {
- return memoryUsages.get(size)
- .map(rows -> rows.map(row -> row.map(CharSeq::length))).get()
- .reduce((row1, row2) -> row1.zip(row2).map(ts -> max(ts._1, ts._2)));
- }
-
- static void storeMemoryUsages(int elementCount, Object target) {
- memoryUsages = memoryUsages.put(elementCount, memoryUsages.get(elementCount).getOrElse(LinkedHashSet.empty()).add(Array.of(
- toHumanReadableName(target),
- toHumanReadableByteSize(target)
- ).map(CharSeq::of)));
- }
- private static String toHumanReadableByteSize(Object target) { return FORMAT.format(byteSize(target)); }
- private static long byteSize(Object target) { return GraphLayout.parseInstance(target).totalSize(); }
-
- private static HashMap, String> names = HashMap.ofEntries(
- Tuple.of("^java\\.", "Java mutable @ "),
- Tuple.of("^fj\\.", "Functional Java persistent @ "),
- Tuple.of("^org\\.pcollections", "PCollections persistent @ "),
- Tuple.of("^org\\.eclipse\\.collections", "Eclipse Collections persistent @ "),
- Tuple.of("^clojure\\.", "Clojure persistent @ "),
- Tuple.of("^scalaz\\.Heap", "Scalaz persistent @ "),
- Tuple.of("^scala\\.collection.immutable", "Scala persistent @ "),
- Tuple.of("^scala\\.collection.mutable", "Scala mutable @ "),
- Tuple.of("^io\\.usethesource", "Capsule persistent @ "),
- Tuple.of("^io\\.vavr\\.", "Vavr persistent @ ")
- ).mapKeys(r -> Pattern.compile(r).asPredicate());
- private static String toHumanReadableName(Object target) {
- final Class> type = target.getClass();
- return prefix(type) + type.getSimpleName();
- }
- private static String prefix(Class> type) { return names.find(p -> p._1.test(type.getName())).get()._2; }
-}
diff --git a/vavr-benchmark/src/test/java/io/vavr/collection/ArrayBenchmark.java b/vavr-benchmark/src/test/java/io/vavr/collection/ArrayBenchmark.java
deleted file mode 100644
index 3f93867697..0000000000
--- a/vavr-benchmark/src/test/java/io/vavr/collection/ArrayBenchmark.java
+++ /dev/null
@@ -1,419 +0,0 @@
-/* ____ ______________ ________________________ __________
- * \ \/ / \ \/ / __/ / \ \/ / \
- * \______/___/\___\______/___/_____/___/\___\______/___/\___\
- *
- * Copyright 2014-2025 Vavr, https://vavr.io
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.vavr.collection;
-
-import io.vavr.JmhRunner;
-import org.junit.jupiter.api.Test;
-import org.openjdk.jmh.annotations.*;
-
-import java.util.ArrayList;
-import java.util.Objects;
-
-import static java.util.Arrays.asList;
-import static io.vavr.JmhRunner.create;
-import static io.vavr.JmhRunner.getRandomValues;
-import static io.vavr.collection.Collections.areEqual;
-
-public class ArrayBenchmark {
- static final Array> CLASSES = Array.of(
- Create.class,
- Head.class,
- Tail.class,
- Get.class,
- Update.class,
- Prepend.class,
- Append.class,
- Iterate.class
- , Fill.class
- );
-
- @Test
- public void testAsserts() {
- JmhRunner.runDebugWithAsserts(CLASSES);
- }
-
- public static void main(String... args) {
- JmhRunner.runNormalNoAsserts(CLASSES);
- }
-
- @State(Scope.Benchmark)
- public static class Base {
- @Param({"10", "100", "1000", "2500"})
- public int CONTAINER_SIZE;
-
- int EXPECTED_AGGREGATE;
- Integer[] ELEMENTS;
-
- java.util.ArrayList javaMutable;
- fj.data.Array fjavaMutable;
- io.vavr.collection.Array vavrPersistent;
- org.pcollections.PVector pcollVector;
-
-
- @Setup
- public void setup() {
- ELEMENTS = getRandomValues(CONTAINER_SIZE, 0);
- EXPECTED_AGGREGATE = Iterator.of(ELEMENTS).reduce(JmhRunner::aggregate);
-
- javaMutable = create(java.util.ArrayList::new, asList(ELEMENTS), v -> areEqual(v, asList(ELEMENTS)));
- fjavaMutable = create(fj.data.Array::array, ELEMENTS, ELEMENTS.length, v -> areEqual(v, asList(ELEMENTS)));
- vavrPersistent = create(io.vavr.collection.Array::ofAll, javaMutable, v -> areEqual(v, javaMutable));
- pcollVector = create(org.pcollections.TreePVector::from, javaMutable, v -> areEqual(v, javaMutable));
- }
- }
-
- public static class Create extends Base {
- @Benchmark
- public Object java_mutable() {
- final ArrayList values = new ArrayList<>(javaMutable);
- assert areEqual(values, javaMutable);
- return values;
- }
-
- @Benchmark
- public Object fjava_persistent() {
- final fj.data.Array values = fj.data.Array.iterableArray(javaMutable);
- assert areEqual(values, fjavaMutable);
- return values;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- final io.vavr.collection.Array values = io.vavr.collection.Array.ofAll(javaMutable);
- assert areEqual(values, vavrPersistent);
- return values.head();
- }
-
- @Benchmark
- public Object pcoll_vector() {
- final org.pcollections.PVector values = org.pcollections.TreePVector.from(javaMutable);
- assert areEqual(values, pcollVector);
- return values;
- }
- }
-
- public static class Head extends Base {
- @Benchmark
- public Object java_mutable() {
- final Object head = javaMutable.get(0);
- assert Objects.equals(head, ELEMENTS[0]);
- return head;
- }
-
- @Benchmark
- public Object fjava_mutable() {
- final Object head = fjavaMutable.get(0);
- assert Objects.equals(head, ELEMENTS[0]);
- return head;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- final Object head = vavrPersistent.get(0);
- assert Objects.equals(head, ELEMENTS[0]);
- return head;
- }
-
- @Benchmark
- public Object pcoll_vector() {
- final Object head = pcollVector.get(0);
- assert Objects.equals(head, ELEMENTS[0]);
- return head;
- }
- }
-
- @SuppressWarnings("Convert2MethodRef")
- public static class Tail extends Base {
- @State(Scope.Thread)
- public static class Initialized {
- final java.util.ArrayList javaMutable = new java.util.ArrayList<>();
-
- @Setup(Level.Invocation)
- public void initializeMutable(Base state) {
- java.util.Collections.addAll(javaMutable, state.ELEMENTS);
- assert areEqual(javaMutable, asList(state.ELEMENTS));
- }
-
- @TearDown(Level.Invocation)
- public void tearDown() {
- javaMutable.clear();
- }
- }
-
- @Benchmark
- public Object java_mutable(Initialized state) {
- final java.util.ArrayList values = state.javaMutable;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- values.remove(0);
- }
- assert values.isEmpty();
- return values;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- io.vavr.collection.Array values = vavrPersistent;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- values = values.tail();
- }
- assert values.isEmpty();
- return values;
- }
-
- @Benchmark
- public Object pcoll_vector() {
- org.pcollections.PVector values = pcollVector;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- values = values.minus(1);
- }
- assert values.isEmpty();
- return values;
- }
- }
-
- public static class Get extends Base {
- @Benchmark
- public int java_mutable() {
- int aggregate = 0;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- aggregate ^= javaMutable.get(i);
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
-
- @Benchmark
- public int fjava_mutable() {
- int aggregate = 0;
- for (int i = 0; i < ELEMENTS.length; i++) {
- aggregate ^= fjavaMutable.get(i);
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
-
- @Benchmark
- public int vavr_persistent() {
- int aggregate = 0;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- aggregate ^= vavrPersistent.get(i);
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
-
- @Benchmark
- public Object pcoll_vector() {
- int aggregate = 0;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- aggregate ^= pcollVector.get(i);
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
- }
-
- public static class Update extends Base {
- @Benchmark
- public Object java_mutable() {
- final java.util.ArrayList values = javaMutable;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- values.set(i, 0);
- }
- assert Iterator.ofAll(values).forAll(e -> e == 0);
- return javaMutable;
- }
-
- @Benchmark
- public Object fjava_mutable() {
- final fj.data.Array values = fjavaMutable;
- for (int i = 0; i < ELEMENTS.length; i++) {
- values.set(i, 0);
- }
- assert values.forall(e -> e == 0);
- return fjavaMutable;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- io.vavr.collection.Array values = vavrPersistent;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- values = values.update(i, 0);
- }
- assert values.forAll(e -> e == 0);
- return values;
- }
-
- @Benchmark
- public Object pcoll_vector() {
- org.pcollections.PVector values = pcollVector;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- values = values.with(i, 0);
- }
- assert Iterator.ofAll(values).forAll(e -> e == 0);
- return values;
- }
- }
-
- public static class Prepend extends Base {
- @Benchmark
- public Object java_mutable() {
- final java.util.ArrayList values = new java.util.ArrayList<>(CONTAINER_SIZE);
- for (Integer element : ELEMENTS) {
- values.add(0, element);
- }
- assert areEqual(List.ofAll(values).reverse(), javaMutable);
- return values;
- }
-
- @Benchmark
- public Object fjava_mutable() {
- fj.data.Array values = fj.data.Array.empty();
- for (Integer element : ELEMENTS) {
- values = fj.data.Array.array(element).append(values);
- }
- assert areEqual(values.reverse(), javaMutable);
- return values;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- io.vavr.collection.Array values = io.vavr.collection.Array.empty();
- for (Integer element : ELEMENTS) {
- values = values.prepend(element);
- }
- assert areEqual(values.reverse(), javaMutable);
- return values;
- }
-
- @Benchmark
- public Object pcoll_vector() {
- org.pcollections.PVector values = org.pcollections.TreePVector.empty();
- for (Integer element : ELEMENTS) {
- values = values.plus(0, element);
- }
- assert areEqual(List.ofAll(values).reverse(), javaMutable);
- return values;
- }
- }
-
- public static class Append extends Base {
- @SuppressWarnings("ManualArrayToCollectionCopy")
- @Benchmark
- public Object java_mutable() {
- final java.util.ArrayList values = new java.util.ArrayList<>(CONTAINER_SIZE);
- for (Integer element : ELEMENTS) {
- values.add(element);
- }
- assert areEqual(values, javaMutable);
- return values;
- }
-
- @Benchmark
- public Object fjava_mutable() {
- fj.data.Array values = fj.data.Array.empty();
- for (Integer element : ELEMENTS) {
- values = values.append(fj.data.Array.array(element));
- }
- assert areEqual(values, javaMutable);
- return values;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- io.vavr.collection.Array values = io.vavr.collection.Array.empty();
- for (Integer element : ELEMENTS) {
- values = values.append(element);
- }
- assert areEqual(values, javaMutable);
- return values;
- }
-
- @Benchmark
- public Object pcoll_vector() {
- org.pcollections.PVector values = org.pcollections.TreePVector.empty();
- for (Integer element : ELEMENTS) {
- values = values.plus(element);
- }
- assert areEqual(values, javaMutable);
- return values;
- }
- }
-
- @SuppressWarnings("ForLoopReplaceableByForEach")
- public static class Iterate extends Base {
- @Benchmark
- public int java_mutable() {
- int aggregate = 0;
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- aggregate ^= javaMutable.get(i);
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
-
- @Benchmark
- public int fjava_mutable() {
- int aggregate = 0;
- for (final java.util.Iterator iterator = fjavaMutable.iterator(); iterator.hasNext(); ) {
- aggregate ^= iterator.next();
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
-
- @Benchmark
- public int vavr_persistent() {
- int aggregate = 0;
- for (final Iterator iterator = vavrPersistent.iterator(); iterator.hasNext(); ) {
- aggregate ^= iterator.next();
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
-
- @Benchmark
- public int pcoll_vector() {
- int aggregate = 0;
- for (final java.util.Iterator iterator = pcollVector.iterator(); iterator.hasNext(); ) {
- aggregate ^= iterator.next();
- }
- assert aggregate == EXPECTED_AGGREGATE;
- return aggregate;
- }
- }
-
- public static class Fill extends Base {
- @Benchmark
- public Object vavr_persistent_constant_supplier() {
- final io.vavr.collection.Array values = io.vavr.collection.Array.fill(CONTAINER_SIZE, () -> ELEMENTS[0]);
- final Integer head = values.head();
- assert Objects.equals(head, ELEMENTS[0]);
- return head;
- }
-
- @Benchmark
- public Object vavr_persistent_constant_object() {
- final io.vavr.collection.Array values = io.vavr.collection.Array.fill(CONTAINER_SIZE, ELEMENTS[0]);
- final Integer head = values.head();
- assert Objects.equals(head, ELEMENTS[0]);
- return head;
- }
- }
-}
diff --git a/vavr-benchmark/src/test/java/io/vavr/collection/BitSetBenchmark.java b/vavr-benchmark/src/test/java/io/vavr/collection/BitSetBenchmark.java
deleted file mode 100644
index a519777dd8..0000000000
--- a/vavr-benchmark/src/test/java/io/vavr/collection/BitSetBenchmark.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/* ____ ______________ ________________________ __________
- * \ \/ / \ \/ / __/ / \ \/ / \
- * \______/___/\___\______/___/_____/___/\___\______/___/\___\
- *
- * Copyright 2014-2025 Vavr, https://vavr.io
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.vavr.collection;
-
-import io.vavr.JmhRunner;
-import org.junit.jupiter.api.Test;
-import org.openjdk.jmh.annotations.*;
-
-import static io.vavr.JmhRunner.create;
-import static io.vavr.JmhRunner.getRandomValues;
-import static io.vavr.collection.Collections.areEqual;
-import static scala.collection.JavaConverters.asJavaCollection;
-import static scala.collection.JavaConverters.asScalaBuffer;
-
-public class BitSetBenchmark {
- static final Array> CLASSES = Array.of(
- AddAll.class,
- Iterate.class
- );
-
- @Test
- public void testAsserts() {
- JmhRunner.runDebugWithAsserts(CLASSES);
- }
-
- public static void main(String... args) {
- JmhRunner.runNormalNoAsserts(CLASSES);
- }
-
- @State(Scope.Benchmark)
- public static class Base {
- @Param({"10", "100", "1000", "2500"})
- public int CONTAINER_SIZE;
-
- int EXPECTED_AGGREGATE;
- int[] ELEMENTS;
- TreeSet DISTINCT;
-
- scala.collection.immutable.BitSet scalaPersistent;
- io.vavr.collection.BitSet vavrPersistent;
-
- @Setup
- @SuppressWarnings("RedundantCast")
- public void setup() {
- final Integer[] values = getRandomValues(CONTAINER_SIZE, 0, true);
- ELEMENTS = new int[CONTAINER_SIZE];
- for (int i = 0; i < CONTAINER_SIZE; i++) {
- ELEMENTS[i] = values[i];
- }
-
- DISTINCT = TreeSet.ofAll(ELEMENTS);
- EXPECTED_AGGREGATE = DISTINCT.reduce(JmhRunner::aggregate);
-
- scalaPersistent = create(v -> (scala.collection.immutable.BitSet) scala.collection.immutable.BitSet$.MODULE$.apply(asScalaBuffer(v)), DISTINCT.toJavaList(), v -> areEqual(asJavaCollection(v), DISTINCT));
- vavrPersistent = create(io.vavr.collection.BitSet::ofAll, ELEMENTS, ELEMENTS.length, v -> areEqual(v, DISTINCT));
- }
- }
-
- public static class AddAll extends Base {
- @Benchmark
- public Object scala_persistent() {
- scala.collection.immutable.BitSet values = new scala.collection.immutable.BitSet.BitSet1(0L);
- for (int element : ELEMENTS) {
- values = values.$plus(element);
- }
- assert values.equals(scalaPersistent);
- return values;
- }
-
- @Benchmark
- public Object vavr_persistent() {
- io.vavr.collection.Set values = io.vavr.collection.BitSet.empty();
- for (Integer element : ELEMENTS) {
- values = values.add(element);
- }
- assert values.equals(vavrPersistent);
- return values;
- }
- }
-
- @SuppressWarnings("ForLoopReplaceableByForEach")
- public static class Iterate extends Base {
- @Benchmark
- public int scala_persistent() {
- int aggregate = 0;
- for (final scala.collection.Iterator