From b3f94c47d96747614e4818c61a7179ee68fc3970 Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Wed, 10 Apr 2019 16:35:30 +0800 Subject: [PATCH] Cache MimeTypes to improve performance See gh-16507 --- .../boot/web/embedded/netty/CompressionCustomizer.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/CompressionCustomizer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/CompressionCustomizer.java index a31c3c7a7d..68329f3e10 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/CompressionCustomizer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/CompressionCustomizer.java @@ -17,7 +17,9 @@ package org.springframework.boot.web.embedded.netty; import java.util.Arrays; +import java.util.List; import java.util.function.BiPredicate; +import java.util.stream.Collectors; import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; @@ -67,6 +69,10 @@ final class CompressionCustomizer implements NettyServerCustomizer { if (ObjectUtils.isEmpty(mimeTypes)) { return ALWAYS_COMPRESS; } + + List mimeTypeList = Arrays.stream(mimeTypes) + .map(MimeTypeUtils::parseMimeType).collect(Collectors.toList()); + return (request, response) -> { String contentType = response.responseHeaders() .get(HttpHeaderNames.CONTENT_TYPE); @@ -74,7 +80,7 @@ final class CompressionCustomizer implements NettyServerCustomizer { return false; } MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType); - return Arrays.stream(mimeTypes).map(MimeTypeUtils::parseMimeType) + return mimeTypeList.stream() .anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType)); }; }