diff --git a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java index 38aecac42f..b93e220333 100644 --- a/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java +++ b/java/core/src/main/java/com/google/protobuf/UnknownFieldSet.java @@ -85,6 +85,10 @@ public final class UnknownFieldSet implements MessageLite { /** Get a map of fields in the set by number. */ public Map asMap() { + // Avoid an allocation for the common case of an empty map. + if (fields.isEmpty()) { + return Collections.emptyMap(); + } return (Map) fields.clone(); }