Skip to content

Conversation

@scottsom
Copy link

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.

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.
@elasticmachine
Copy link
Collaborator

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
@elasticmachine
Copy link
Collaborator

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?

Copy link
Member

@jasontedor jasontedor left a 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);
Copy link
Member

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?

Copy link
Author

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

Copy link
Member

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?

Copy link
Author

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.

Copy link
Member

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.

Copy link
Member

@jasontedor jasontedor left a 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;
Copy link
Member

@jasontedor jasontedor Feb 13, 2018

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;

Copy link
Author

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.

@jasontedor
Copy link
Member

test this please

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

LGTM.

@jasontedor jasontedor merged commit a138e0e into elastic:master Feb 13, 2018
jasontedor pushed a commit that referenced this pull request Feb 13, 2018
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
@jasontedor jasontedor added v6.2.3 and removed v6.2.2 labels Feb 13, 2018
jasontedor pushed a commit that referenced this pull request Feb 13, 2018
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
@jasontedor
Copy link
Member

Thanks @scottsom.

@scottsom scottsom deleted the version_static_init branch February 13, 2018 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants