From d0b016fcfcd00a0e48d006f06868b8fb9876974a Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Mon, 6 May 2024 17:26:29 -0700 Subject: [PATCH] Java full runtime: Avoid allocating when calling asMap on empty UnknownFieldSet PiperOrigin-RevId: 631235442 --- .../src/main/java/com/google/protobuf/UnknownFieldSet.java | 4 ++++ 1 file changed, 4 insertions(+) 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 38aecac42f49..b93e22033323 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 int hashCode() { /** 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(); }