Java 内存有效多值映射
您好,我有以下问题: 我将字符串和相应的整数值列表存储在Java 内存有效多值映射,java,performance,memory,map,memory-efficient,Java,Performance,Memory,Map,Memory Efficient,您好,我有以下问题: 我将字符串和相应的整数值列表存储在多值映射中 我存储了大约130000000个字符串,一个字符串最多可以有500个或更多的值。 对于每一个值,我都可以在地图上随机访问。所以最坏的情况是130000*500个put呼叫。现在,映射的速度很好,但内存开销相当高。MultiValueMap与HashMap/TreeMap不同,取决于存储在映射中的整数值,大量堆内存开销可能是由不同的整数实例引起的,这些实例占用的内存比原始int值多得多 考虑使用从String到浮动的众多IntAr
多值映射中
我存储了大约130000000个字符串,一个字符串最多可以有500个或更多的值。
对于每一个值,我都可以在地图上随机访问。所以最坏的情况是130000*500个put呼叫。现在,映射的速度很好,但内存开销相当高。
MultiValueMap
与HashMap/TreeMap不同,取决于存储在映射中的整数值,大量堆内存开销可能是由不同的整数实例引起的,这些实例占用的内存比原始int值多得多
考虑使用从String
到浮动的众多IntArrayList
实现之一的Map
(例如,在Colt或Java的原始集合中),它基本上实现了一个由int数组支持的列表,而不是由整数实例数组支持。您可以使用来大幅减少内存使用量
此外,还有更激进的解决方案(需要重新实施):
- 或者
如果您切换到Guava的Multimap——我不知道这是否适用于您的应用程序——您可能可以使用Trove并获得
ListMultimap<String, Integer> multimap = Multimaps.newListMultimap(
new HashMap<String, Collection<Integer>>(),
new Supplier<List<Integer>>() {
public List<Integer> get() {
return new TIntListDecorator();
}
});
ListMultimap multimap=Multimaps.newListMultimap(
新建HashMap(),
新供应商(){
公共列表get(){
返回新的TIntListDecorator();
}
});
这将生成一个ListMultimap
,它使用HashMap
映射到List
由int[]
数组支持的值,该数组应该是内存有效的,尽管由于装箱,您将支付少量的速度代价。你可以做类似于 MultValueMaP的事情,虽然我不知道这是从哪个库来的。 首先,考虑整数所取的内存。你说的范围大约是0-400万。24位足以表示16777216个不同的值。如果这是可以接受的,您可以使用字节数组作为整数,每个整数3个字节,节省25%。您必须索引到数组中,如下所示:
int getPackedInt(byte[] array, int index) {
int i = index*3;
return ((array[i] & 0xFF)<<16) + ((array[i+1] & 0xFF) <<8) + (array[i+2] & 0xFF);
}
int storePackedInt(byte[] array, int index, int value) {
assert value >= 0 && value <= 0xFFFFFF;
int i = index*3;
array[i] = (byte)((value>>16) & 0xFF);
array[i+1] = (byte)((value>>8) & 0xFF);
array[i+2] = (byte)(value & 0xFF);
}
int getPackedInt(字节[]数组,int索引){
int i=指数*3;
返回((数组[i]&0xFF)8)&0xFF);
数组[i+2]=(字节)(值&0xFF);
}
你能谈谈整数的分布吗?如果其中许多适合16位,则可以使用每个数字的字节数可变的编码(类似于UTF-8用于表示字符)
接下来,考虑是否可以在字符串上保存内存。弦的特征是什么?通常需要多长时间?许多字符串是否共享前缀?根据应用程序的特点定制的压缩方案可以节省大量空间(正如falsarella指出的)。或者,如果许多字符串共享前缀,则将它们存储在某种类型的搜索trie中可能更有效。(有一种称为“patricia”的trie可能适用于此应用程序。)另外,请注意,在trie中搜索字符串可能比搜索哈希映射更快(尽管您必须进行基准测试,以确定这在应用程序中是否正确)
字符串都是ASCII码吗?如果是这样,用于字符串的50%内存将被浪费,因为Javachar
是16位的。同样,在这种情况下,可以考虑使用字节数组。
如果只需要查找字符串,而不是迭代存储的字符串,也可以考虑一些非常规的东西:散列字符串,只保留散列。由于不同的字符串可以散列到相同的值,因此搜索可能仍然会“找到”从未存储过的字符串。但是如果你为散列值使用了足够的位(以及一个好的散列函数),你可以使这个机会变得非常小,以至于在宇宙的估计寿命内几乎肯定不会发生
最后,还有结构本身的内存,它保存字符串和整数。我已经建议使用trie,但是如果您决定不这样做,那么没有什么比并行数组使用更少的内存了——一个排序的字符串数组(正如您所说,您可以对其进行二进制搜索)和一个并行的整数数组。在执行二进制搜索以查找字符串数组中的索引后,可以使用相同的索引访问整数数组的数组
在构建结构时,如果您确实认为搜索trie是一个不错的选择,我将直接使用它。否则,您可以执行两次操作:一次构建一组字符串(然后将它们放入数组并对其进行排序),第二次添加整数数组。如果键字符串中有模式,特别是公共根,那么a可能是一种有效的方法,可以存储更少的数据
这是工作地图的代码
注意:关于使用EntrySet
在Map
s上迭代的通常建议不适用于Trie
s。它们在Trie
中效率极低,因此请尽可能避免请求
/**
* Implementation of a Trie structure.
*
* A Trie is a compact form of tree that takes advantage of common prefixes
* to the keys.
*
* A normal HashSet will take the key and compute a hash from it, this hash will
* be used to locate the value through various methods but usually some kind
* of bucket system is used. The memory footprint resulting becomes something
* like O(n).
*
* A Trie structure essentuially combines all common prefixes into a single key.
* For example, holding the strings A, AB, ABC and ABCD will only take enough
* space to record the presence of ABCD. The presence of the others will be
* recorded as flags within the record of ABCD structure at zero cost.
*
* This structure is useful for holding similar strings such as product IDs or
* credit card numbers.
*
*/
public class TrieMap<V> extends AbstractMap<String, V> implements Map<String, V> {
/**
* Map each character to a sub-trie.
*
* Could replace this with a 256 entry array of Tries but this will handle
* multibyte character sets and I can discard empty maps.
*
* Maintained at null until needed (for better memory footprint).
*
*/
protected Map<Character, TrieMap<V>> children = null;
/**
* Here we store the map contents.
*/
protected V leaf = null;
/**
* Set the leaf value to a new setting and return the old one.
*
* @param newValue
* @return old value of leaf.
*/
protected V setLeaf(V newValue) {
V old = leaf;
leaf = newValue;
return old;
}
/**
* I've always wanted to name a method something like this.
*/
protected void makeChildren () {
if ( children == null ) {
// Use a TreeMap to ensure sorted iteration.
children = new TreeMap<Character, TrieMap<V>>();
}
}
/**
* Finds the TrieMap that "should" contain the key.
*
* @param key
*
* The key to find.
*
* @param grow
*
* Set to true to grow the Trie to fit the key.
*
* @return
*
* The sub Trie that "should" contain the key or null if key was not found and
* grow was false.
*/
protected TrieMap<V> find(String key, boolean grow) {
if (key.length() == 0) {
// Found it!
return this;
} else {
// Not at end of string.
if (grow) {
// Grow the tree.
makeChildren();
}
if (children != null) {
// Ask the kids.
char ch = key.charAt(0);
TrieMap<V> child = children.get(ch);
if (child == null && grow) {
// Make the child.
child = new TrieMap<V>();
// Store the child.
children.put(ch, child);
}
if (child != null) {
// Find it in the child.
return child.find(tail(key), grow);
}
}
}
return null;
}
/**
* Remove the head (first character) from the string.
*
* @param s
*
* The string.
*
* @return
*
* The same string without the first (head) character.
*
*/
// Suppress warnings over taking a subsequence
private String tail(String s) {
return s.substring(1, s.length());
}
/**
*
* Add a new value to the map.
*
* Time footprint = O(s.length).
*
* @param s
*
* The key defining the place to add.
*
* @param value
*
* The value to add there.
*
* @return
*
* The value that was there, or null if it wasn't.
*
*/
@Override
public V put(String key, V value) {
V old = null;
// If empty string.
if (key.length() == 0) {
old = setLeaf(value);
} else {
// Find it.
old = find(key, true).put("", value);
}
return old;
}
/**
* Gets the value at the specified key position.
*
* @param o
*
* The key to the location.
*
* @return
*
* The value at that location, or null if there is no value at that location.
*/
@Override
public V get(Object o) {
V got = null;
if (o != null) {
String key = (String) o;
TrieMap<V> it = find(key, false);
if (it != null) {
got = it.leaf;
}
} else {
throw new NullPointerException("Nulls not allowed.");
}
return got;
}
/**
* Remove the value at the specified location.
*
* @param o
*
* The key to the location.
*
* @return
*
* The value that was removed, or null if there was no value at that location.
*/
@Override
public V remove(Object o) {
V old = null;
if (o != null) {
String key = (String) o;
if (key.length() == 0) {
// Its me!
old = leaf;
leaf = null;
} else {
TrieMap<V> it = find(key, false);
if (it != null) {
old = it.remove("");
}
}
} else {
throw new NullPointerException("Nulls not allowed.");
}
return old;
}
/**
* Count the number of values in the structure.
*
* @return
*
* The number of values in the structure.
*/
@Override
public int size() {
// If I am a leaf then size increases by 1.
int size = leaf != null ? 1 : 0;
if (children != null) {
// Add sizes of all my children.
for (Character c : children.keySet()) {
size += children.get(c).size();
}
}
return size;
}
/**
* Is the tree empty?
*
* @return
*
* true if the tree is empty.
* false if there is still at least one value in the tree.
*/
@Override
public boolean isEmpty() {
// I am empty if I am not a leaf and I have no children
// (slightly quicker than the AbstaractCollection implementation).
return leaf == null && (children == null || children.isEmpty());
}
/**
* Returns all keys as a Set.
*
* @return
*
* A HashSet of all keys.
*
* Note: Although it returns Set<S> it is actually a Set<String> that has been
* home-grown because the original keys are not stored in the structure
* anywhere.
*/
@Override
public Set<String> keySet() {
// Roll them a temporary list and give them a Set from it.
return new HashSet<String>(keyList());
}
/**
* List all my keys.
*
* @return
*
* An ArrayList of all keys in the tree.
*
* Note: Although it returns List<S> it is actually a List<String> that has been
* home-grown because the original keys are not stored in the structure
* anywhere.
*
*/
protected List<String> keyList() {
List<String> contents = new ArrayList<String>();
if (leaf != null) {
// If I am a leaf, a null string is in the set.
contents.add((String) "");
}
// Add all sub-tries.
if (children != null) {
for (Character c : children.keySet()) {
TrieMap<V> child = children.get(c);
List<String> childContents = child.keyList();
for (String subString : childContents) {
// All possible substrings can be prepended with this character.
contents.add((String) (c + subString.toString()));
}
}
}
return contents;
}
/**
* Does the map contain the specified key.
*
* @param key
*
* The key to look for.
*
* @return
*
* true if the key is in the Map.
* false if not.
*/
public boolean containsKey(String key) {
TrieMap<V> it = find(key, false);
if (it != null) {
return it.leaf != null;
}
return false;
}
/**
* Represent me as a list.
*
* @return
*
* A String representation of the tree.
*/
@Override
public String toString() {
List<String> list = keyList();
//Collections.sort((List<String>)list);
StringBuilder sb = new StringBuilder();
Separator comma = new Separator(",");
sb.append("{");
for (String s : list) {
sb.append(comma.sep()).append(s).append("=").append(get(s));
}
sb.append("}");
return sb.toString();
}
/**
* Clear down completely.
*/
@Override
public void clear() {
children = null;
leaf = null;
}
/**
* Return a list of key/value pairs.
*
* @return
*
* The entry set.
*/
public Set<Map.Entry<String, V>> entrySet() {
Set<Map.Entry<String, V>> entries = new HashSet<Map.Entry<String, V>>();
List<String> keys = keyList();
for (String key : keys) {
entries.add(new Entry<String,V>(key, get(key)));
}
return entries;
}
/**
* An entry.
*
* @param <S>
*
* The type of the key.
*
* @param <V>
*
* The type of the value.
*/
private static class Entry<S, V> implements Map.Entry<S, V> {
protected S key;
protected V value;
public Entry(S key, V value) {
this.key = key;
this.value = value;
}
public S getKey() {
return key;
}
public V getValue() {
return value;
}
public V setValue(V newValue) {
V oldValue = value;
value = newValue;
return oldValue;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof TrieMap.Entry)) {
return false;
}
Entry e = (Entry) o;
return (key == null ? e.getKey() == null : key.equals(e.getKey()))
&& (value == null ? e.getValue() == null : value.equals(e.getValue()));
}
@Override
public int hashCode() {
int keyHash = (key == null ? 0 : key.hashCode());
int valueHash = (value == null ? 0 : value.hashCode());
return keyHash ^ valueHash;
}
@Override
public String toString() {
return key + "=" + value;
}
}
}
/**
*Trie结构的实现。
*
*Trie是一种紧凑的树形式,它利用了常见的前缀
*到钥匙那儿去。
*
*一个普通的散列集将获取密钥并从中计算一个散列,这个散列将
*用于通过各种方法定位值,但通常是某种方法
*采用了铲斗系统。由此产生的内存占用将成为一个问题
*像O(n)。
*
*Trie结构基本上将所有公共前缀组合成一个键。
*例如,保持字符串A、AB、ABC和ABCD只需要足够的时间
*用于记录是否存在ABCD的空间。其他人的存在将是
*记录为z处ABCD结构记录中的标志