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

Code optimization suggestions #1634

Open
noobyu6 opened this issue Jun 8, 2022 · 0 comments
Open

Code optimization suggestions #1634

noobyu6 opened this issue Jun 8, 2022 · 0 comments

Comments

@noobyu6
Copy link

noobyu6 commented Jun 8, 2022

Hi,

I find that the private field hasAnnotations at Line 287 in the file 'guice/core/src/com/google/inject/internal/Annotations.java ' on the master branch is only assigned and used in the field cache. Therefore, this field can be removed from the class, and become an input parameter in the field cache. This transformation will normally reduce memory usage and improve readability of your code.I will be happy if this transformation is helpful.

// line 287 this field can be removed from the class
private CacheLoader<Class<? extends Annotation>, Boolean> hasAnnotations = 
        new CacheLoader<Class<? extends Annotation>, Boolean>() {
          @Override
          public Boolean load(Class<? extends Annotation> annotationType) {
            for (Annotation annotation : annotationType.getAnnotations()) {
              if (annotationTypes.contains(annotation.annotationType())) {
                return true;
              }
            }
            return false;
          }
        };
// start 
final LoadingCache<Class<? extends Annotation>, Boolean> cache =
        CacheBuilder.newBuilder().weakKeys().build(new CacheLoader<Class<? extends Annotation>, Boolean>() {
          @Override
          public Boolean load(Class<? extends Annotation> annotationType) {
            for (Annotation annotation : annotationType.getAnnotations()) {
              if (annotationTypes.contains(annotation.annotationType())) {
                return true;
              }
            }
            return false;
          }
        });
// end

    final LoadingCache<Class<? extends Annotation>, Boolean> cache =
        CacheBuilder.newBuilder().weakKeys().build(hasAnnotations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant