Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
56cba8ee
Commit
56cba8ee
authored
Apr 14, 2014
by
Max Cai
Committed by
Gerrit Code Review
Apr 14, 2014
Browse files
Options
Browse Files
Download
Plain Diff
Merge "Don't reset cachedSize to 0 in getSerializedSize"
parents
dc2ab875
598087ef
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
37 additions
and
9 deletions
+37
-9
README.txt
java/README.txt
+9
-0
ExtendableMessageNano.java
.../java/com/google/protobuf/nano/ExtendableMessageNano.java
+1
-2
MessageNano.java
java/src/main/java/com/google/protobuf/nano/MessageNano.java
+13
-4
NanoTest.java
java/src/test/java/com/google/protobuf/NanoTest.java
+12
-0
javanano_message.cc
src/google/protobuf/compiler/javanano/javanano_message.cc
+2
-3
No files found.
java/README.txt
View file @
56cba8ee
...
...
@@ -437,6 +437,15 @@ 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
...
...
java/src/main/java/com/google/protobuf/nano/ExtendableMessageNano.java
View file @
56cba8ee
...
...
@@ -47,7 +47,7 @@ public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>>
protected
List
<
UnknownFieldData
>
unknownFieldData
;
@Override
p
ublic
int
get
SerializedSize
()
{
p
rotected
int
compute
SerializedSize
()
{
int
size
=
0
;
int
unknownFieldCount
=
unknownFieldData
==
null
?
0
:
unknownFieldData
.
size
();
for
(
int
i
=
0
;
i
<
unknownFieldCount
;
i
++)
{
...
...
@@ -55,7 +55,6 @@ public abstract class ExtendableMessageNano<M extends ExtendableMessageNano<M>>
size
+=
CodedOutputByteBufferNano
.
computeRawVarint32Size
(
unknownField
.
tag
);
size
+=
unknownField
.
bytes
.
length
;
}
cachedSize
=
size
;
return
size
;
}
...
...
java/src/main/java/com/google/protobuf/nano/MessageNano.java
View file @
56cba8ee
...
...
@@ -38,7 +38,7 @@ import java.io.IOException;
* @author wink@google.com Wink Saville
*/
public
abstract
class
MessageNano
{
protected
int
cachedSize
=
-
1
;
protected
volatile
int
cachedSize
=
-
1
;
/**
* Get the number of bytes required to encode this message.
...
...
@@ -61,9 +61,18 @@ public abstract class MessageNano {
* using getCachedSize().
*/
public
int
getSerializedSize
()
{
// This is overridden if the generated message has serialized fields.
cachedSize
=
0
;
return
0
;
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
;
}
/**
...
...
java/src/test/java/com/google/protobuf/NanoTest.java
View file @
56cba8ee
...
...
@@ -105,6 +105,12 @@ public class NanoTest extends TestCase {
assertEquals
(
456
,
newMsg
.
d
);
assertEquals
(
2
,
msg
.
nestedMsg
.
bb
);
assertEquals
(
SimpleMessageNano
.
BAR
,
msg
.
defaultNestedEnum
);
msg
.
nestedMsg
=
null
;
assertTrue
(
msgSerializedSize
!=
msg
.
getSerializedSize
());
msg
.
clear
();
assertEquals
(
0
,
msg
.
getSerializedSize
());
}
public
void
testRecursiveMessageNano
()
throws
Exception
{
...
...
@@ -143,6 +149,12 @@ public class NanoTest extends TestCase {
assertEquals
(
3
,
newMsg
.
repeatedRecursiveMessageNano
[
0
].
id
);
}
public
void
testMessageNoFields
()
{
SingleMessageNano
msg
=
new
SingleMessageNano
();
assertEquals
(
0
,
msg
.
getSerializedSize
());
assertEquals
(
0
,
MessageNano
.
toByteArray
(
msg
).
length
);
}
public
void
testNanoRequiredInt32
()
throws
Exception
{
TestAllTypesNano
msg
=
new
TestAllTypesNano
();
msg
.
id
=
123
;
...
...
src/google/protobuf/compiler/javanano/javanano_message.cc
View file @
56cba8ee
...
...
@@ -304,8 +304,8 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
printer
->
Print
(
"
\n
"
"@Override
\n
"
"p
ublic int get
SerializedSize() {
\n
"
" int size = super.
get
SerializedSize();
\n
"
);
"p
rotected int compute
SerializedSize() {
\n
"
" int size = super.
compute
SerializedSize();
\n
"
);
printer
->
Indent
();
for
(
int
i
=
0
;
i
<
descriptor_
->
field_count
();
i
++
)
{
...
...
@@ -314,7 +314,6 @@ GenerateMessageSerializationMethods(io::Printer* printer) {
printer
->
Outdent
();
printer
->
Print
(
" cachedSize = size;
\n
"
" return size;
\n
"
"}
\n
"
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment