Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize some iterations in BodyExtractor and BodyInserter #30136

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ protected Mono<Void> doCommit(@Nullable Supplier<? extends Publisher<Void>> writ
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,16 @@ private static <T, S extends Publisher<T>> S readWithMessageReaders(
MediaType contentType = Optional.ofNullable(message.getHeaders().getContentType())
.orElse(MediaType.APPLICATION_OCTET_STREAM);

return context.messageReaders().stream()
.filter(reader -> reader.canRead(elementType, contentType))
.findFirst()
.map(BodyExtractors::<T>cast)
.map(readerFunction)
.orElseGet(() -> {
List<MediaType> mediaTypes = context.messageReaders().stream()
.flatMap(reader -> reader.getReadableMediaTypes(elementType).stream())
.toList();
return errorFunction.apply(
new UnsupportedMediaTypeException(contentType, mediaTypes, elementType));
});
for (HttpMessageReader<?> messageReader : context.messageReaders()) {
if (messageReader.canRead(elementType, contentType)) {
return readerFunction.apply(cast(messageReader));
}
}
List<MediaType> mediaTypes = context.messageReaders().stream()
.flatMap(reader -> reader.getReadableMediaTypes(elementType).stream())
.toList();
return errorFunction.apply(
new UnsupportedMediaTypeException(contentType, mediaTypes, elementType));
}

private static <T> Mono<T> readToMono(ReactiveHttpInputMessage message, BodyExtractor.Context context,
Expand Down Expand Up @@ -245,12 +243,13 @@ private static <T> Flux<T> unsupportedErrorHandler(
private static <T> HttpMessageReader<T> findReader(
ResolvableType elementType, MediaType mediaType, BodyExtractor.Context context) {

return context.messageReaders().stream()
.filter(messageReader -> messageReader.canRead(elementType, mediaType))
.findFirst()
.map(BodyExtractors::<T>cast)
.orElseThrow(() -> new IllegalStateException(
"No HttpMessageReader for \"" + mediaType + "\" and \"" + elementType + "\""));
for (HttpMessageReader<?> messageReader : context.messageReaders()) {
if (messageReader.canRead(elementType, mediaType)) {
return cast(messageReader);
}
}
throw new IllegalStateException(
"No HttpMessageReader for \"" + mediaType + "\" and \"" + elementType + "\"");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ else if (adapter != null) {
publisher = Mono.just(body);
}
MediaType mediaType = outputMessage.getHeaders().getContentType();
return context.messageWriters().stream()
.filter(messageWriter -> messageWriter.canWrite(bodyType, mediaType))
.findFirst()
.map(BodyInserters::cast)
.map(writer -> write(publisher, bodyType, mediaType, outputMessage, context, writer))
.orElseGet(() -> Mono.error(unsupportedError(bodyType, context, mediaType)));
for (HttpMessageWriter<?> messageWriter : context.messageWriters()) {
if (messageWriter.canWrite(bodyType, mediaType)) {
HttpMessageWriter<Object> typedMessageWriter = cast(messageWriter);
return write(publisher, bodyType, mediaType, outputMessage, context, typedMessageWriter);
}
}
return Mono.error(unsupportedError(bodyType, context, mediaType));
}

private static UnsupportedMediaTypeException unsupportedError(ResolvableType bodyType,
Expand Down Expand Up @@ -406,12 +407,13 @@ private static <T> Mono<Void> write(Publisher<? extends T> input, ResolvableType
private static <T> HttpMessageWriter<T> findWriter(
BodyInserter.Context context, ResolvableType elementType, @Nullable MediaType mediaType) {

return context.messageWriters().stream()
.filter(messageWriter -> messageWriter.canWrite(elementType, mediaType))
.findFirst()
.map(BodyInserters::<T>cast)
.orElseThrow(() -> new IllegalStateException(
"No HttpMessageWriter for \"" + mediaType + "\" and \"" + elementType + "\""));
for (HttpMessageWriter<?> messageWriter : context.messageWriters()) {
if (messageWriter.canWrite(elementType, mediaType)) {
return cast(messageWriter);
}
}
throw new IllegalStateException(
"No HttpMessageWriter for \"" + mediaType + "\" and \"" + elementType + "\"");
}

@SuppressWarnings("unchecked")
Expand Down