Skip to content

MultimapBuilder creates unnecessary objects #5673

Open
@CodingFabian

Description

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions