Skip to content

Commit

Permalink
testing with benchmark, it is just faster... even with length 10
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
wagyourtail committed Feb 3, 2025
1 parent 0e55409 commit b8e8c5b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@

public class J_U_S_Stream {

private static final Class<?> REF_PIPELINE;

static {
try {
REF_PIPELINE = Class.forName("java.util.stream.ReferencePipeline");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@Stub
public static <T, R> Stream<R> mapMulti(Stream<T> stream, BiConsumer<? super T, ? super Consumer<R>> mapper) {
return stream.flatMap(new MapMultiConsumer<>(mapper));
Expand All @@ -37,7 +47,12 @@ public static <T> DoubleStream mapMultiToDouble(Stream<T> stream, BiConsumer<? s

@Stub
public static <T> List<T> toList(Stream<T> stream) {
return (List<T>) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(stream.toArray())));
List unsafeList = Arrays.asList(stream.toArray());
if (REF_PIPELINE.isAssignableFrom(stream.getClass())) {
return Collections.unmodifiableList(unsafeList);
} else {
return Collections.unmodifiableList(new ArrayList<>(unsafeList));
}
}

public static class MapMultiConsumer<T, R> implements Function<T, Stream<R>> {
Expand Down
10 changes: 10 additions & 0 deletions testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
java
id("me.champeau.jmh") version "0.7.2"
}

val testVersion = JavaVersion.toVersion(project.properties["testVersion"] as String)
Expand Down Expand Up @@ -38,6 +39,15 @@ dependencies {

val testTargetVersion = JavaVersion.toVersion(project.properties["testTargetVersion"] as String)

jmh {
warmupIterations = 2
iterations = 2
fork = 2
// javaLauncher = javaToolchains.launcherFor {
// languageVersion.set(JavaLanguageVersion.of(8))
// }
}

tasks.test {
useJUnitPlatform()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xyz.wagyourtail.jvmdg.benchmarks;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@State(Scope.Benchmark)
public class StreamToListTest {

private static final Class<?> REF_PIPELINE;

static {
try {
REF_PIPELINE = Class.forName("java.util.stream.ReferencePipeline");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

@Benchmark
public void testNewArrayList10(Blackhole bh) {
Stream<Integer> s = IntStream.rangeClosed(0, 10).mapToObj(i -> i + 10);

List<Integer> l = (List) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(s.toArray())));

bh.consume(l);
}

@Benchmark
public void testCheckClass10(Blackhole bh) {
Stream<Integer> s = IntStream.rangeClosed(0, 10).mapToObj(i -> i + 10);

List<Integer> l;
if (REF_PIPELINE.isAssignableFrom(s.getClass())) {
l = (List) Collections.unmodifiableList(Arrays.asList(s.toArray()));
} else {
l = (List) Collections.unmodifiableList(new ArrayList<>(Arrays.asList(s.toArray())));
}

bh.consume(l);
}

}

0 comments on commit b8e8c5b

Please sign in to comment.