Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit cedfcb7

Browse files
committed
Optimize JacksonPersister#hydrateFromBytes
Optimize to avoid allocation of heap ByteBuffer via InputStreamReader. Remove after upgrade to Jackson 2.16. see: FasterXML/jackson-core#1081 and FasterXML/jackson-benchmarks#9
1 parent 9ef9d34 commit cedfcb7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

atlasdb-client/src/main/java/com/palantir/atlasdb/persister/JacksonPersister.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import com.fasterxml.jackson.databind.ObjectMapper;
2020
import com.google.common.base.Throwables;
2121
import com.palantir.atlasdb.persist.api.ReusablePersister;
22+
import java.io.ByteArrayInputStream;
2223
import java.io.IOException;
24+
import java.io.StringReader;
25+
import java.nio.charset.StandardCharsets;
2326

2427
/**
2528
* A {@link ReusablePersister} that uses an {@link ObjectMapper} to serialize and deserialize objects
@@ -38,7 +41,14 @@ public JacksonPersister(Class<T> typeRef, ObjectMapper mapper) {
3841
@Override
3942
public final T hydrateFromBytes(byte[] input) {
4043
try {
41-
return mapper.readValue(input, typeRef);
44+
if (input.length <= 8192 && mapper.getFactory().canUseCharArrays()) {
45+
// Optimize to avoid allocation of heap ByteBuffer via InputStreamReader.
46+
// Remove after upgrade to Jackson 2.16.
47+
// see: https://github.com/FasterXML/jackson-core/pull/1081
48+
// and https://github.com/FasterXML/jackson-benchmarks/pull/9
49+
return mapper.readValue(new StringReader(new String(input, StandardCharsets.UTF_8)), typeRef);
50+
}
51+
return mapper.readValue(new ByteArrayInputStream(input), typeRef);
4252
} catch (IOException e) {
4353
throw Throwables.propagate(e);
4454
}

0 commit comments

Comments
 (0)