diff options
author | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-11-02 18:50:19 +0000 |
---|---|---|
committer | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2009-11-02 18:50:19 +0000 |
commit | f85d70f9e47c3ec5f3a104ec70d062540023267a (patch) | |
tree | cc8e2da3b0e59f3d9da9d4c99ddc70b2756aa30c /java/src | |
parent | 573989f7828809e8fe615a1ee9d9657a379958d9 (diff) | |
download | protobuf-f85d70f9e47c3ec5f3a104ec70d062540023267a.tar.gz protobuf-f85d70f9e47c3ec5f3a104ec70d062540023267a.tar.bz2 protobuf-f85d70f9e47c3ec5f3a104ec70d062540023267a.zip |
Optimize Java serialization of small messages to streams. Patch from Evan Jones.
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/main/java/com/google/protobuf/AbstractMessageLite.java | 11 | ||||
-rw-r--r-- | java/src/main/java/com/google/protobuf/CodedOutputStream.java | 12 |
2 files changed, 21 insertions, 2 deletions
diff --git a/java/src/main/java/com/google/protobuf/AbstractMessageLite.java b/java/src/main/java/com/google/protobuf/AbstractMessageLite.java index 489577df..86bf02d8 100644 --- a/java/src/main/java/com/google/protobuf/AbstractMessageLite.java +++ b/java/src/main/java/com/google/protobuf/AbstractMessageLite.java @@ -72,13 +72,20 @@ public abstract class AbstractMessageLite implements MessageLite { } public void writeTo(final OutputStream output) throws IOException { - final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + final int bufferSize = + CodedOutputStream.computePreferredBufferSize(getSerializedSize()); + final CodedOutputStream codedOutput = + CodedOutputStream.newInstance(output, bufferSize); writeTo(codedOutput); codedOutput.flush(); } public void writeDelimitedTo(final OutputStream output) throws IOException { - final CodedOutputStream codedOutput = CodedOutputStream.newInstance(output); + final int serialized = getSerializedSize(); + final int bufferSize = CodedOutputStream.computePreferredBufferSize( + CodedOutputStream.computeRawVarint32Size(serialized) + serialized); + final CodedOutputStream codedOutput = + CodedOutputStream.newInstance(output, bufferSize); codedOutput.writeRawVarint32(getSerializedSize()); writeTo(codedOutput); codedOutput.flush(); diff --git a/java/src/main/java/com/google/protobuf/CodedOutputStream.java b/java/src/main/java/com/google/protobuf/CodedOutputStream.java index d3907a7c..58dd1506 100644 --- a/java/src/main/java/com/google/protobuf/CodedOutputStream.java +++ b/java/src/main/java/com/google/protobuf/CodedOutputStream.java @@ -60,6 +60,18 @@ public final class CodedOutputStream { */ public static final int DEFAULT_BUFFER_SIZE = 4096; + /** + * Returns the buffer size to efficiently write dataLength bytes to this + * CodedOutputStream. Used by AbstractMessageLite. + * + * @return the buffer size to efficiently write dataLength bytes to this + * CodedOutputStream. + */ + static int computePreferredBufferSize(int dataLength) { + if (dataLength > DEFAULT_BUFFER_SIZE) return DEFAULT_BUFFER_SIZE; + return dataLength; + } + private CodedOutputStream(final byte[] buffer, final int offset, final int length) { output = null; |