Skip to content

Commit

Permalink
Remove extra allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mkruskal-google committed Sep 27, 2022
1 parent 561e997 commit 5306f10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Expand Up @@ -254,6 +254,9 @@ <UT, UB> UB parseExtension(
value = reader.readString();
break;
case GROUP:
// Special case handling for non-repeated sub-messages: merge in-place rather than
// building up new sub-messages and merging those, which is too slow.
// TODO(b/249368670): clean this up
if (!extension.isRepeated()) {
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue instanceof GeneratedMessageLite) {
Expand All @@ -274,6 +277,9 @@ <UT, UB> UB parseExtension(
break;

case MESSAGE:
// Special case handling for non-repeated sub-messages: merge in-place rather than
// building up new sub-messages and merging those, which is too slow.
// TODO(b/249368670): clean this up
if (!extension.isRepeated()) {
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue instanceof GeneratedMessageLite) {
Expand Down Expand Up @@ -303,6 +309,7 @@ <UT, UB> UB parseExtension(
switch (extension.getLiteType()) {
case MESSAGE:
case GROUP:
// TODO(b/249368670): this shouldn't be reachable, clean this up
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue != null) {
value = Internal.mergeMessage(oldValue, value);
Expand Down
8 changes: 5 additions & 3 deletions java/core/src/main/java/com/google/protobuf/FieldSet.java
Expand Up @@ -39,6 +39,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
* A class which represents an arbitrary set of fields of some message type. This is used to
Expand Down Expand Up @@ -123,9 +124,10 @@ public void makeImmutable() {
if (isImmutable) {
return;
}
for (Object value : fields.values()) {
if (value instanceof GeneratedMessageLite) {
((GeneratedMessageLite<?, ?>) value).makeImmutable();
for (int i = 0; i < fields.getNumArrayEntries(); ++i) {
Entry<T, Object> entry = fields.getArrayEntryAt(i);
if (entry.getValue() instanceof GeneratedMessageLite) {
((GeneratedMessageLite<?, ?>) entry.getValue()).makeImmutable();
}
}
fields.makeImmutable();
Expand Down

0 comments on commit 5306f10

Please sign in to comment.