ChangeField: Use ImmutableTable.Builder to build tables

Creating a whole empty hash-based Table is a lot of garbage memory
when we just copy it into an ImmutableTable, particularly since it
special-cases zero- and one-element tables to save memory.

Instead, use ImmutableTable.Builder, which collects its entries as a
list rather than a Table. The only semantic change is this will reject
duplicate cells instead of silently ignoring them. However considering
that the index field was populated from a Table, there shouldn't be
duplicates anyway.

Change-Id: I76b5435d6bfc484cd61fb750e0802223af8756e2
This commit is contained in:
Dave Borowitz
2016-06-20 14:40:33 -04:00
parent 5279e6f08b
commit c59d65d502

View File

@@ -20,9 +20,9 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
@@ -351,8 +351,8 @@ public class ChangeField {
}
public static ReviewerSet parseReviewerFieldValues(Iterable<String> values) {
Table<ReviewerStateInternal, Account.Id, Timestamp> table =
HashBasedTable.create();
ImmutableTable.Builder<ReviewerStateInternal, Account.Id, Timestamp> b =
ImmutableTable.builder();
for (String v : values) {
int f = v.indexOf(',');
if (f < 0) {
@@ -362,12 +362,12 @@ public class ChangeField {
if (l == f) {
continue;
}
table.put(
b.put(
ReviewerStateInternal.valueOf(v.substring(0, f)),
Account.Id.parse(v.substring(f + 1, l)),
new Timestamp(Long.valueOf(v.substring(l + 1, v.length()))));
}
return ReviewerSet.fromTable(table);
return ReviewerSet.fromTable(b.build());
}
/** Commit ID of any patch set on the change, using prefix match. */