Skip to content

Commit

Permalink
Optimize JsonRpcErrorCodes lookup using Int2ObjectOpenHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Malaydewangan09 committed Oct 13, 2024
1 parent 2ddbb0f commit dc68ccc
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package tech.pegasys.teku.ethereum.executionclient.web3j;

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

public enum JsonRpcErrorCodes {
PARSE_ERROR(-32700, "Parse error"),
INVALID_REQUEST(-32600, "Invalid Request"),
Expand All @@ -23,6 +25,14 @@ public enum JsonRpcErrorCodes {

private final int errorCode;
private final String description;
private static final Int2ObjectOpenHashMap<JsonRpcErrorCodes> CODE_TO_ERROR_MAP;

static {
CODE_TO_ERROR_MAP = new Int2ObjectOpenHashMap<>();
for (JsonRpcErrorCodes error : values()) {
CODE_TO_ERROR_MAP.put(error.getErrorCode(), error);
}
}

JsonRpcErrorCodes(final int errorCode, final String description) {
this.errorCode = errorCode;
Expand All @@ -42,10 +52,9 @@ public static String getErrorMessage(final int errorCode) {
}

public static JsonRpcErrorCodes fromCode(final int errorCode) {
for (JsonRpcErrorCodes error : values()) {
if (error.getErrorCode() == errorCode) {
return error;
}
JsonRpcErrorCodes error = CODE_TO_ERROR_MAP.get(errorCode);
if (error != null) {
return error;
}
return errorCode >= -32099 && errorCode <= -32000 ? SERVER_ERROR : INTERNAL_ERROR;
}
Expand Down

0 comments on commit dc68ccc

Please sign in to comment.