From 9349a40c1b228a4d110454179f3798c0db8c64d7 Mon Sep 17 00:00:00 2001 From: William Burns Date: Thu, 3 Oct 2024 14:40:56 -0700 Subject: [PATCH] IPROTO-265 Avoid byte[] allocation for buffer of OutputStream --- .../java/org/infinispan/protostream/ProtobufUtil.java | 10 +++++++--- .../org/infinispan/protostream/WrappedMessage.java | 6 +++--- .../org/infinispan/protostream/impl/JsonUtils.java | 6 +++--- .../infinispan/protostream/impl/TagWriterImpl.java | 11 +++++++++-- .../protostream/impl/UnknownFieldSetImpl.java | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/infinispan/protostream/ProtobufUtil.java b/core/src/main/java/org/infinispan/protostream/ProtobufUtil.java index 833d48fdf..c6f402a83 100644 --- a/core/src/main/java/org/infinispan/protostream/ProtobufUtil.java +++ b/core/src/main/java/org/infinispan/protostream/ProtobufUtil.java @@ -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); } diff --git a/core/src/main/java/org/infinispan/protostream/WrappedMessage.java b/core/src/main/java/org/infinispan/protostream/WrappedMessage.java index 27179e028..ba467ae66 100644 --- a/core/src/main/java/org/infinispan/protostream/WrappedMessage.java +++ b/core/src/main/java/org/infinispan/protostream/WrappedMessage.java @@ -316,7 +316,7 @@ private static 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()); @@ -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()); @@ -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()); diff --git a/core/src/main/java/org/infinispan/protostream/impl/JsonUtils.java b/core/src/main/java/org/infinispan/protostream/impl/JsonUtils.java index e20149be3..fc3538d8c 100644 --- a/core/src/main/java/org/infinispan/protostream/impl/JsonUtils.java +++ b/core/src/main/java/org/infinispan/protostream/impl/JsonUtils.java @@ -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(); @@ -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; @@ -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); diff --git a/core/src/main/java/org/infinispan/protostream/impl/TagWriterImpl.java b/core/src/main/java/org/infinispan/protostream/impl/TagWriterImpl.java index 0b4319c6d..ba4baf4b9 100644 --- a/core/src/main/java/org/infinispan/protostream/impl/TagWriterImpl.java +++ b/core/src/main/java/org/infinispan/protostream/impl/TagWriterImpl.java @@ -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; @@ -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)); } @@ -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)); } diff --git a/core/src/main/java/org/infinispan/protostream/impl/UnknownFieldSetImpl.java b/core/src/main/java/org/infinispan/protostream/impl/UnknownFieldSetImpl.java index 5add47264..6b2d13774 100644 --- a/core/src/main/java/org/infinispan/protostream/impl/UnknownFieldSetImpl.java +++ b/core/src/main/java/org/infinispan/protostream/impl/UnknownFieldSetImpl.java @@ -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();