Skip to content

Commit

Permalink
IPROTO-265 Avoid byte[] allocation for buffer of OutputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
wburns authored and pruivo committed Oct 4, 2024
1 parent 4f663bf commit 66a09ae
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
10 changes: 7 additions & 3 deletions core/src/main/java/org/infinispan/protostream/ProtobufUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,24 @@ public static byte[] toWrappedByteArray(ImmutableSerializationContext ctx, Objec

public static byte[] toWrappedByteArray(ImmutableSerializationContext ctx, Object t, int bufferSize) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
WrappedMessage.write(ctx, TagWriterImpl.newInstanceNoBuffer(ctx, baos), t);
WrappedMessage.write(ctx, TagWriterImpl.newInstance(ctx, baos), t);
return baos.toByteArray();
}

public static ByteBuffer toWrappedByteBuffer(ImmutableSerializationContext ctx, Object t) throws IOException {
ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(DEFAULT_ARRAY_BUFFER_SIZE);
WrappedMessage.write(ctx, TagWriterImpl.newInstanceNoBuffer(ctx, baos), t);
WrappedMessage.write(ctx, TagWriterImpl.newInstance(ctx, baos), t);
return baos.getByteBuffer();
}

public static void toWrappedStream(ImmutableSerializationContext ctx, OutputStream out, Object t) throws IOException {
toWrappedStream(ctx, out, t, DEFAULT_STREAM_BUFFER_SIZE);
WrappedMessage.write(ctx, TagWriterImpl.newInstance(ctx, out), t);
}

/**
* @deprecated since 5.0.10 Please use {@link #toWrappedStream(ImmutableSerializationContext, OutputStream, Object)} with a {@link java.io.BufferedOutputStream} instead
*/
@Deprecated
public static void toWrappedStream(ImmutableSerializationContext ctx, OutputStream out, Object t, int bufferSize) throws IOException {
WrappedMessage.write(ctx, TagWriterImpl.newInstance(ctx, out, bufferSize), t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private static <T> void writeCustomObject(ImmutableSerializationContext ctx, Tag
((EnumMarshallerDelegate) marshallerDelegate).encode(WRAPPED_ENUM, (Enum<?>) t, out);
} else {
ByteArrayOutputStreamEx buffer = new ByteArrayOutputStreamEx();
TagWriterImpl nestedCtx = TagWriterImpl.newInstanceNoBuffer(ctx, buffer);
TagWriterImpl nestedCtx = TagWriterImpl.newInstance(ctx, buffer);
marshallerDelegate.marshall(nestedCtx, null, t);
nestedCtx.flush();
out.writeBytes(WRAPPED_MESSAGE, buffer.getByteBuffer());
Expand All @@ -340,7 +340,7 @@ private static void writeContainer(ImmutableSerializationContext ctx, TagWriter
out.writeUInt32(WRAPPED_CONTAINER_SIZE, containerSize);

ByteArrayOutputStreamEx buffer = new ByteArrayOutputStreamEx();
TagWriterImpl nestedCtx = TagWriterImpl.newInstanceNoBuffer(ctx, buffer);
TagWriterImpl nestedCtx = TagWriterImpl.newInstance(ctx, buffer);
marshallerDelegate.marshall(nestedCtx, null, container);
nestedCtx.flush();
out.writeBytes(WRAPPED_CONTAINER_MESSAGE, buffer.getByteBuffer());
Expand Down Expand Up @@ -397,7 +397,7 @@ private static void writeContainerElementWrapped(ImmutableSerializationContext c
return;
}
buffer.reset();
TagWriterImpl elementWriter = TagWriterImpl.newInstanceNoBuffer(ctx, buffer);
TagWriterImpl elementWriter = TagWriterImpl.newInstance(ctx, buffer);
writeMessage(ctx, elementWriter, e, true);
elementWriter.flush();
out.writeBytes(WRAPPED_MESSAGE, buffer.getByteBuffer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static byte[] fromCanonicalJSON(ImmutableSerializationContext ctx, Reader
ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);

try (reader; baos) {
TagWriter writer = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
TagWriter writer = TagWriterImpl.newInstance(ctx, baos);
JsonParser parser = jsonFactory.createParser(reader);
while (true) {
JsonToken token = parser.nextToken();
Expand Down Expand Up @@ -214,7 +214,7 @@ private static void processEnum(JsonParser parser, TagWriter writer, EnumDescrip

private static void processObject(ImmutableSerializationContext ctx, JsonParser parser, TagWriter writer, Descriptor messageDescriptor, Integer fieldNumber, boolean topLevel) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);
TagWriter nestedWriter = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
TagWriter nestedWriter = TagWriterImpl.newInstance(ctx, baos);

String currentField = null;

Expand Down Expand Up @@ -296,7 +296,7 @@ private static void processMap(ImmutableSerializationContext ctx, MapDescriptor
throw new IllegalStateException("Unexpected token");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);
TagWriter nestedWriter = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
TagWriter nestedWriter = TagWriterImpl.newInstance(ctx, baos);
String key = parser.getCurrentName();
switch (md.getKeyType()) {
case STRING -> nestedWriter.writeString(1, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.infinispan.protostream.ImmutableSerializationContext;
import org.infinispan.protostream.ProtobufTagMarshaller;
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.protostream.TagWriter;
import org.infinispan.protostream.descriptors.WireType;

Expand Down Expand Up @@ -66,9 +65,13 @@ public static TagWriterImpl newNestedInstance(ProtobufTagMarshaller.WriteContext
}

public static TagWriterImpl newInstance(ImmutableSerializationContext serCtx, OutputStream output) {
return new TagWriterImpl((SerializationContextImpl) serCtx, new OutputStreamEncoder(output, ProtobufUtil.DEFAULT_STREAM_BUFFER_SIZE));
return new TagWriterImpl((SerializationContextImpl) serCtx, new OutputStreamNoBufferEncoder(output));
}

/**
* @deprecated since 5.0.10 Please use {@link #newInstance(ImmutableSerializationContext, OutputStream)} with a {@link java.io.BufferedOutputStream} instead
*/
@Deprecated
public static TagWriterImpl newInstance(ImmutableSerializationContext serCtx, OutputStream output, int bufferSize) {
return new TagWriterImpl((SerializationContextImpl) serCtx, new OutputStreamEncoder(output, bufferSize));
}
Expand All @@ -90,6 +93,10 @@ public static TagWriterImpl newInstance(ImmutableSerializationContext serCtx) {
return new TagWriterImpl((SerializationContextImpl) serCtx, new NoOpEncoder());
}

/**
* @deprecated since 5.0.10 Please use {@link #newInstance(ImmutableSerializationContext, OutputStream)}
*/
@Deprecated
public static TagWriterImpl newInstanceNoBuffer(ImmutableSerializationContext ctx, OutputStream out) {
return new TagWriterImpl((SerializationContextImpl) ctx, new OutputStreamNoBufferEncoder(out));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public boolean hasTag(int tag) {
@Override
public void writeExternal(ObjectOutput out) throws IOException {
ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx();
TagWriter output = TagWriterImpl.newInstanceNoBuffer(null, baos);
TagWriter output = TagWriterImpl.newInstance(null, baos);
writeTo(output);
output.flush();
ByteBuffer buffer = baos.getByteBuffer();
Expand Down

0 comments on commit 66a09ae

Please sign in to comment.