Skip to content

Commit

Permalink
Optimize JacksonPersister#hydrateFromBytes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schlosna committed Sep 22, 2023
1 parent 9ef9d34 commit cedfcb7
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Throwables;
import com.palantir.atlasdb.persist.api.ReusablePersister;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

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

0 comments on commit cedfcb7

Please sign in to comment.