Skip to content

Commit 393f446

Browse files
StringMapConverter: avoid HashMap re-hashing.
1 parent ebb9eed commit 393f446

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public Map<String, String> convertToEntityProperty(byte[] databaseValue) {
6262
int entryCount = map.size();
6363
FlexBuffers.KeyVector keys = map.keys();
6464
FlexBuffers.Vector values = map.values();
65-
Map<String, String> resultMap = new HashMap<>(entryCount);
65+
// Note: avoid HashMap re-hashing by choosing large enough initial capacity.
66+
// From docs: If the initial capacity is greater than the maximum number of entries divided by the load factor,
67+
// no rehash operations will ever occur.
68+
// So set initial capacity based on default load factor 0.75 accordingly.
69+
Map<String, String> resultMap = new HashMap<>((int) (entryCount / 0.75 + 1));
6670
for (int i = 0; i < entryCount; i++) {
6771
String key = keys.get(i).toString();
6872
String value = values.get(i).asString();

0 commit comments

Comments
 (0)