Skip to content

Commit a70f009

Browse files
committed
Skip unnecessary presizing.
------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=81367572
1 parent d585b1b commit a70f009

File tree

11 files changed

+31
-30
lines changed

11 files changed

+31
-30
lines changed

guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSetMultimap.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,13 @@ private static <V> ImmutableSet<V> emptySet(
490490
: ImmutableSortedSet.<V>emptySet(valueComparator);
491491
}
492492

493+
private static <V> ImmutableSet.Builder<V> valuesBuilder(
494+
@Nullable Comparator<? super V> valueComparator) {
495+
return (valueComparator == null)
496+
? new ImmutableSet.Builder<V>()
497+
: new ImmutableSortedSet.Builder<V>(valueComparator);
498+
}
499+
493500
@Nullable Comparator<? super V> valueComparator() {
494501
return emptySet instanceof ImmutableSortedSet
495502
? ((ImmutableSortedSet<V>) emptySet).comparator()

guava-gwt/src/com/google/common/collect/LinkedHashMultimap_CustomFieldSerializer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ public static LinkedHashMultimap<Object, Object> instantiate(
3939
SerializationStreamReader stream) throws SerializationException {
4040
LinkedHashMultimap<Object, Object> multimap = LinkedHashMultimap.create();
4141

42-
multimap.valueSetCapacity = stream.readInt();
4342
int distinctKeys = stream.readInt();
44-
Map<Object, Collection<Object>> map =
45-
new LinkedHashMap<Object, Collection<Object>>(Maps.capacity(distinctKeys));
43+
Map<Object, Collection<Object>> map = new LinkedHashMap<Object, Collection<Object>>();
4644
for (int i = 0; i < distinctKeys; i++) {
4745
Object key = stream.readObject();
4846
map.put(key, multimap.createCollection(key));
@@ -60,7 +58,6 @@ public static LinkedHashMultimap<Object, Object> instantiate(
6058

6159
public static void serialize(SerializationStreamWriter stream,
6260
LinkedHashMultimap<?, ?> multimap) throws SerializationException {
63-
stream.writeInt(multimap.valueSetCapacity);
6461
stream.writeInt(multimap.keySet().size());
6562
for (Object key : multimap.keySet()) {
6663
stream.writeObject(key);

guava/src/com/google/common/collect/ArrayListMultimap.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,16 @@ public void trimToSize() {
148148
@GwtIncompatible("java.io.ObjectOutputStream")
149149
private void writeObject(ObjectOutputStream stream) throws IOException {
150150
stream.defaultWriteObject();
151-
stream.writeInt(expectedValuesPerKey);
152151
Serialization.writeMultimap(this, stream);
153152
}
154153

155154
@GwtIncompatible("java.io.ObjectOutputStream")
156155
private void readObject(ObjectInputStream stream)
157156
throws IOException, ClassNotFoundException {
158157
stream.defaultReadObject();
159-
expectedValuesPerKey = stream.readInt();
158+
expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
160159
int distinctKeys = Serialization.readCount(stream);
161-
Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
160+
Map<K, Collection<V>> map = Maps.newHashMap();
162161
setMap(map);
163162
Serialization.populateMultimap(this, stream, distinctKeys);
164163
}

guava/src/com/google/common/collect/HashBiMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
647647
@GwtIncompatible("java.io.ObjectInputStream")
648648
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
649649
stream.defaultReadObject();
650+
init(16);
650651
int size = Serialization.readCount(stream);
651-
init(size);
652652
Serialization.populateMap(this, stream, size);
653653
}
654654

guava/src/com/google/common/collect/HashMultimap.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,16 @@ private HashMultimap(Multimap<? extends K, ? extends V> multimap) {
122122
@GwtIncompatible("java.io.ObjectOutputStream")
123123
private void writeObject(ObjectOutputStream stream) throws IOException {
124124
stream.defaultWriteObject();
125-
stream.writeInt(expectedValuesPerKey);
126125
Serialization.writeMultimap(this, stream);
127126
}
128127

129128
@GwtIncompatible("java.io.ObjectInputStream")
130129
private void readObject(ObjectInputStream stream)
131130
throws IOException, ClassNotFoundException {
132131
stream.defaultReadObject();
133-
expectedValuesPerKey = stream.readInt();
132+
expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
134133
int distinctKeys = Serialization.readCount(stream);
135-
Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
134+
Map<K, Collection<V>> map = Maps.newHashMap();
136135
setMap(map);
137136
Serialization.populateMultimap(this, stream, distinctKeys);
138137
}

guava/src/com/google/common/collect/HashMultiset.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ private void readObject(ObjectInputStream stream)
9191
throws IOException, ClassNotFoundException {
9292
stream.defaultReadObject();
9393
int distinctElements = Serialization.readCount(stream);
94-
setBackingMap(
95-
Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
94+
setBackingMap(Maps.<E, Count>newHashMap());
9695
Serialization.populateMultiset(this, stream, distinctElements);
9796
}
9897

guava/src/com/google/common/collect/ImmutableListMultimap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ private void readObject(ObjectInputStream stream)
393393
throw new InvalidObjectException("Invalid value count " + valueCount);
394394
}
395395

396-
Object[] array = new Object[valueCount];
396+
ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder();
397397
for (int j = 0; j < valueCount; j++) {
398-
array[j] = stream.readObject();
398+
valuesBuilder.add(stream.readObject());
399399
}
400-
builder.put(key, ImmutableList.copyOf(array));
400+
builder.put(key, valuesBuilder.build());
401401
tmpSize += valueCount;
402402
}
403403

guava/src/com/google/common/collect/ImmutableSetMultimap.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.common.collect;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20-
import static java.util.Arrays.asList;
2120

2221
import com.google.common.annotations.Beta;
2322
import com.google.common.annotations.GwtCompatible;
@@ -496,6 +495,13 @@ private static <V> ImmutableSet<V> emptySet(
496495
: ImmutableSortedSet.<V>emptySet(valueComparator);
497496
}
498497

498+
private static <V> ImmutableSet.Builder<V> valuesBuilder(
499+
@Nullable Comparator<? super V> valueComparator) {
500+
return (valueComparator == null)
501+
? new ImmutableSet.Builder<V>()
502+
: new ImmutableSortedSet.Builder<V>(valueComparator);
503+
}
504+
499505
/**
500506
* @serialData number of distinct keys, and then for each distinct key: the
501507
* key, the number of values for that key, and the key's values
@@ -536,12 +542,12 @@ private void readObject(ObjectInputStream stream)
536542
throw new InvalidObjectException("Invalid value count " + valueCount);
537543
}
538544

539-
Object[] array = new Object[valueCount];
545+
ImmutableSet.Builder<Object> valuesBuilder = valuesBuilder(valueComparator);
540546
for (int j = 0; j < valueCount; j++) {
541-
array[j] = stream.readObject();
547+
valuesBuilder.add(stream.readObject());
542548
}
543-
ImmutableSet<Object> valueSet = valueSet(valueComparator, asList(array));
544-
if (valueSet.size() != array.length) {
549+
ImmutableSet<Object> valueSet = valuesBuilder.build();
550+
if (valueSet.size() != valueCount) {
545551
throw new InvalidObjectException(
546552
"Duplicate key-value pairs exist for key " + key);
547553
}

guava/src/com/google/common/collect/LinkedHashMultimap.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ public void clear() {
538538
@GwtIncompatible("java.io.ObjectOutputStream")
539539
private void writeObject(ObjectOutputStream stream) throws IOException {
540540
stream.defaultWriteObject();
541-
stream.writeInt(valueSetCapacity);
542541
stream.writeInt(keySet().size());
543542
for (K key : keySet()) {
544543
stream.writeObject(key);
@@ -556,10 +555,9 @@ private void readObject(ObjectInputStream stream)
556555
stream.defaultReadObject();
557556
multimapHeaderEntry = new ValueEntry<K, V>(null, null, 0, null);
558557
succeedsInMultimap(multimapHeaderEntry, multimapHeaderEntry);
559-
valueSetCapacity = stream.readInt();
558+
valueSetCapacity = DEFAULT_VALUE_SET_CAPACITY;
560559
int distinctKeys = stream.readInt();
561-
Map<K, Collection<V>> map =
562-
new LinkedHashMap<K, Collection<V>>(Maps.capacity(distinctKeys));
560+
Map<K, Collection<V>> map = new LinkedHashMap<K, Collection<V>>();
563561
for (int i = 0; i < distinctKeys; i++) {
564562
@SuppressWarnings("unchecked")
565563
K key = (K) stream.readObject();

guava/src/com/google/common/collect/LinkedHashMultiset.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ private void readObject(ObjectInputStream stream)
103103
throws IOException, ClassNotFoundException {
104104
stream.defaultReadObject();
105105
int distinctElements = Serialization.readCount(stream);
106-
setBackingMap(new LinkedHashMap<E, Count>(
107-
Maps.capacity(distinctElements)));
106+
setBackingMap(new LinkedHashMap<E, Count>());
108107
Serialization.populateMultiset(this, stream, distinctElements);
109108
}
110109

0 commit comments

Comments
 (0)