Skip to content

Commit 945605b

Browse files
committed
immutable ContainerNodes to avoid deep copies
Note, this requires Jackson 2.7.x.
1 parent 3490a7d commit 945605b

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.fasterxml.jackson.core</groupId>
4747
<artifactId>jackson-databind</artifactId>
48-
<version>2.6.4</version>
48+
<version>2.7.0</version>
4949
</dependency>
5050
</dependencies>
5151
<properties>

src/main/java/com/maxmind/db/CHMCache.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.concurrent.ConcurrentHashMap;
55

66
import com.fasterxml.jackson.databind.JsonNode;
7-
import com.fasterxml.jackson.databind.node.ContainerNode;
87

98
/**
109
* A simplistic cache using a {@link ConcurrentHashMap}. There's no eviction
@@ -42,9 +41,6 @@ public JsonNode get(int key, Loader loader) throws IOException {
4241
}
4342
}
4443
}
45-
if (value instanceof ContainerNode) {
46-
value = value.deepCopy();
47-
}
4844
return value;
4945
}
5046

src/main/java/com/maxmind/db/Decoder.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import java.nio.charset.CharacterCodingException;
77
import java.nio.charset.Charset;
88
import java.nio.charset.CharsetDecoder;
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
914

1015
import com.fasterxml.jackson.databind.JsonNode;
1116
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -257,26 +262,27 @@ private static BooleanNode decodeBoolean(int size)
257262
}
258263

259264
private JsonNode decodeArray(int size) throws IOException {
260-
ArrayNode array = OBJECT_MAPPER.createArrayNode();
261265

266+
List<JsonNode> array = new ArrayList<JsonNode>(size);
262267
for (int i = 0; i < size; i++) {
263268
JsonNode r = this.decode();
264269
array.add(r);
265270
}
266271

267-
return array;
272+
return new ArrayNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableList(array));
268273
}
269274

270275
private JsonNode decodeMap(int size) throws IOException {
271-
ObjectNode map = OBJECT_MAPPER.createObjectNode();
276+
int capacity = (int) (size / 0.75F + 1.0F);
277+
Map<String, JsonNode> map = new HashMap<String, JsonNode>(capacity);
272278

273279
for (int i = 0; i < size; i++) {
274280
String key = this.decode().asText();
275281
JsonNode value = this.decode();
276-
map.set(key, value);
282+
map.put(key, value);
277283
}
278284

279-
return map;
285+
return new ObjectNode(OBJECT_MAPPER.getNodeFactory(), Collections.unmodifiableMap(map));
280286
}
281287

282288
private byte[] getByteArray(int length) {

0 commit comments

Comments
 (0)