Use size hint for HashMap in multiGet. Similar to https://github.com/facebook/rocksdb/pull/1344 (#1367)

This commit is contained in:
Adam Retter 2016-09-29 18:55:53 -04:00 committed by yiwu-arbug
parent 13f7a01f61
commit 3c21c64c78

View File

@ -824,7 +824,8 @@ public class RocksDB extends RocksObject {
final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets,
keyLengths); 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++) { for(int i = 0; i < values.length; i++) {
if(values[i] == null) { if(values[i] == null) {
continue; continue;
@ -836,6 +837,12 @@ public class RocksDB extends RocksObject {
return keyValueMap; 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. * Returns a map of keys for which values were found in DB.
* <p> * <p>
@ -880,7 +887,8 @@ public class RocksDB extends RocksObject {
final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets, final byte[][] values = multiGet(nativeHandle_, keysArray, keyOffsets,
keyLengths, cfHandles); 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++) { for(int i = 0; i < values.length; i++) {
if (values[i] == null) { if (values[i] == null) {
continue; continue;
@ -915,7 +923,8 @@ public class RocksDB extends RocksObject {
final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_,
keysArray, keyOffsets, keyLengths); 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++) { for(int i = 0; i < values.length; i++) {
if(values[i] == null) { if(values[i] == null) {
continue; continue;
@ -971,7 +980,8 @@ public class RocksDB extends RocksObject {
final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_, final byte[][] values = multiGet(nativeHandle_, opt.nativeHandle_,
keysArray, keyOffsets, keyLengths, cfHandles); 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++) { for(int i = 0; i < values.length; i++) {
if(values[i] == null) { if(values[i] == null) {
continue; continue;