Skip to content

Commit

Permalink
Optimized 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 88cc8b7 commit 38c1c9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
16 changes: 8 additions & 8 deletions api/src/main/java/net/jqwik/api/ShrinkingDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public static <T> ShrinkingDistance combine(List<Shrinkable<T>> shrinkables) {
if (shrinkables.isEmpty()) {
throw new IllegalArgumentException("At least one shrinkable is required");
}
ShrinkingDistance acc = new ShrinkingDistance(new long[0]);
for (Shrinkable<T> shrinkable : shrinkables) {
ShrinkingDistance distance = shrinkable.distance();
acc = acc.append(distance);
}
return acc;
// 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());
long[] combinedDistances = concatenate(shrinkableDistances);
return new ShrinkingDistance(combinedDistances);
}

private ShrinkingDistance(long[] distances) {
Expand Down Expand Up @@ -110,13 +110,13 @@ private int compareDimension(ShrinkingDistance other, int i) {

@API(status = INTERNAL)
public ShrinkingDistance plus(ShrinkingDistance other) {
long[] summedUpDistances = sumUpArrays(distances, other.distances);
long[] summedUpDistances = sumUp(distances, other.distances);
return new ShrinkingDistance(summedUpDistances);
}

@API(status = INTERNAL)
public ShrinkingDistance append(ShrinkingDistance other) {
long[] appendedDistances = concatArrays(distances, other.distances);
long[] appendedDistances = concatenate(Arrays.asList(distances, other.distances));
return new ShrinkingDistance(appendedDistances);
}

Expand Down
15 changes: 10 additions & 5 deletions api/src/main/java/net/jqwik/api/support/LongArraysSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static long at(long[] array, int i) {
return array.length > i ? array[i] : 0;
}

public static long[] sumUpArrays(long[] left, long[] right) {
public static long[] sumUp(long[] left, long[] right) {
long[] sum = new long[Math.max(left.length, right.length)];
for (int i = 0; i < sum.length; i++) {
long summedValue = at(left, i) + at(right, i);
Expand All @@ -25,9 +25,14 @@ public static long[] sumUpArrays(long[] left, long[] right) {
return sum;
}

public static long[] concatArrays(long[] left, long[] right) {
long[] concatenated = Arrays.copyOf(left, left.length + right.length);
System.arraycopy(right, 0, concatenated, left.length, right.length);
return concatenated;
public static long[] concatenate(List<long[]> listOfArrays) {
int size = listOfArrays.stream().mapToInt(s -> s.length).sum();
long[] concatenatedArrays = new long[size];
int i = 0;
for (long[] array : listOfArrays) {
System.arraycopy(array, 0, concatenatedArrays, i, array.length);
i += array.length;
}
return concatenatedArrays;
}
}

0 comments on commit 38c1c9a

Please sign in to comment.