Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save allocating enum values array in two hot spots #104952

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static XContentType fromMediaType(String mediaTypeHeaderValue) throws Ill
return null;
}

private int index;
private final int index;

XContentType(int index) {
this.index = index;
Expand Down Expand Up @@ -315,4 +315,10 @@ public ParsedMediaType toParsedMediaType() {
public XContentType canonical() {
return this;
}

private static final XContentType[] values = values();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a value in wrapping this array into unmodifyable list to protect against modifications (even if this is private)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, that's not a 0-overhead thing either I think, even after all the JITing in the world :)


public static XContentType ofOrdinal(int ordinal) {
return values[ordinal];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public IndexRequest(@Nullable ShardId shardId, StreamInput in) throws IOExceptio
isRetry = in.readBoolean();
autoGeneratedTimestamp = in.readLong();
if (in.readBoolean()) {
contentType = in.readEnum(XContentType.class);
// faster than StreamInput::readEnum, do not replace we read a lot of these instances at times
contentType = XContentType.ofOrdinal(in.readByte());
} else {
contentType = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public static RefreshPolicy parse(String value) {
throw new IllegalArgumentException("Unknown value for refresh: [" + value + "].");
}

private static final RefreshPolicy[] values = values();

public static RefreshPolicy readFrom(StreamInput in) throws IOException {
return RefreshPolicy.values()[in.readByte()];
return values[in.readByte()];
}

@Override
Expand Down