Skip to content

Commit

Permalink
Automated rollback of commit 05e5107.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 638409590
  • Loading branch information
protobuf-github-bot authored and deannagarcia committed Jun 20, 2024
1 parent 21ff287 commit 6fa6350
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
33 changes: 4 additions & 29 deletions java/core/src/main/java/com/google/protobuf/GeneratedMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1028,38 +1028,21 @@ public boolean isInitialized() {
* numbers, but we must write them in canonical (sorted by field number) order. ExtensionWriter
* helps us write individual ranges of extensions at once.
*/
protected interface ExtensionWriter {
public void writeUntil(final int end, final CodedOutputStream output) throws IOException;
}

// Singleton instance so we can avoid allocating a new one for each message serialization.
private static final NoOpExtensionWriter NO_OP_EXTENSION_WRITER = new NoOpExtensionWriter();

/** No-op implementation that writes nothing, for messages with no extensions. */
private static final class NoOpExtensionWriter implements ExtensionWriter {
@Override
public void writeUntil(final int end, final CodedOutputStream output) {
// no-op
}
}

/** Implementation that writes extensions from the FieldSet, for messages with extensions. */
private final class FieldSetExtensionWriter implements ExtensionWriter {
protected class ExtensionWriter {
// Imagine how much simpler this code would be if Java iterators had
// a way to get the next element without advancing the iterator.

private final Iterator<Map.Entry<FieldDescriptor, Object>> iter = extensions.iterator();
private Map.Entry<FieldDescriptor, Object> next;
private final boolean messageSetWireFormat;

private FieldSetExtensionWriter(final boolean messageSetWireFormat) {
private ExtensionWriter(final boolean messageSetWireFormat) {
if (iter.hasNext()) {
next = iter.next();
}
this.messageSetWireFormat = messageSetWireFormat;
}

@Override
public void writeUntil(final int end, final CodedOutputStream output) throws IOException {
while (next != null && next.getKey().getNumber() < end) {
FieldDescriptor descriptor = next.getKey();
Expand Down Expand Up @@ -1093,19 +1076,11 @@ public void writeUntil(final int end, final CodedOutputStream output) throws IOE
}

protected ExtensionWriter newExtensionWriter() {
// Avoid allocation in the common case of no extensions.
if (extensions.isEmpty()) {
return NO_OP_EXTENSION_WRITER;
}
return new FieldSetExtensionWriter(false);
return new ExtensionWriter(false);
}

protected ExtensionWriter newMessageSetExtensionWriter() {
// Avoid allocation in the common case of no extensions.
if (extensions.isEmpty()) {
return NO_OP_EXTENSION_WRITER;
}
return new FieldSetExtensionWriter(true);
return new ExtensionWriter(true);
}

/** Called by subclasses to compute the size of extensions. */
Expand Down
10 changes: 6 additions & 4 deletions src/google/protobuf/compiler/java/full/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,15 @@ void ImmutableMessageGenerator::GenerateMessageSerializationMethods(
if (descriptor_->options().message_set_wire_format()) {
printer->Print(
"com.google.protobuf.GeneratedMessage\n"
" .ExtendableMessage.ExtensionWriter\n"
" extensionWriter = newMessageSetExtensionWriter();\n");
" .ExtendableMessage<$classname$>.ExtensionWriter\n"
" extensionWriter = newMessageSetExtensionWriter();\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
} else {
printer->Print(
"com.google.protobuf.GeneratedMessage\n"
" .ExtendableMessage.ExtensionWriter\n"
" extensionWriter = newExtensionWriter();\n");
" .ExtendableMessage<$classname$>.ExtensionWriter\n"
" extensionWriter = newExtensionWriter();\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
}
}

Expand Down

0 comments on commit 6fa6350

Please sign in to comment.