From b3f94c47d96747614e4818c61a7179ee68fc3970 Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Wed, 10 Apr 2019 16:35:30 +0800 Subject: [PATCH 1/2] 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)); }; } From 2448efc0281e0356de7b1a5ba3e695ef1615d602 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 19 Apr 2019 16:30:54 +0200 Subject: [PATCH 2/2] Polish "Cache MimeTypes to improve performance" Closes gh-16507 --- .../web/embedded/netty/CompressionCustomizer.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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 68329f3e10..807b3861a5 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,14 +65,12 @@ final class CompressionCustomizer implements NettyServerCustomizer { return server; } - private CompressionPredicate getMimeTypesPredicate(String[] mimeTypes) { - if (ObjectUtils.isEmpty(mimeTypes)) { + private CompressionPredicate getMimeTypesPredicate(String[] mimeTypeIds) { + if (ObjectUtils.isEmpty(mimeTypeIds)) { return ALWAYS_COMPRESS; } - - List mimeTypeList = Arrays.stream(mimeTypes) + List mimeTypes = Arrays.stream(mimeTypeIds) .map(MimeTypeUtils::parseMimeType).collect(Collectors.toList()); - return (request, response) -> { String contentType = response.responseHeaders() .get(HttpHeaderNames.CONTENT_TYPE); @@ -80,7 +78,7 @@ final class CompressionCustomizer implements NettyServerCustomizer { return false; } MimeType contentMimeType = MimeTypeUtils.parseMimeType(contentType); - return mimeTypeList.stream() + return mimeTypes.stream() .anyMatch((candidate) -> candidate.isCompatibleWith(contentMimeType)); }; }