Skip to content

Commit

Permalink
Optimized creation of distandes list in ShrinkingDistance.combine(..)
Browse files Browse the repository at this point in the history
Working towards #435 effects
  • Loading branch information
jlink committed Dec 25, 2022
1 parent 38c1c9a commit 19ae50e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions api/src/main/java/net/jqwik/api/ShrinkingDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.*;

import org.apiguardian.api.*;
import org.jetbrains.annotations.*;

import static org.apiguardian.api.API.Status.*;

Expand Down Expand Up @@ -41,9 +42,7 @@ public static <T> ShrinkingDistance combine(List<Shrinkable<T>> shrinkables) {
throw new IllegalArgumentException("At least one shrinkable is required");
}
// This is an optimization to avoid creating many temporary arrays, which the old streams-based implementation did.
List<long[]> shrinkableDistances = shrinkables.stream()
.map(tShrinkable -> tShrinkable.distance().distances)
.collect(Collectors.toList());
List<long[]> shrinkableDistances = toDistances(shrinkables);
long[] combinedDistances = concatenate(shrinkableDistances);
return new ShrinkingDistance(combinedDistances);
}
Expand Down Expand Up @@ -120,4 +119,14 @@ public ShrinkingDistance append(ShrinkingDistance other) {
return new ShrinkingDistance(appendedDistances);
}

@NotNull
private static <T> List<long[]> toDistances(List<Shrinkable<T>> shrinkables) {
List<long[]> listOfDistances = new ArrayList<>(shrinkables.size());
for (Shrinkable<?> tShrinkable : shrinkables) {
long[] longs = tShrinkable.distance().distances;
listOfDistances.add(longs);
}
return listOfDistances;
}

}

0 comments on commit 19ae50e

Please sign in to comment.