Skip to content

Commit d0b016f

Browse files
mhansencopybara-github
authored andcommitted
Java full runtime: Avoid allocating when calling asMap on empty UnknownFieldSet
PiperOrigin-RevId: 631235442
1 parent f597991 commit d0b016f

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public int hashCode() {
8585

8686
/** Get a map of fields in the set by number. */
8787
public Map<Integer, Field> asMap() {
88+
// Avoid an allocation for the common case of an empty map.
89+
if (fields.isEmpty()) {
90+
return Collections.emptyMap();
91+
}
8892
return (Map<Integer, Field>) fields.clone();
8993
}
9094

0 commit comments

Comments
 (0)