Skip to content

Commit 7f10621

Browse files
committed
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
1 parent e4c0ff5 commit 7f10621

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

spring-web/src/main/java/org/springframework/http/HttpStatus.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -416,6 +416,13 @@ public enum HttpStatus {
416416
NETWORK_AUTHENTICATION_REQUIRED(511, Series.SERVER_ERROR, "Network Authentication Required");
417417

418418

419+
private static final HttpStatus[] VALUES;
420+
421+
static {
422+
VALUES = values();
423+
}
424+
425+
419426
private final int value;
420427

421428
private final Series series;
@@ -550,7 +557,8 @@ public static HttpStatus valueOf(int statusCode) {
550557
*/
551558
@Nullable
552559
public static HttpStatus resolve(int statusCode) {
553-
for (HttpStatus status : values()) {
560+
// used cached VALUES instead of values() to prevent array allocation
561+
for (HttpStatus status : VALUES) {
554562
if (status.value == statusCode) {
555563
return status;
556564
}

0 commit comments

Comments
 (0)