-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Simplify map initial sizing #126767
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?
Simplify map initial sizing #126767
Changes from all commits
7729714
f1c5b6e
7dd4636
1bcd3dd
5736036
01114fb
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 |
---|---|---|
|
@@ -280,7 +280,7 @@ public static <K, V> Map<K, V> newMapWithExpectedSize(int expectedSize) { | |
* @return a new pre-sized {@link HashMap} | ||
*/ | ||
public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { | ||
return new HashMap<>(capacity(expectedSize)); | ||
return HashMap.newHashMap(expectedSize); | ||
} | ||
|
||
/** | ||
|
@@ -292,7 +292,7 @@ public static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { | |
* @return a new pre-sized {@link HashMap} | ||
*/ | ||
public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expectedSize) { | ||
return new ConcurrentHashMap<>(capacity(expectedSize)); | ||
return new ConcurrentHashMap<>(expectedSize); | ||
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. ConcurrentHashMap does this sizing computation internally: according to the javadoc: and constructor code:
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. Similar to the above note, this utility method doesn't seem to be doing anything anymore, so perhaps it should be removed. 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. (here and above comment) Yeap, newMapWithExpectedSize, newHashMapWithExpectedSize, newConcurrentHashMapWithExpectedSize, newLinkedHashMapWithExpectedSize can all be inlined. |
||
} | ||
|
||
/** | ||
|
@@ -304,12 +304,7 @@ public static <K, V> Map<K, V> newConcurrentHashMapWithExpectedSize(int expected | |
* @return a new pre-sized {@link LinkedHashMap} | ||
*/ | ||
public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) { | ||
return new LinkedHashMap<>(capacity(expectedSize)); | ||
} | ||
|
||
static int capacity(int expectedSize) { | ||
assert expectedSize >= 0; | ||
return expectedSize < 2 ? expectedSize + 1 : (int) (expectedSize / 0.75 + 1.0); | ||
return LinkedHashMap.newLinkedHashMap(expectedSize); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,25 +293,25 @@ public void testToXContentWithMultipleProjects() throws IOException { | |
}, | ||
"projects": [ | ||
{ | ||
"id": "tb5W0bx765nDVIwqJPw92G", | ||
"id": "3LftaL7hgfXAsF60Gm6jcD", | ||
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. What happened 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. It looks like our calculation creates a bigger maps in some cases. This results in a different iteration order in several tests. 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.
|
||
"indices": { | ||
"common-index": { | ||
"9": { | ||
"another-index": { | ||
"5": { | ||
"retryable": false, | ||
"description": "index metadata (api)", | ||
"levels": [ "metadata_read", "metadata_write"] | ||
"description": "index read-only (api)", | ||
"levels": [ "write", "metadata_write"] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"id": "3LftaL7hgfXAsF60Gm6jcD", | ||
"id": "tb5W0bx765nDVIwqJPw92G", | ||
"indices": { | ||
"another-index": { | ||
"5": { | ||
"common-index": { | ||
"9": { | ||
"retryable": false, | ||
"description": "index read-only (api)", | ||
"levels": [ "write", "metadata_write"] | ||
"description": "index metadata (api)", | ||
"levels": [ "metadata_read", "metadata_write"] | ||
} | ||
} | ||
} | ||
|
@@ -468,40 +468,7 @@ public void testToXContentWithMultipleProjects() throws IOException { | |
"voting_config_exclusions": [] | ||
}, | ||
"projects": [ | ||
{ | ||
"id": "tb5W0bx765nDVIwqJPw92G", | ||
"templates": {}, | ||
"indices": { | ||
"common-index": { | ||
"version": 2, | ||
"mapping_version": 1, | ||
"settings_version": 1, | ||
"aliases_version": 1, | ||
"routing_num_shards": 3, | ||
"state": "open", | ||
"settings": { | ||
"index": { | ||
"number_of_shards": "3", | ||
"number_of_replicas": "1", | ||
"uuid": "tE62Ga40yvlmOSujUvruVw", | ||
"version": { "created": "%s" } | ||
} | ||
}, | ||
"mappings": {}, | ||
"aliases": [], | ||
"primary_terms": { "0":0, "1":0, "2":0 }, | ||
"in_sync_allocations": { "0":[], "1":[], "2":[] }, | ||
"rollover_info": {}, | ||
"mappings_updated_version": %s, | ||
"system": false, | ||
"timestamp_range": { "shards":[] }, | ||
"event_ingested_range": { "shards": [] } | ||
} | ||
}, | ||
"index-graveyard": { "tombstones": [] }, | ||
"reserved_state": {} | ||
}, | ||
{ | ||
{ | ||
"id": "3LftaL7hgfXAsF60Gm6jcD", | ||
"templates": {}, | ||
"indices": { | ||
|
@@ -559,12 +526,45 @@ public void testToXContentWithMultipleProjects() throws IOException { | |
"index-graveyard": { "tombstones": [] }, | ||
"reserved_state": {} | ||
}, | ||
{ | ||
{ | ||
"id": "WHyuJ0uqBYOPgHX9kYUXlZ", | ||
"templates": {}, | ||
"indices": {}, | ||
"index-graveyard": { "tombstones": [] }, | ||
"reserved_state": {} | ||
}, | ||
{ | ||
"id": "tb5W0bx765nDVIwqJPw92G", | ||
"templates": {}, | ||
"indices": { | ||
"common-index": { | ||
"version": 2, | ||
"mapping_version": 1, | ||
"settings_version": 1, | ||
"aliases_version": 1, | ||
"routing_num_shards": 3, | ||
"state": "open", | ||
"settings": { | ||
"index": { | ||
"number_of_shards": "3", | ||
"number_of_replicas": "1", | ||
"uuid": "tE62Ga40yvlmOSujUvruVw", | ||
"version": { "created": "%s" } | ||
} | ||
}, | ||
"mappings": {}, | ||
"aliases": [], | ||
"primary_terms": { "0":0, "1":0, "2":0 }, | ||
"in_sync_allocations": { "0":[], "1":[], "2":[] }, | ||
"rollover_info": {}, | ||
"mappings_updated_version": %s, | ||
"system": false, | ||
"timestamp_range": { "shards":[] }, | ||
"event_ingested_range": { "shards": [] } | ||
} | ||
}, | ||
"index-graveyard": { "tombstones": [] }, | ||
"reserved_state": {} | ||
} | ||
], | ||
"reserved_state": {} | ||
|
@@ -812,14 +812,14 @@ public void testToXContentWithMultipleProjects() throws IOException { | |
Version.CURRENT, | ||
IndexVersions.MINIMUM_COMPATIBLE, | ||
IndexVersion.current(), | ||
// project:tb5W0bx765nDVIwqJPw92G index:common-index | ||
IndexVersion.current(), | ||
IndexVersion.current(), | ||
// project:3LftaL7hgfXAsF60Gm6jcD index:another-index | ||
IndexVersion.current(), | ||
IndexVersion.current(), | ||
// project:3LftaL7hgfXAsF60Gm6jcD index:common-index | ||
IndexVersion.current(), | ||
IndexVersion.current(), | ||
// project:tb5W0bx765nDVIwqJPw92G index:common-index | ||
IndexVersion.current(), | ||
IndexVersion.current() | ||
); | ||
|
||
|
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.
Since 19 both HashMap and LinkedHashMap could be presized using standard lib factories.
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.
nit: perhaps then (doesn't have to be here) we should convert all the uses of this helper to use HashMap.newHashMap directly?