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

Commit 925e868

Browse files
authored
Optimize JacksonPersister#hydrateFromBytes (#6750)
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 7fc71ca commit 925e868

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-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
}

changelog/@unreleased/pr-6750.v2.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type: improvement
2+
improvement:
3+
description: |-
4+
Optimize to avoid allocation of heap ByteBuffer via InputStreamReader. Remove after upgrade to Jackson 2.16.
5+
6+
see: https://github.com/FasterXML/jackson-core/pull/1081 and https://github.com/FasterXML/jackson-benchmarks/pull/9
7+
links:
8+
- https://github.com/palantir/atlasdb/pull/6750

0 commit comments

Comments
 (0)