Copy HttpStatus::values to prevent allocation

Before this commit, HttpStatus::resolve used the values() method in its
logic. This causes a new array to be allocated for each invocation,
and results in memory overhead.

This commit makes a copy of the HttpStatus values array, and uses that
to resolve status codes.

Closes gh-26842
This commit is contained in:
Arjen Poutsma
2021-04-22 15:39:03 +02:00
parent e4c0ff569b
commit 7f1062159e

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@@ -416,6 +416,13 @@ public enum HttpStatus {
NETWORK_AUTHENTICATION_REQUIRED(511, Series.SERVER_ERROR, "Network Authentication Required");
private static final HttpStatus[] VALUES;
static {
VALUES = values();
}
private final int value;
private final Series series;
@@ -550,7 +557,8 @@ public enum HttpStatus {
*/
@Nullable
public static HttpStatus resolve(int statusCode) {
for (HttpStatus status : values()) {
// used cached VALUES instead of values() to prevent array allocation
for (HttpStatus status : VALUES) {
if (status.value == statusCode) {
return status;
}