Skip to content

Commit 9b0b2e6

Browse files
committed
Avoid Enum.values() creating an array for parsing the values
Since arrays can't be immutable the JVM will always construct and return a new enum array. This change is a GC / performance improvement to avoid those creations and collections by using a private static array.
1 parent ad95874 commit 9b0b2e6

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
group = org.threadly
22
version = 0.27-SNAPSHOT
3-
threadlyVersion = 5.40
3+
threadlyVersion = 5.41
44
litesocketsVersion = 4.11
55
org.gradle.parallel=false
66
junitVersion = 4.12

protocol/src/main/java/org/threadly/litesockets/protocols/http/shared/HTTPResponseCode.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ public enum HTTPResponseCode {
6969
NetworkAuthenticationRequired(511, "Network Authentication Required") /*(RFC 6585)*/,
7070
OptionNotSupported(551, "Option not supported") /*(RTSP)*/;
7171

72-
private int val;
73-
private String text;
72+
private static final HTTPResponseCode[] VALUES = HTTPResponseCode.values(); // avoid copies
73+
74+
private final int val;
75+
private final String text;
76+
7477
private HTTPResponseCode(int val, String text) {
7578
this.val = val;
7679
this.text = text;
@@ -97,7 +100,7 @@ public String toString() {
97100
* @throws IllegalArgumentException thrown if no code is associated with the provided value
98101
*/
99102
public static HTTPResponseCode findResponseCode(int val) {
100-
for(HTTPResponseCode hrc: HTTPResponseCode.values()) {
103+
for(HTTPResponseCode hrc: VALUES) {
101104
if(hrc.getId() == val) {
102105
return hrc;
103106
}

protocol/src/main/java/org/threadly/litesockets/protocols/websocket/WSOPCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public enum WSOPCode {
77
Continuation((byte)0), Text((byte)1), Binary((byte)2),
88
Close((byte)8), Ping((byte)9), Pong((byte)10);
99

10+
private static final WSOPCode[] VALUES = WSOPCode.values(); // avoid copies
11+
1012
private final byte value;
1113

1214
private WSOPCode(byte value) {
@@ -29,7 +31,7 @@ public byte getValue() {
2931
* @return Matching opcode or {@code null} if none was found
3032
*/
3133
public static WSOPCode fromByte(byte b) {
32-
for(WSOPCode oc: WSOPCode.values()) {
34+
for(WSOPCode oc: VALUES) {
3335
if(oc.getValue() == b) {
3436
return oc;
3537
}

0 commit comments

Comments
 (0)