Skip to content

Commit 16e0a63

Browse files
mhansencopybara-github
authored andcommitted
Remove field SmallSortedMap.maxArraySize
It's always set to 16 in production code. Except for when it's set to 0 for the immutable empty SmallSortedMap, but the number doesn't change anything for that immutable empty map (it's always empty, it will never hit the max). This should save 4 bytes of memory per SmallSortedMap. PiperOrigin-RevId: 636035193
1 parent c05be32 commit 16e0a63

File tree

3 files changed

+88
-99
lines changed

3 files changed

+88
-99
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,19 @@ public interface FieldDescriptorLite<T extends FieldDescriptorLite<T>> extends C
4949
MessageLite.Builder internalMergeFrom(MessageLite.Builder to, MessageLite from);
5050
}
5151

52-
private static final int DEFAULT_FIELD_MAP_ARRAY_SIZE = 16;
53-
5452
private final SmallSortedMap<T, Object> fields;
5553
private boolean isImmutable;
5654
private boolean hasLazyField;
5755

5856
/** Construct a new FieldSet. */
5957
private FieldSet() {
60-
this.fields = SmallSortedMap.newFieldMap(DEFAULT_FIELD_MAP_ARRAY_SIZE);
58+
this.fields = SmallSortedMap.newFieldMap();
6159
}
6260

6361
/** Construct an empty FieldSet. This is only used to initialize DEFAULT_INSTANCE. */
6462
@SuppressWarnings("unused")
6563
private FieldSet(final boolean dummy) {
66-
this(SmallSortedMap.<T>newFieldMap(0));
64+
this(SmallSortedMap.<T>newFieldMap());
6765
makeImmutable();
6866
}
6967

@@ -185,7 +183,7 @@ public Map<T, Object> getAllFields() {
185183

186184
private static <T extends FieldDescriptorLite<T>> SmallSortedMap<T, Object> cloneAllFieldsMap(
187185
SmallSortedMap<T, Object> fields, boolean copyList, boolean resolveLazyFields) {
188-
SmallSortedMap<T, Object> result = SmallSortedMap.newFieldMap(DEFAULT_FIELD_MAP_ARRAY_SIZE);
186+
SmallSortedMap<T, Object> result = SmallSortedMap.newFieldMap();
189187
for (int i = 0; i < fields.getNumArrayEntries(); i++) {
190188
cloneFieldEntry(result, fields.getArrayEntryAt(i), copyList, resolveLazyFields);
191189
}
@@ -931,7 +929,7 @@ static final class Builder<T extends FieldDescriptorLite<T>> {
931929
private boolean hasNestedBuilders;
932930

933931
private Builder() {
934-
this(SmallSortedMap.<T>newFieldMap(DEFAULT_FIELD_MAP_ARRAY_SIZE));
932+
this(SmallSortedMap.<T>newFieldMap());
935933
}
936934

937935
private Builder(SmallSortedMap<T, Object> fields) {

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
// a subclass to aid testability of the core logic.
5959
class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> {
6060

61+
static final int DEFAULT_FIELD_MAP_ARRAY_SIZE = 16;
62+
6163
/**
6264
* Creates a new instance for mapping FieldDescriptors to their values. The {@link
6365
* #makeImmutable()} implementation will convert the List values of any repeated fields to
@@ -67,22 +69,22 @@ class SmallSortedMap<K extends Comparable<K>, V> extends AbstractMap<K, V> {
6769
* mappings.
6870
*/
6971
static <FieldDescriptorType extends FieldSet.FieldDescriptorLite<FieldDescriptorType>>
70-
SmallSortedMap<FieldDescriptorType, Object> newFieldMap(int arraySize) {
71-
return new SmallSortedMap<FieldDescriptorType, Object>(arraySize) {
72+
SmallSortedMap<FieldDescriptorType, Object> newFieldMap() {
73+
return new SmallSortedMap<FieldDescriptorType, Object>() {
7274
@Override
7375
@SuppressWarnings("unchecked")
7476
public void makeImmutable() {
7577
if (!isImmutable()) {
7678
for (int i = 0; i < getNumArrayEntries(); i++) {
7779
final Map.Entry<FieldDescriptorType, Object> entry = getArrayEntryAt(i);
7880
if (entry.getKey().isRepeated()) {
79-
final List value = (List) entry.getValue();
81+
final List<?> value = (List) entry.getValue();
8082
entry.setValue(Collections.unmodifiableList(value));
8183
}
8284
}
8385
for (Map.Entry<FieldDescriptorType, Object> entry : getOverflowEntries()) {
8486
if (entry.getKey().isRepeated()) {
85-
final List value = (List) entry.getValue();
87+
final List<?> value = (List) entry.getValue();
8688
entry.setValue(Collections.unmodifiableList(value));
8789
}
8890
}
@@ -92,17 +94,11 @@ public void makeImmutable() {
9294
};
9395
}
9496

95-
/**
96-
* Creates a new instance for testing.
97-
*
98-
* @param arraySize The size of the entry array containing the lexicographically smallest
99-
* mappings.
100-
*/
101-
static <K extends Comparable<K>, V> SmallSortedMap<K, V> newInstanceForTest(int arraySize) {
102-
return new SmallSortedMap<K, V>(arraySize);
97+
/** Creates a new instance for testing. */
98+
static <K extends Comparable<K>, V> SmallSortedMap<K, V> newInstanceForTest() {
99+
return new SmallSortedMap<>();
103100
}
104101

105-
private final int maxArraySize;
106102
// The "entry array" is actually a List because generic arrays are not
107103
// allowed. ArrayList also nicely handles the entry shifting on inserts and
108104
// removes.
@@ -119,8 +115,7 @@ static <K extends Comparable<K>, V> SmallSortedMap<K, V> newInstanceForTest(int
119115
* @code arraySize Size of the array in which the lexicographically smallest mappings are stored.
120116
* (i.e. the {@code k} referred to in the class documentation).
121117
*/
122-
private SmallSortedMap(int arraySize) {
123-
this.maxArraySize = arraySize;
118+
private SmallSortedMap() {
124119
this.entryList = Collections.emptyList();
125120
this.overflowEntries = Collections.emptyMap();
126121
this.overflowEntriesDescending = Collections.emptyMap();
@@ -215,14 +210,14 @@ public V put(K key, V value) {
215210
}
216211
ensureEntryArrayMutable();
217212
final int insertionPoint = -(index + 1);
218-
if (insertionPoint >= maxArraySize) {
213+
if (insertionPoint >= DEFAULT_FIELD_MAP_ARRAY_SIZE) {
219214
// Put directly in overflow.
220215
return getOverflowEntriesMutable().put(key, value);
221216
}
222217
// Insert new Entry in array.
223-
if (entryList.size() == maxArraySize) {
218+
if (entryList.size() == DEFAULT_FIELD_MAP_ARRAY_SIZE) {
224219
// Shift the last array entry into overflow.
225-
final Entry lastEntryInArray = entryList.remove(maxArraySize - 1);
220+
final Entry lastEntryInArray = entryList.remove(DEFAULT_FIELD_MAP_ARRAY_SIZE - 1);
226221
getOverflowEntriesMutable().put(lastEntryInArray.getKey(), lastEntryInArray.getValue());
227222
}
228223
entryList.add(insertionPoint, new Entry(key, value));
@@ -357,7 +352,7 @@ private SortedMap<K, V> getOverflowEntriesMutable() {
357352
private void ensureEntryArrayMutable() {
358353
checkMutable();
359354
if (entryList.isEmpty() && !(entryList instanceof ArrayList)) {
360-
entryList = new ArrayList<Entry>(maxArraySize);
355+
entryList = new ArrayList<>(DEFAULT_FIELD_MAP_ARRAY_SIZE);
361356
}
362357
}
363358

0 commit comments

Comments
 (0)