-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Avoid unnecessary sorting and instantiations in readMapOfStrings #15457
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,14 +18,9 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.TreeMap; | ||
| import java.util.TreeSet; | ||
| import org.apache.lucene.util.BitUtil; | ||
|
|
||
| /** | ||
|
|
@@ -248,17 +243,17 @@ public DataInput clone() { | |
| public Map<String, String> readMapOfStrings() throws IOException { | ||
| int count = readVInt(); | ||
| if (count == 0) { | ||
| return Collections.emptyMap(); | ||
| return Map.of(); | ||
| } else if (count == 1) { | ||
| return Collections.singletonMap(readString(), readString()); | ||
| return Map.of(readString(), readString()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes only work if input arguments are non-null, which I'm not sure we ever enforced or documented so it would be an incompatible change. Applies to both sets and maps. To play it safe, I'd limit the change to just removing the treeset (and leaving hash set). Then this can be backported with no problems. If you'd like to move to non-null arguments then this needs to be documented in the migration, method javadocs, etc.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently the |
||
| } else { | ||
| Map<String, String> map = count > 10 ? new HashMap<>() : new TreeMap<>(); | ||
| @SuppressWarnings("unchecked") | ||
| Map.Entry<String, String>[] entries = | ||
| (Map.Entry<String, String>[]) new Map.Entry<?, ?>[count]; | ||
| for (int i = 0; i < count; i++) { | ||
| final String key = readString(); | ||
| final String val = readString(); | ||
| map.put(key, val); | ||
| entries[i] = Map.entry(readString(), readString()); | ||
| } | ||
| return Collections.unmodifiableMap(map); | ||
| return Map.ofEntries(entries); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -270,15 +265,15 @@ public Map<String, String> readMapOfStrings() throws IOException { | |
| public Set<String> readSetOfStrings() throws IOException { | ||
| int count = readVInt(); | ||
| if (count == 0) { | ||
| return Collections.emptySet(); | ||
| return Set.of(); | ||
| } else if (count == 1) { | ||
| return Collections.singleton(readString()); | ||
| return Set.of(readString()); | ||
| } else { | ||
| Set<String> set = count > 10 ? new HashSet<>() : new TreeSet<>(); | ||
| String[] set = new String[count]; | ||
| for (int i = 0; i < count; i++) { | ||
| set.add(readString()); | ||
| set[i] = readString(); | ||
| } | ||
| return Collections.unmodifiableSet(set); | ||
| return Set.of(set); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we call out the parts of the code that mean anything on this? e.g. segment attributes & diagnostics are now not ordered. Also, this will mean two segment infos that were previously equivalent will now have different toStrings (which is likely OK)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
SegmentInfothe maps were not ordered before the change because te use ofMap.copyOf, in the constructor, was discarding theTreeMapordering. A similar thing was happening for theTreeSet. Its elements were converted and added to aHashSet. OnlyFieldInfoattributeswas somehow retaining the order has long as no new attibutes were added. Of course that was only valid if you add less than 10 elements within the maps.