Optimize some iterations in BodyExtractor and BodyInserter

This commit turns some stream-based iterations back into simpler
enhanced for loops.

For simple use cases like these, where the stream API is merely used to
map/filter + collect to a List, a for loop is more efficient.
This is especially true for small collections like the ones we deal
with in BodyInserters/BodyExtractors here (in the order of 50ns/op vs
5ns/op). These cases are also simple enough that they don't lose in
readability after the conversion.

Closes gh-30136
This commit is contained in:
James Yuzawa
2023-03-22 06:04:54 -04:00
committed by GitHub
parent 4e896c8125
commit 800b13492b
3 changed files with 35 additions and 32 deletions

View File

@@ -147,8 +147,10 @@ public abstract class AbstractClientHttpRequest implements ClientHttpRequest {
this.commitActions.add(writeAction);
}
List<? extends Publisher<Void>> actions = this.commitActions.stream()
.map(Supplier::get).toList();
List<Publisher<Void>> actions = new ArrayList<>(this.commitActions.size());
for (Supplier<? extends Publisher<Void>> commitAction : this.commitActions) {
actions.add(commitAction.get());
}
return Flux.concat(actions).then();
}