From 3c21c64c7891be68846400ba7d8b1f088b663d04 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Thu, 29 Sep 2016 18:55:53 -0400 Subject: [PATCH] Use size hint for HashMap in multiGet. Similar to https://github.com/facebook/rocksdb/pull/1344 (#1367) --- java/src/main/java/org/rocksdb/RocksDB.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index 6430e23d4..d16ae0435 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -824,7 +824,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, keyLengths); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap = + new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if(values[i] == null) { continue; @@ -836,6 +837,12 @@ public class RocksDB extends RocksObject { return keyValueMap; } + private static int computeCapacityHint(final int estimatedNumberOfItems) { + // Default load factor for HashMap is 0.75, so N * 1.5 will be at the load + // limit. We add +1 for a buffer. + return (int)Math.ceil(estimatedNumberOfItems * 1.5 + 1.0); + } + /** * Returns a map of keys for which values were found in DB. *

@@ -880,7 +887,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, keyLengths, cfHandles); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap = + new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if (values[i] == null) { continue; @@ -915,7 +923,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, keysArray, keyOffsets, keyLengths); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap = + new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if(values[i] == null) { continue; @@ -971,7 +980,8 @@ public class RocksDB extends RocksObject { final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, keysArray, keyOffsets, keyLengths, cfHandles); - final Map keyValueMap = new HashMap<>(); + final Map keyValueMap + = new HashMap<>(computeCapacityHint(values.length)); for(int i = 0; i < values.length; i++) { if(values[i] == null) { continue;