From 761d49a27067f74c56818e979b979bd3942a27cc Mon Sep 17 00:00:00 2001 From: Mark Hansen Date: Sun, 7 Jul 2024 18:46:10 -0700 Subject: [PATCH] Add overload for LazyStringArrayList.add(String): boolean This is a performance optimisation that avoids us going through from AbstractList.add(E) to LazyStringArrayList.add(int index, String) and then having to call the index-based add in ArrayList, which has more bookkeeping around moving elements across if necessary. We can avoid that bookkeeping by adding this overload. Also, group together overloads to squash a style warning. PiperOrigin-RevId: 650089286 --- .../google/protobuf/LazyStringArrayList.java | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java b/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java index cb9b62accebf3..e01cd6bc56e3f 100644 --- a/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/LazyStringArrayList.java @@ -145,6 +145,29 @@ private void add(int index, byte[] element) { modCount++; } + @Override + @CanIgnoreReturnValue + public boolean add(String element) { + ensureIsMutable(); + list.add(element); + modCount++; + return true; + } + + @Override + public void add(ByteString element) { + ensureIsMutable(); + list.add(element); + modCount++; + } + + @Override + public void add(byte[] element) { + ensureIsMutable(); + list.add(element); + modCount++; + } + @Override public boolean addAll(Collection c) { // The default implementation of AbstractCollection.addAll(Collection) @@ -197,20 +220,6 @@ public void clear() { modCount++; } - @Override - public void add(ByteString element) { - ensureIsMutable(); - list.add(element); - modCount++; - } - - @Override - public void add(byte[] element) { - ensureIsMutable(); - list.add(element); - modCount++; - } - @Override public Object getRaw(int index) { return list.get(index);