Skip to content

Commit 0cf652a

Browse files
MapConverter: avoid HashMap re-hashing.
1 parent d1873fe commit 0cf652a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

objectbox-java/src/main/java/io/objectbox/converter/FlexMapConverter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ private Map<Object, Object> buildMap(FlexBuffers.Map map) {
164164
int entryCount = map.size();
165165
FlexBuffers.KeyVector keys = map.keys();
166166
FlexBuffers.Vector values = map.values();
167-
Map<Object, Object> resultMap = new HashMap<>(entryCount);
167+
// Note: avoid HashMap re-hashing by choosing large enough initial capacity.
168+
// From docs: If the initial capacity is greater than the maximum number of entries divided by the load factor,
169+
// no rehash operations will ever occur.
170+
// So set initial capacity based on default load factor 0.75 accordingly.
171+
Map<Object, Object> resultMap = new HashMap<>((int) (entryCount / 0.75 + 1));
168172
for (int i = 0; i < entryCount; i++) {
169173
String rawKey = keys.get(i).toString();
170174
Object key = convertToKey(rawKey);

0 commit comments

Comments
 (0)