Skip to content

Commit

Permalink
Avoid allocating TreeMap in UnknownFieldSet.Builder.asMap
Browse files Browse the repository at this point in the history
If UnknownFieldSet is empty

PiperOrigin-RevId: 632504647
  • Loading branch information
mhansen authored and copybara-github committed May 10, 2024
1 parent 8c6e5b7 commit 4b7c2d4
Showing 1 changed file with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ public Builder addField(int number, Field field) {
* changes may or may not be reflected in this map.
*/
public Map<Integer, Field> asMap() {
// Avoid an allocation for the common case of an empty map.
if (fieldBuilders.isEmpty()) {
return Collections.emptyMap();
}

TreeMap<Integer, Field> fields = new TreeMap<>();
for (Map.Entry<Integer, Field.Builder> entry : fieldBuilders.entrySet()) {
fields.put(entry.getKey(), entry.getValue().build());
Expand Down

0 comments on commit 4b7c2d4

Please sign in to comment.