Avoid allocating FieldSet iterator if FieldSet is empty

This saves some allocations during writing out messages with extensions declared in the common case of empty FieldSet.

PiperOrigin-RevId: 636031501
pull/16905/head
Mark Hansen 6 months ago committed by Copybara-Service
parent 590e5aafee
commit 3ba72807a6
  1. 8
      java/core/src/main/java/com/google/protobuf/FieldSet.java

@ -213,6 +213,10 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
* library as it is not protected from mutation when fields is not immutable.
*/
public Iterator<Map.Entry<T, Object>> iterator() {
// Avoid allocation in the common case of empty FieldSet.
if (isEmpty()) {
return Collections.emptyIterator();
}
if (hasLazyField) {
return new LazyIterator<T>(fields.entrySet().iterator());
}
@ -225,6 +229,10 @@ final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> {
* fields is not immutable.
*/
Iterator<Map.Entry<T, Object>> descendingIterator() {
// Avoid an allocation in the common case of empty FieldSet.
if (isEmpty()) {
return Collections.emptyIterator();
}
if (hasLazyField) {
return new LazyIterator<T>(fields.descendingEntrySet().iterator());
}

Loading…
Cancel
Save