Skip to content

Commit

Permalink
Use size hint for HashMap in multiGet. Similar to facebook#1344
Browse files Browse the repository at this point in the history
  • Loading branch information
adamretter committed Sep 29, 2016
1 parent 87dfc1d commit c1b5f05
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions java/src/main/java/org/rocksdb/RocksDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,8 @@ public Map<byte[], byte[]> multiGet(final List<byte[]> keys)
final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets,
keyLengths);

final Map<byte[], byte[]> keyValueMap = new HashMap<>();
final Map<byte[], byte[]> keyValueMap =
new HashMap<>(computeCapacityHint(values.length));
for(int i = 0; i < values.length; i++) {
if(values[i] == null) {
continue;
Expand All @@ -836,6 +837,12 @@ public Map<byte[], byte[]> multiGet(final List<byte[]> keys)
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.
* <p>
Expand Down Expand Up @@ -880,7 +887,8 @@ public Map<byte[], byte[]> multiGet(
final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets,
keyLengths, cfHandles);

final Map<byte[], byte[]> keyValueMap = new HashMap<>();
final Map<byte[], byte[]> keyValueMap =
new HashMap<>(computeCapacityHint(values.length));
for(int i = 0; i < values.length; i++) {
if (values[i] == null) {
continue;
Expand Down Expand Up @@ -915,7 +923,8 @@ public Map<byte[], byte[]> multiGet(final ReadOptions opt,
final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_,
keysArray, keyOffsets, keyLengths);

final Map<byte[], byte[]> keyValueMap = new HashMap<>();
final Map<byte[], byte[]> keyValueMap =
new HashMap<>(computeCapacityHint(values.length));
for(int i = 0; i < values.length; i++) {
if(values[i] == null) {
continue;
Expand Down Expand Up @@ -971,7 +980,8 @@ public Map<byte[], byte[]> multiGet(final ReadOptions opt,
final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_,
keysArray, keyOffsets, keyLengths, cfHandles);

final Map<byte[], byte[]> keyValueMap = new HashMap<>();
final Map<byte[], byte[]> keyValueMap
= new HashMap<>(computeCapacityHint(values.length));
for(int i = 0; i < values.length; i++) {
if(values[i] == null) {
continue;
Expand Down

0 comments on commit c1b5f05

Please sign in to comment.