File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed
java/core/src/main/java/com/google/protobuf Expand file tree Collapse file tree 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(
213213 * library as it is not protected from mutation when fields is not immutable.
214214 */
215215 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+ }
216220 if (hasLazyField ) {
217221 return new LazyIterator <T >(fields .entrySet ().iterator ());
218222 }
@@ -225,6 +229,10 @@ public Iterator<Map.Entry<T, Object>> iterator() {
225229 * fields is not immutable.
226230 */
227231 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+ }
228236 if (hasLazyField ) {
229237 return new LazyIterator <T >(fields .descendingEntrySet ().iterator ());
230238 }
You can’t perform that action at this time.
0 commit comments