Skip to content

Commit

Permalink
perf: optimize memory usage in mostSuccessOf* methods by releasing …
Browse files Browse the repository at this point in the history
…unneeded exception instances 📝
  • Loading branch information
oldratlee committed Feb 8, 2025
1 parent 2c6f205 commit bf5fd0b
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ private static <T> CompletableFuture<T> f_mostSuccessTupleOf0(
Executor executorWhenTimeout, long timeout, TimeUnit unit, CompletionStage<?>[] stages) {
// 1. MUST be non-minimal-stage CF instances in order to read results(`getSuccessNow`), otherwise UnsupportedOpException.
// 2. SHOULD copy input cfs to avoid memory leaks, otherwise all input cfs would be retained until output cf completes.
final CompletableFuture<Object>[] cfArray = toNonMinCfCopyArray0(stages);
CompletableFuture<?>[] cfArray = mapArray(stages, CompletableFuture[]::new,
s -> toNonMinCf0(s).exceptionally(v -> null));
return cffuCompleteOnTimeout(CompletableFuture.allOf(cfArray), null, timeout, unit, executorWhenTimeout)
.handle((unused, ex) -> f_tupleOf0(f_mGetSuccessNow0(null, cfArray)));
}
Expand Down Expand Up @@ -946,7 +947,8 @@ private static <T> CompletableFuture<List<T>> mostSuccessResultsOf0(

// 1. MUST be non-minimal-stage CF instances in order to read results(`getSuccessNow`), otherwise UnsupportedOpException.
// 2. SHOULD copy input cfs to avoid memory leaks, otherwise all input cfs would be retained until output cf completes.
final CompletableFuture<T>[] cfArray = toNonMinCfCopyArray0(cfs);
CompletableFuture<T>[] cfArray = mapArray(cfs, CompletableFuture[]::new,
s -> LLCF.<T>toNonMinCf0(s).exceptionally(v -> valueIfNotSuccess));
return cffuCompleteOnTimeout(CompletableFuture.allOf(cfArray), null, timeout, unit, executorWhenTimeout)
.handle((unused, ex) -> arrayList(f_mGetSuccessNow0(valueIfNotSuccess, cfArray)));
}
Expand Down

0 comments on commit bf5fd0b

Please sign in to comment.