Skip to content

Commit

Permalink
Skip unnecessary presizing.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=81367572
  • Loading branch information
cpovirk committed Dec 5, 2014
1 parent d585b1b commit a70f009
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,13 @@ private static <V> ImmutableSet<V> emptySet(
: ImmutableSortedSet.<V>emptySet(valueComparator);
}

private static <V> ImmutableSet.Builder<V> valuesBuilder(
@Nullable Comparator<? super V> valueComparator) {
return (valueComparator == null)
? new ImmutableSet.Builder<V>()
: new ImmutableSortedSet.Builder<V>(valueComparator);
}

@Nullable Comparator<? super V> valueComparator() {
return emptySet instanceof ImmutableSortedSet
? ((ImmutableSortedSet<V>) emptySet).comparator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ public static LinkedHashMultimap<Object, Object> instantiate(
SerializationStreamReader stream) throws SerializationException {
LinkedHashMultimap<Object, Object> multimap = LinkedHashMultimap.create();

multimap.valueSetCapacity = stream.readInt();
int distinctKeys = stream.readInt();
Map<Object, Collection<Object>> map =
new LinkedHashMap<Object, Collection<Object>>(Maps.capacity(distinctKeys));
Map<Object, Collection<Object>> map = new LinkedHashMap<Object, Collection<Object>>();
for (int i = 0; i < distinctKeys; i++) {
Object key = stream.readObject();
map.put(key, multimap.createCollection(key));
Expand All @@ -60,7 +58,6 @@ public static LinkedHashMultimap<Object, Object> instantiate(

public static void serialize(SerializationStreamWriter stream,
LinkedHashMultimap<?, ?> multimap) throws SerializationException {
stream.writeInt(multimap.valueSetCapacity);
stream.writeInt(multimap.keySet().size());
for (Object key : multimap.keySet()) {
stream.writeObject(key);
Expand Down
5 changes: 2 additions & 3 deletions guava/src/com/google/common/collect/ArrayListMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,16 @@ public void trimToSize() {
@GwtIncompatible("java.io.ObjectOutputStream")
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeInt(expectedValuesPerKey);
Serialization.writeMultimap(this, stream);
}

@GwtIncompatible("java.io.ObjectOutputStream")
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
expectedValuesPerKey = stream.readInt();

This comment has been minimized.

Copy link
@step76

step76 May 8, 2017

This change and the other changes like that in this commit have completely broken the serialization.
This behaviour is not documented. Can we rely on serialize this kind of objects between guava versions? I think no, watching this commit.

This comment has been minimized.

Copy link
@jbduncan

jbduncan May 8, 2017

Contributor

@step76 According to the README.md, serializability of things between Guava versions is not guaranteed in the slightest.

IMPORTANT WARNINGS:
...

  1. Serialized forms of ALL objects are subject to change unless noted otherwise. Do not persist these and assume they can be read by a future version of the library.

I hope this helps.

This comment has been minimized.

Copy link
@jbduncan

jbduncan May 8, 2017

Contributor

If long-term serializability something you need, you might want to consider things like protobuf, Apache Thrift and GRPC. 😃

This comment has been minimized.

Copy link
@step76

step76 May 9, 2017

Thanks for the reply, I completely ignored that part in the readme file.

expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
int distinctKeys = Serialization.readCount(stream);
Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
Map<K, Collection<V>> map = Maps.newHashMap();
setMap(map);
Serialization.populateMultimap(this, stream, distinctKeys);
}
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/collect/HashBiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ private void writeObject(ObjectOutputStream stream) throws IOException {
@GwtIncompatible("java.io.ObjectInputStream")
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
init(16);
int size = Serialization.readCount(stream);
init(size);
Serialization.populateMap(this, stream, size);
}

Expand Down
5 changes: 2 additions & 3 deletions guava/src/com/google/common/collect/HashMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,16 @@ private HashMultimap(Multimap<? extends K, ? extends V> multimap) {
@GwtIncompatible("java.io.ObjectOutputStream")
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeInt(expectedValuesPerKey);
Serialization.writeMultimap(this, stream);
}

@GwtIncompatible("java.io.ObjectInputStream")
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
expectedValuesPerKey = stream.readInt();
expectedValuesPerKey = DEFAULT_VALUES_PER_KEY;
int distinctKeys = Serialization.readCount(stream);
Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
Map<K, Collection<V>> map = Maps.newHashMap();
setMap(map);
Serialization.populateMultimap(this, stream, distinctKeys);
}
Expand Down
3 changes: 1 addition & 2 deletions guava/src/com/google/common/collect/HashMultiset.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
int distinctElements = Serialization.readCount(stream);
setBackingMap(
Maps.<E, Count>newHashMapWithExpectedSize(distinctElements));
setBackingMap(Maps.<E, Count>newHashMap());
Serialization.populateMultiset(this, stream, distinctElements);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,11 @@ private void readObject(ObjectInputStream stream)
throw new InvalidObjectException("Invalid value count " + valueCount);
}

Object[] array = new Object[valueCount];
ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder();
for (int j = 0; j < valueCount; j++) {
array[j] = stream.readObject();
valuesBuilder.add(stream.readObject());
}
builder.put(key, ImmutableList.copyOf(array));
builder.put(key, valuesBuilder.build());
tmpSize += valueCount;
}

Expand Down
16 changes: 11 additions & 5 deletions guava/src/com/google/common/collect/ImmutableSetMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.common.collect;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Arrays.asList;

import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -496,6 +495,13 @@ private static <V> ImmutableSet<V> emptySet(
: ImmutableSortedSet.<V>emptySet(valueComparator);
}

private static <V> ImmutableSet.Builder<V> valuesBuilder(
@Nullable Comparator<? super V> valueComparator) {
return (valueComparator == null)
? new ImmutableSet.Builder<V>()
: new ImmutableSortedSet.Builder<V>(valueComparator);
}

/**
* @serialData number of distinct keys, and then for each distinct key: the
* key, the number of values for that key, and the key's values
Expand Down Expand Up @@ -536,12 +542,12 @@ private void readObject(ObjectInputStream stream)
throw new InvalidObjectException("Invalid value count " + valueCount);
}

Object[] array = new Object[valueCount];
ImmutableSet.Builder<Object> valuesBuilder = valuesBuilder(valueComparator);
for (int j = 0; j < valueCount; j++) {
array[j] = stream.readObject();
valuesBuilder.add(stream.readObject());
}
ImmutableSet<Object> valueSet = valueSet(valueComparator, asList(array));
if (valueSet.size() != array.length) {
ImmutableSet<Object> valueSet = valuesBuilder.build();
if (valueSet.size() != valueCount) {
throw new InvalidObjectException(
"Duplicate key-value pairs exist for key " + key);
}
Expand Down
6 changes: 2 additions & 4 deletions guava/src/com/google/common/collect/LinkedHashMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ public void clear() {
@GwtIncompatible("java.io.ObjectOutputStream")
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
stream.writeInt(valueSetCapacity);
stream.writeInt(keySet().size());
for (K key : keySet()) {
stream.writeObject(key);
Expand All @@ -556,10 +555,9 @@ private void readObject(ObjectInputStream stream)
stream.defaultReadObject();
multimapHeaderEntry = new ValueEntry<K, V>(null, null, 0, null);
succeedsInMultimap(multimapHeaderEntry, multimapHeaderEntry);
valueSetCapacity = stream.readInt();
valueSetCapacity = DEFAULT_VALUE_SET_CAPACITY;
int distinctKeys = stream.readInt();
Map<K, Collection<V>> map =
new LinkedHashMap<K, Collection<V>>(Maps.capacity(distinctKeys));
Map<K, Collection<V>> map = new LinkedHashMap<K, Collection<V>>();
for (int i = 0; i < distinctKeys; i++) {
@SuppressWarnings("unchecked")
K key = (K) stream.readObject();
Expand Down
3 changes: 1 addition & 2 deletions guava/src/com/google/common/collect/LinkedHashMultiset.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
int distinctElements = Serialization.readCount(stream);
setBackingMap(new LinkedHashMap<E, Count>(
Maps.capacity(distinctElements)));
setBackingMap(new LinkedHashMap<E, Count>());
Serialization.populateMultiset(this, stream, distinctElements);
}

Expand Down
3 changes: 0 additions & 3 deletions guava/src/com/google/common/collect/Serialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ private Serialization() {}
* #writeMultiset(Multiset, ObjectOutputStream)}, or the number of distinct
* keys in a multimap serialized by {@link
* #writeMultimap(Multimap, ObjectOutputStream)}.
*
* <p>The returned count may be used to construct an empty collection of the
* appropriate capacity before calling any of the {@code populate} methods.
*/
static int readCount(ObjectInputStream stream) throws IOException {
return stream.readInt();
Expand Down

1 comment on commit a70f009

@martinm1000
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beware: officially guava doesn't care about serialization compatibility, but that change broke it.

Please sign in to comment.