File tree 1 file changed +8
-0
lines changed
java/core/src/main/java/com/google/protobuf 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -213,6 +213,10 @@ private static <T extends FieldDescriptorLite<T>> void cloneFieldEntry(
213
213
* library as it is not protected from mutation when fields is not immutable.
214
214
*/
215
215
public Iterator <Map .Entry <T , Object >> iterator () {
216
+ // Avoid allocation in the common case of empty FieldSet.
217
+ if (isEmpty ()) {
218
+ return Collections .emptyIterator ();
219
+ }
216
220
if (hasLazyField ) {
217
221
return new LazyIterator <T >(fields .entrySet ().iterator ());
218
222
}
@@ -225,6 +229,10 @@ public Iterator<Map.Entry<T, Object>> iterator() {
225
229
* fields is not immutable.
226
230
*/
227
231
Iterator <Map .Entry <T , Object >> descendingIterator () {
232
+ // Avoid an allocation in the common case of empty FieldSet.
233
+ if (isEmpty ()) {
234
+ return Collections .emptyIterator ();
235
+ }
228
236
if (hasLazyField ) {
229
237
return new LazyIterator <T >(fields .descendingEntrySet ().iterator ());
230
238
}
You can’t perform that action at this time.
0 commit comments