Skip to content

Commit

Permalink
The JacksonCodec implementation of toString/toBuffer lacks of efficie…
Browse files Browse the repository at this point in the history
…ncy compared to the DatabindCodec implementation which uses the buffer recyler of Jackson.

Update the JacksonCodec implementation to use the buffer recycler instead.
  • Loading branch information
vietj committed Jul 7, 2024
1 parent 5d0d5d9 commit fd37a36
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/main/java/io/vertx/core/json/jackson/JacksonCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package io.vertx.core.json.jackson;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import com.fasterxml.jackson.core.type.TypeReference;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
Expand Down Expand Up @@ -108,35 +109,33 @@ public <T> T fromValue(Object json, TypeReference<T> type) {

@Override
public String toString(Object object, boolean pretty) throws EncodeException {
StringWriter sw = new StringWriter();
JsonGenerator generator = createGenerator(sw, pretty);
try {
com.fasterxml.jackson.core.util.BufferRecycler br = factory._getBufferRecycler();
try (SegmentedStringWriter sw = new SegmentedStringWriter(br)) {
com.fasterxml.jackson.core.JsonGenerator generator = createGenerator(sw, pretty);
encodeJson(object, generator);
generator.flush();
return sw.toString();
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
generator.close();
return sw.getAndClear();
} catch (java.io.IOException e) {
throw new io.vertx.core.json.EncodeException(e.getMessage(), e);
} finally {
close(generator);
br.releaseToPool();
}
}

@Override
public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
ByteBuf buf = Unpooled.buffer();
// There is no need to use a try with resources here as jackson
// is a well-behaved and always calls the closes all streams in the
// "finally" block bellow.
ByteBufOutputStream out = new ByteBufOutputStream(buf);
JsonGenerator generator = createGenerator(out, pretty);
try {
com.fasterxml.jackson.core.util.BufferRecycler br = factory._getBufferRecycler();
try (com.fasterxml.jackson.core.util.ByteArrayBuilder bb = new com.fasterxml.jackson.core.util.ByteArrayBuilder(br)) {
com.fasterxml.jackson.core.JsonGenerator generator = createGenerator(bb, pretty);
encodeJson(object, generator);
generator.flush();
return Buffer.buffer(buf);
} catch (IOException e) {
throw new EncodeException(e.getMessage(), e);
generator.close();
byte[] result = bb.toByteArray();
bb.release();
return io.vertx.core.buffer.Buffer.buffer(result);
} catch (java.io.IOException e) {
throw new io.vertx.core.json.EncodeException(e.getMessage(), e);
} finally {
close(generator);
br.releaseToPool();
}
}

Expand Down Expand Up @@ -286,7 +285,7 @@ static void close(Closeable parser) {
}

// In recursive calls, the callee is in charge of opening and closing the data structure
private static void encodeJson(Object json, JsonGenerator generator) throws EncodeException {
public static void encodeJson(Object json, JsonGenerator generator) throws EncodeException {
try {
if (json instanceof JsonObject) {
json = ((JsonObject)json).getMap();
Expand Down

0 comments on commit fd37a36

Please sign in to comment.