Skip to content

Commit

Permalink
Extension patch
Browse files Browse the repository at this point in the history
  • Loading branch information
mkruskal-google committed Sep 27, 2022
1 parent ffb5424 commit ef85826
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Change the Lite runtime to prefer merging from the wireformat into mutable
messages rather than building up a new immutable object before merging. This
way results in fewer allocations and copy operations.
* Make message-type extensions merge from wire-format instead of building up instances and merging afterwards. This has much better performance.

2022-09-13 version 3.20.2 (C++/Java/Python/PHP/Objective-C/C#/Ruby)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,40 @@ <UT, UB> UB parseExtension(
value = reader.readString();
break;
case GROUP:
if (!extension.isRepeated()) {
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue instanceof GeneratedMessageLite) {
Schema extSchema = Protobuf.getInstance().schemaFor(oldValue);
if (!((GeneratedMessageLite<?, ?>) oldValue).isMutable()) {
Object newValue = extSchema.newInstance();
extSchema.mergeFrom(newValue, oldValue);
extensions.setField(extension.descriptor, newValue);
oldValue = newValue;
}
reader.mergeGroupField(oldValue, extSchema, extensionRegistry);
return unknownFields;
}
}
value =
reader.readGroup(
extension.getMessageDefaultInstance().getClass(), extensionRegistry);
break;

case MESSAGE:
if (!extension.isRepeated()) {
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue instanceof GeneratedMessageLite) {
Schema extSchema = Protobuf.getInstance().schemaFor(oldValue);
if (!((GeneratedMessageLite<?, ?>) oldValue).isMutable()) {
Object newValue = extSchema.newInstance();
extSchema.mergeFrom(newValue, oldValue);
extensions.setField(extension.descriptor, newValue);
oldValue = newValue;
}
reader.mergeMessageField(oldValue, extSchema, extensionRegistry);
return unknownFields;
}
}
value =
reader.readMessage(
extension.getMessageDefaultInstance().getClass(), extensionRegistry);
Expand Down
5 changes: 5 additions & 0 deletions java/core/src/main/java/com/google/protobuf/FieldSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public void makeImmutable() {
if (isImmutable) {
return;
}
for (Object value : fields.values()) {
if (value instanceof GeneratedMessageLite) {
((GeneratedMessageLite<?, ?>) value).makeImmutable();
}
}
fields.makeImmutable();
isImmutable = true;
}
Expand Down

0 comments on commit ef85826

Please sign in to comment.