-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Compute declared versions in a static block #28661
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
Conversation
This method is called often enough that the reflection and sort can be seen while profiling. The impact is quite small but it is an easy enough fix and I see no reason why this needs to be computed every invoke.
|
Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually? |
1 similar comment
|
Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually? |
jasontedor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @scottsom, I left some feedback.
|
|
||
| private static final List<Version> DECLARED_VERSIONS; | ||
| static { | ||
| DECLARED_VERSIONS = getDeclaredVersions(Version.class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this lazy, and make this an unmodifiable list. Also, I think we can drop this method and only access the list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the method needs to stay since it can be called with a different version class:
https://github.com/elastic/elasticsearch/blob/master/test/framework/src/main/java/org/elasticsearch/test/VersionUtils.java#L53
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yet you can expose the list to that class too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The input in the VersionUtils method is an arbitrary Class<?> rather than Version.class, so the values in the list will change based on the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for investigating.
jasontedor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a suggestion, let me know what you think?
| public final byte build; | ||
| public final org.apache.lucene.util.Version luceneVersion; | ||
|
|
||
| private List<Version> declaredVersionsCache; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking something like:
diff --git a/server/src/main/java/org/elasticsearch/Version.java b/server/src/main/java/org/elasticsearch/Version.java
index 1e1993e6d6..c0181d4f1e 100644
--- a/server/src/main/java/org/elasticsearch/Version.java
+++ b/server/src/main/java/org/elasticsearch/Version.java
@@ -372,7 +372,9 @@ public class Version implements Comparable<Version> {
public final byte build;
public final org.apache.lucene.util.Version luceneVersion;
- private List<Version> declaredVersionsCache;
+ private static final class DeclaredVersionsHolder {
+ static final List<Version> declaredVersions = Collections.unmodifiableList(getDeclaredVersions(Version.class));
+ }
Version(int id, org.apache.lucene.util.Version luceneVersion) {
this.id = id;
@@ -414,17 +416,10 @@ public class Version implements Comparable<Version> {
public Version minimumCompatibilityVersion() {
if (major >= 6) {
// all major versions from 6 onwards are compatible with last minor series of the previous major
- List<Version> declaredVersions = declaredVersionsCache;
-
- if (declaredVersions == null) {
- declaredVersions = Collections.unmodifiableList(getDeclaredVersions(getClass()));
- declaredVersionsCache = declaredVersions;
- }
-
Version bwcVersion = null;
- for (int i = declaredVersions.size() - 1; i >= 0; i--) {
- final Version candidateVersion = declaredVersions.get(i);
+ for (int i = DeclaredVersionsHolder.declaredVersions.size() - 1; i >= 0; i--) {
+ final Version candidateVersion = DeclaredVersionsHolder.declaredVersions.get(i);
if (candidateVersion.major == major - 1 && candidateVersion.isRelease() && after(candidateVersion)) {
if (bwcVersion != null && candidateVersion.minor < bwcVersion.minor) {
break;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, I was copying the String#hashCode style. I'll make that change.
|
test this please |
jasontedor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
This method is called often enough (when computing minimum compatibility versions) that the reflection and sort can be seen while profiling. This commit addresses this issue by computing the declared versions exactly once. Relates #28661
This method is called often enough (when computing minimum compatibility versions) that the reflection and sort can be seen while profiling. This commit addresses this issue by computing the declared versions exactly once. Relates #28661
|
Thanks @scottsom. |
This method is called often enough that the reflection and sort
can be seen while profiling. The impact is quite small but
it is an easy enough fix and I see no reason why this needs to be
computed every invoke.