-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Speed up DiscoveryNodeFilters.trimTier #78170
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
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 |
---|---|---|
|
@@ -81,12 +81,27 @@ public static <K, V> Map<K, V> copyMapWithAddedOrReplacedEntry(final Map<K, V> m | |
* @param <V> the type of the values in the map | ||
* @return an immutable map that contains the items from the specified map with the provided key removed | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <K, V> Map<K, V> copyMapWithRemovedEntry(final Map<K, V> map, final K key) { | ||
Objects.requireNonNull(map); | ||
Objects.requireNonNull(key); | ||
assert checkIsImmutableMap(map, key, map.get(key)); | ||
return map.entrySet().stream().filter(k -> key.equals(k.getKey()) == false) | ||
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
if (map.containsKey(key) == false) { | ||
return map; | ||
} | ||
Comment on lines
+89
to
+91
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. This subtly changes the behavior because someone would assume that the returned value is always a copied map (since it's in the name), and not the original map |
||
final int size = map.size(); | ||
if (size == 1) { | ||
return Map.of(); | ||
} | ||
@SuppressWarnings("rawtypes") | ||
final Map.Entry<K, V>[] entries = new Map.Entry[size - 1]; | ||
int i = 0; | ||
for (Map.Entry<K, V> entry : map.entrySet()) { | ||
if (key.equals(entry.getKey()) == false) { | ||
entries[i++] = entry; | ||
} | ||
} | ||
return Map.ofEntries(entries); | ||
Comment on lines
+96
to
+104
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. This feels a lot like premature optimization, is this really a bottleneck here? 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. This whole thing showed up hot during cluster restart + reroute tests on large shard count benchmarks. The problem with those really is that all these |
||
} | ||
|
||
// map classes that are known to be immutable, used to speed up immutability check in #assertImmutableMap | ||
|
@@ -154,7 +169,6 @@ public static <K, V> boolean deepEquals(Map<K, V> left, Map<K, V> right) { | |
* @param map - input to be flattened | ||
* @param flattenArrays - if false, arrays will be ignored | ||
* @param ordered - if true the resulted map will be sorted | ||
* @return | ||
*/ | ||
public static Map<String, Object> flatten(Map<String, Object> map, boolean flattenArrays, boolean ordered) { | ||
return flatten(map, flattenArrays, ordered, null); | ||
|
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 you move this up to the top where the other class variables are please?