replace LinkedHashMap with a more memory efficient implementation
This saves approximately 50MB of heap space.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
|
||||
dependencies {
|
||||
|
||||
compile project(':pdb-utils')
|
||||
}
|
||||
@@ -1,28 +1,27 @@
|
||||
package org.lucares.pdb.api;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import org.lucares.utils.MiniMap;
|
||||
|
||||
public class Tags {
|
||||
public static final Tags EMPTY = new Tags();
|
||||
|
||||
private final Map<String, Tag> tags;
|
||||
private final MiniMap<String, Tag> tags;
|
||||
|
||||
private int cachedHash = 0;
|
||||
|
||||
private Tags() {
|
||||
super();
|
||||
tags = Collections.emptyMap();
|
||||
tags = MiniMap.emptyMap();
|
||||
}
|
||||
|
||||
private Tags(final Map<String, Tag> tags) {
|
||||
private Tags(final MiniMap<String, Tag> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
@@ -31,13 +30,13 @@ public class Tags {
|
||||
}
|
||||
|
||||
public static Tags create(final String key, final String value) {
|
||||
final Map<String, Tag> tags = new LinkedHashMap<>(1);
|
||||
final MiniMap<String, Tag> tags = new MiniMap<>();
|
||||
tags.put(key, new Tag(key, value));
|
||||
return new Tags(tags);
|
||||
}
|
||||
|
||||
public static Tags create(final String key1, final String value1, final String key2, final String value2) {
|
||||
final Map<String, Tag> tags = new LinkedHashMap<>(2);
|
||||
final MiniMap<String, Tag> tags = new MiniMap<>();
|
||||
tags.put(key1, new Tag(key1, value1));
|
||||
tags.put(key2, new Tag(key2, value2));
|
||||
return new Tags(tags);
|
||||
@@ -45,7 +44,7 @@ public class Tags {
|
||||
|
||||
public static Tags create(final String key1, final String value1, final String key2, final String value2,
|
||||
final String key3, final String value3) {
|
||||
final Map<String, Tag> tags = new LinkedHashMap<>(3);
|
||||
final MiniMap<String, Tag> tags = new MiniMap<>();
|
||||
tags.put(key1, new Tag(key1, value1));
|
||||
tags.put(key2, new Tag(key2, value2));
|
||||
tags.put(key3, new Tag(key3, value3));
|
||||
@@ -56,7 +55,7 @@ public class Tags {
|
||||
Objects.requireNonNull(key, "key must not be null");
|
||||
Objects.requireNonNull(value, "value must not be null");
|
||||
|
||||
final Map<String, Tag> newTags = new LinkedHashMap<>(tags);
|
||||
final MiniMap<String, Tag> newTags = new MiniMap<>(tags);
|
||||
|
||||
newTags.put(key, new Tag(key, value));
|
||||
|
||||
@@ -85,8 +84,12 @@ public class Tags {
|
||||
}
|
||||
|
||||
public void forEach(final BiConsumer<String, String> keyValueConsumer) {
|
||||
for (final Map.Entry<String, Tag> e : tags.entrySet()) {
|
||||
keyValueConsumer.accept(e.getKey(), e.getValue().getValue());
|
||||
|
||||
Set<String> keys = tags.keySet();
|
||||
|
||||
for (String key : keys) {
|
||||
final Tag value = tags.get(key);
|
||||
keyValueConsumer.accept(key, value.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user