Skip to content

Commit

Permalink
optimize: remove unnecessary copy for SINGLE cf input, since the subs…
Browse files Browse the repository at this point in the history
…equent `thenApply` operation return a new CF
  • Loading branch information
oldratlee committed Aug 17, 2024
1 parent 2dbf1e0 commit 609047e
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public static <T> CompletableFuture<List<T>> allResultsFastFailOf(CompletionStag
requireCfsAndEleNonNull(cfs);
final int len = cfs.length;
if (len == 0) return completedFuture(arrayList());
// Defensive copy input cf to non-minimal-stage instance(toNonMinCfCopy) for SINGLE input
// convert input cf to non-minimal-stage instance(toNonMinCfCopy) for SINGLE input
// in order to ensure that the returned cf is not minimal-stage CF instance(UnsupportedOperationException)
if (len == 1) return toNonMinCf(cfs[0]).thenApply(CompletableFutureUtils::arrayList);

Expand Down Expand Up @@ -1249,9 +1249,9 @@ public static <T> CompletableFuture<List<T>> allResultsOf(CompletionStage<? exte
requireCfsAndEleNonNull(cfs);
final int len = cfs.length;
if (len == 0) return completedFuture(arrayList());
// Defensive copy input cf to non-minimal-stage instance(toNonMinCfCopy) for SINGLE input
// convert input cf to non-minimal-stage instance(toNonMinCfCopy) for SINGLE input
// in order to ensure that the returned cf is not minimal-stage CF instance(UnsupportedOperationException)
if (len == 1) return toNonMinCfCopy(cfs[0]).thenApply(CompletableFutureUtils::arrayList);
if (len == 1) return toNonMinCf(cfs[0]).thenApply(CompletableFutureUtils::arrayList);

final Object[] result = new Object[len];
final CompletableFuture<Void>[] resultSetterCfs = createResultSetterCfs(cfs, result);
Expand Down Expand Up @@ -1296,9 +1296,9 @@ public static CompletableFuture<Void> allFastFailOf(CompletionStage<?>... cfs) {
requireCfsAndEleNonNull(cfs);
final int len = cfs.length;
if (len == 0) return completedFuture(null);
// Defensive copy input cf to non-minimal-stage instance for SINGLE input in order to ensure that
// convert input cf to non-minimal-stage instance for SINGLE input in order to ensure that
// the returned cf is not minimal-stage CF instance(UnsupportedOperationException)
if (len == 1) return toNonMinCfCopy(cfs[0]).thenApply(unused -> null);
if (len == 1) return toNonMinCf(cfs[0]).thenApply(unused -> null);

final CompletableFuture<?>[] successOrBeIncomplete = new CompletableFuture[len];
// NOTE: fill ONE MORE element of failedOrBeIncomplete LATER
Expand Down Expand Up @@ -1345,9 +1345,9 @@ public static CompletableFuture<Void> allFastFailOf(CompletionStage<?>... cfs) {
public static CompletableFuture<Void> allOf(CompletionStage<?>... cfs) {
requireNonNull(cfs, "cfs is null");
if (cfs.length == 0) return completedFuture(null);
// Defensive copy input cf to non-minimal-stage instance(toNonMinCfCopy) for SINGLE input
// convert input cf to non-minimal-stage instance(toNonMinCfCopy) for SINGLE input
// in order to ensure that the returned cf is not minimal-stage CF instance(UnsupportedOperationException)
if (cfs.length == 1) return toNonMinCfCopy(requireNonNull(cfs[0], "cf1 is null")).thenApply(unused -> null);
if (cfs.length == 1) return toNonMinCf(requireNonNull(cfs[0], "cf1 is null")).thenApply(unused -> null);
return CompletableFuture.allOf(f_toCfArray(cfs));
}

Expand Down

0 comments on commit 609047e

Please sign in to comment.