[ISSUE #10511] Replace Enum.values() loop with static array lookup in LanguageCode and SerializeType (#10513)

Co-authored-by: wangjiahua.wjh <wangjiahua.wjh@alibaba-inc.com>
This commit is contained in:
Jiahua Wang
2026-06-17 14:10:28 +08:00
committed by GitHub
parent 59a70d9b80
commit e18f0d2c2c
2 changed files with 19 additions and 12 deletions
@@ -38,6 +38,19 @@ public enum LanguageCode {
RUST((byte) 12),
NODE_JS((byte) 13);
private static final LanguageCode[] BY_CODE;
static {
LanguageCode[] all = values();
int max = 0;
for (LanguageCode lc : all) {
max = Math.max(max, lc.code & 0xFF);
}
BY_CODE = new LanguageCode[max + 1];
for (LanguageCode lc : all) {
BY_CODE[lc.code & 0xFF] = lc;
}
}
private byte code;
LanguageCode(byte code) {
@@ -45,12 +58,8 @@ public enum LanguageCode {
}
public static LanguageCode valueOf(byte code) {
for (LanguageCode languageCode : LanguageCode.values()) {
if (languageCode.getCode() == code) {
return languageCode;
}
}
return null;
int idx = code & 0xFF;
return idx < BY_CODE.length ? BY_CODE[idx] : null;
}
public byte getCode() {
@@ -21,6 +21,8 @@ public enum SerializeType {
JSON((byte) 0),
ROCKETMQ((byte) 1);
private static final SerializeType[] BY_CODE = {JSON, ROCKETMQ};
private byte code;
SerializeType(byte code) {
@@ -28,12 +30,8 @@ public enum SerializeType {
}
public static SerializeType valueOf(byte code) {
for (SerializeType serializeType : SerializeType.values()) {
if (serializeType.getCode() == code) {
return serializeType;
}
}
return null;
int idx = code & 0xFF;
return idx < BY_CODE.length ? BY_CODE[idx] : null;
}
public byte getCode() {