From 2d849337400b64ee913ece4631d3b2dbc95f20d8 Mon Sep 17 00:00:00 2001 From: Wink Saville Date: Fri, 21 Mar 2014 18:05:10 -0700 Subject: Revert "Don't reset cachedSize to 0 in getSerializedSize" This reverts commit c6e12c6702ca764486f952654ba1568f00efe813. --- java/README.txt | 9 --------- .../google/protobuf/nano/ExtendableMessageNano.java | 3 ++- .../java/com/google/protobuf/nano/MessageNano.java | 19 +++++-------------- java/src/test/java/com/google/protobuf/NanoTest.java | 15 --------------- 4 files changed, 7 insertions(+), 39 deletions(-) (limited to 'java') diff --git a/java/README.txt b/java/README.txt index f958d14..13865f6 100644 --- a/java/README.txt +++ b/java/README.txt @@ -437,15 +437,6 @@ and the runtime overhead. An overview of Nano features: MessageNano. - The 'bytes' type translates to the Java type byte[]. -The generated messages are not thread-safe for writes, but may be -used simultaneously from multiple threads in a read-only manner. -In other words, an appropriate synchronization mechanism (such as -a ReadWriteLock) must be used to ensure that a message, its -ancestors, and descendants are not accessed by any other threads -while the message is being modified. Field reads, getter methods, -toByteArray(...), writeTo(...), getCachedSize(), and -getSerializedSize() are all considered read-only operations. - IMPORTANT: If you have fields with defaults and opt out of accessors How fields with defaults are serialized has changed. Because we don't diff --git a/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java b/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java index 63c8afc..839f21c 100644 --- a/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java @@ -47,7 +47,7 @@ public abstract class ExtendableMessageNano> protected List unknownFieldData; @Override - protected int computeSerializedSize() { + public int getSerializedSize() { int size = 0; int unknownFieldCount = unknownFieldData == null ? 0 : unknownFieldData.size(); for (int i = 0; i < unknownFieldCount; i++) { @@ -55,6 +55,7 @@ public abstract class ExtendableMessageNano> size += CodedOutputByteBufferNano.computeRawVarint32Size(unknownField.tag); size += unknownField.bytes.length; } + cachedSize = size; return size; } diff --git a/java/src/main/java/com/google/protobuf/nano/MessageNano.java b/java/src/main/java/com/google/protobuf/nano/MessageNano.java index 3119c93..82dc6cc 100644 --- a/java/src/main/java/com/google/protobuf/nano/MessageNano.java +++ b/java/src/main/java/com/google/protobuf/nano/MessageNano.java @@ -38,7 +38,7 @@ import java.io.IOException; * @author wink@google.com Wink Saville */ public abstract class MessageNano { - protected volatile int cachedSize = -1; + protected int cachedSize = -1; /** * Get the number of bytes required to encode this message. @@ -60,19 +60,10 @@ public abstract class MessageNano { * The size is cached and the cached result can be retrieved * using getCachedSize(). */ - public final int getSerializedSize() { - int size = computeSerializedSize(); - cachedSize = size; - return size; - } - - /** - * Computes the number of bytes required to encode this message. This does not update the - * cached size. - */ - protected int computeSerializedSize() { - // This is overridden if the generated message has serialized fields. - return 0; + public int getSerializedSize() { + // This is overridden if the generated message has serialized fields. + cachedSize = 0; + return 0; } /** diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java index 4aa6d89..9987cac 100644 --- a/java/src/test/java/com/google/protobuf/NanoTest.java +++ b/java/src/test/java/com/google/protobuf/NanoTest.java @@ -105,14 +105,6 @@ public class NanoTest extends TestCase { assertEquals(456, newMsg.d); assertEquals(2, msg.nestedMsg.bb); assertEquals(SimpleMessageNano.BAR, msg.defaultNestedEnum); - - msg.nestedMsg = null; - assertEquals(msgSerializedSize, msg.getCachedSize()); - assertTrue(msgSerializedSize != msg.getSerializedSize()); - - msg.clear(); - assertEquals(0, msg.getCachedSize()); - assertEquals(0, msg.getSerializedSize()); } public void testRecursiveMessageNano() throws Exception { @@ -3540,13 +3532,6 @@ public class NanoTest extends TestCase { assertTrue(Arrays.equals(new boolean[] {false, true, false, true}, nonPacked.bools)); } - public void testMessageNoFields() { - SingleMessageNano msg = new SingleMessageNano(); - assertEquals(0, msg.getSerializedSize()); - assertEquals(0, msg.getCachedSize()); - assertEquals(0, MessageNano.toByteArray(msg).length); - } - private void assertRepeatedPackablesEqual( NanoRepeatedPackables.NonPacked nonPacked, NanoRepeatedPackables.Packed packed) { // Not using MessageNano.equals() -- that belongs to a separate test. -- cgit v1.1