Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPROTO-265 Avoid byte[] allocation for buffer of OutputStream #333

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be TagWriterImpl.newInstanceNoBuffer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I see you changed newInstance(ImmutableSerializationContext serCtx, OutputStream output).
So newInstanceNoBuffer is kind of duplicated/irrelevant at this point!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware of newInstanceNoBuffer! Yeah, no reason newInstance should ever use the buffered version imo. Updated to deprecate newInstanceNoBuffer as well.

}

/**
* @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