Skip to content
New issue

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

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

Already on GitHub? # to your account

MultimapBuilder creates unnecessary objects #5673

Open
CodingFabian opened this issue Aug 7, 2021 · 1 comment · May be fixed by #5676
Open

MultimapBuilder creates unnecessary objects #5673

CodingFabian opened this issue Aug 7, 2021 · 1 comment · May be fixed by #5676

Comments

@CodingFabian
Copy link

Looking at high memory usage in one of our components, we noticed that for every MultiMap created by a MultimapBuilder, we have a Supplier too (in our case it is a HashSetSupplier, but it applies to other Suppliers too)

the reason for that is that we do:

  public static final MultimapBuilder.SetMultimapBuilder<Object, Object> RELATIONS_MAP_BUILDER = MultimapBuilder
      .hashKeys(2)
      .hashSetValues(1);

which in turn calls this Guava code

    public SetMultimapBuilder<K0, Object> hashSetValues(final int expectedValuesPerKey) {
      checkNonnegative(expectedValuesPerKey, "expectedValuesPerKey");
      return new SetMultimapBuilder<K0, Object>() {
        @Override
        public <K extends K0, V> SetMultimap<K, V> build() {
          return Multimaps.newSetMultimap(
              MultimapBuilderWithKeys.this.<K, V>createMap(),
              new HashSetSupplier<V>(expectedValuesPerKey));
        }
      };
    }

This creates a new hashSetSupplier every time build is invoked.
This looks incorrect to me, and the suppliers should be properties of the newly returned builder, shouldn't they?

@CodingFabian
Copy link
Author

image (3)

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants