提交 3c21c64c 编写于 作者: A Adam Retter 提交者: yiwu-arbug

Use size hint for HashMap in multiGet. Similar to...

Use size hint for HashMap in multiGet. Similar to https://github.com/facebook/rocksdb/pull/1344 (#1367)
上级 13f7a01f
......@@ -824,7 +824,8 @@ public class RocksDB extends RocksObject {
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;
......@@ -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.
* <p>
......@@ -880,7 +887,8 @@ public class RocksDB extends RocksObject {
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;
......@@ -915,7 +923,8 @@ public class RocksDB extends RocksObject {
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;
......@@ -971,7 +980,8 @@ public class RocksDB extends RocksObject {
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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册