Skip to content

Reading SegmentInfo and FieldInfo Map and Set fields perform unecessary sorting and object creation #15453

@blerer

Description

@blerer

Description

The logic of DataInput.readMapOfStrings is the following:

  public Map<String, String> readMapOfStrings() throws IOException {
    int count = readVInt();
    if (count == 0) {
      return Collections.emptyMap();
    } else if (count == 1) {
      return Collections.singletonMap(readString(), readString());
    } else {
      Map<String, String> map = count > 10 ? new HashMap<>() : new TreeMap<>();
      for (int i = 0; i < count; i++) {
        final String key = readString();
        final String val = readString();
        map.put(key, val);
      }
      return Collections.unmodifiableMap(map);
    }
  }

If the number of element is greater than 10, the map that is used will be a TreeMap that will perfrom insert in O(log n) instead of an amortized O(1) and the returned map (HashMap or TreeMap) will be wrapped in an unmodifiable one.
The issue is that the sort and the wrapping appear to be in practice useless because SegmentInfo will perform a Map.copyOf on it (discarding the sort and the wrapping). FieldInfo do not perform any operation on top of the Map and expect an Unmodifiable HashMap (based on its code).
A similar thing happen with DataInput.readSetOfStrings

Based on those observations, it would make sense to remove the unecessary sorting and standardize the code on either Unmodifiable Maps or ImmutableMaps. Maps.copyOf is a noop on an ImmutableMap

Happy to provide a patch if the decision is to fix that logic. :-)

Version and environment details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions