wang-jiahua opened a new issue, #10511:
URL: https://github.com/apache/rocketmq/issues/10511
### Before Creating the Enhancement Request
- [x] I have confirmed that this should be classified as an enhancement
rather than a bug/feature.
### Summary
`LanguageCode.valueOf(byte)` and `SerializeType.valueOf(byte)` currently
call `values()` in a loop on every invocation. Since JDK requires
`Enum.values()` to return a fresh array each time (defensive copy), this
allocates a new array per call. Replace with a static `BY_CODE[]` array for
O(1) lookup with zero allocation.
### Motivation
JFR `settings=profile` shows these `valueOf(byte)` methods are called on
every RPC decode path. `values()` creates a new array on every call — a per-RPC
allocation that is completely unnecessary since the enum constants are fixed at
class load time.
### Describe the Solution You'd Like
```java
// LanguageCode.java
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;
}
}
public static LanguageCode valueOf(byte code) {
int idx = code & 0xFF;
return idx < BY_CODE.length ? BY_CODE[idx] : null;
}
```
Same pattern for `SerializeType` (which only has 2 values: JSON=0,
ROCKETMQ=1).
### Describe Alternatives You've Considered
- **HashMap<Byte, LanguageCode>**: Adds boxing overhead (`Byte.valueOf`) and
more memory than a simple array. Array lookup is simpler and faster.
- **Switch statement**: Not applicable since the lookup key is a `byte`
parameter, not a compile-time constant.
### Additional Context
This is the same pattern used in PR #10469 for
`MessageVersion.valueOfMagicCode()` (merged).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]