diff options
author | cpeyser <cpeyser@google.com> | 2018-04-25 12:29:40 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-04-25 12:31:17 -0700 |
commit | ea302f5341ac7532f7536f3bd3bc5b6ca48e2141 (patch) | |
tree | 48c82f237986320b613b12c8f0189a99ec9b7758 /src/main/java/com/google/devtools/build/lib/collect | |
parent | 936139d1b8bcf75a772d3e988b342c4ec8fd507d (diff) |
Serialize NestedSet sizes that get special treatement (empty set, singleton)
instead of one boolean each to save a byte in the encoded stream.
PiperOrigin-RevId: 194280646
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/collect')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetCodecWithStore.java | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetCodecWithStore.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetCodecWithStore.java index 018d360990..0923404ee7 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetCodecWithStore.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetCodecWithStore.java @@ -32,6 +32,10 @@ import java.io.IOException; */ public class NestedSetCodecWithStore<T> implements ObjectCodec<NestedSet<T>> { + private enum NestedSetSize { + EMPTY, SINGLETON, GROUP + } + private final NestedSetStore nestedSetStore; /** Creates a NestedSetCodecWithStore that will use the given {@link NestedSetStore}. */ @@ -56,23 +60,20 @@ public class NestedSetCodecWithStore<T> implements ObjectCodec<NestedSet<T>> { } context.serialize(obj.getOrder(), codedOut); - // If the NestedSet is empty, it needs to be assigned to the EMPTY_CHILDREN constant on - // deserialization. - codedOut.writeBoolNoTag(obj.isEmpty()); if (obj.isEmpty()) { - return; - } - - // If the NestedSet is a singleton, we serialize directly as an optimization. - codedOut.writeBoolNoTag(obj.isSingleton()); - if (obj.isSingleton()) { + // If the NestedSet is empty, it needs to be assigned to the EMPTY_CHILDREN constant on + // deserialization. + context.serialize(NestedSetSize.EMPTY, codedOut); + } else if (obj.isSingleton()) { + // If the NestedSet is a singleton, we serialize directly as an optimization. + context.serialize(NestedSetSize.SINGLETON, codedOut); context.serialize(obj.rawChildren(), codedOut); - return; + } else { + context.serialize(NestedSetSize.GROUP, codedOut); + ByteString fingerprint = + nestedSetStore.computeFingerprintAndStore((Object[]) obj.rawChildren(), context); + codedOut.writeByteArrayNoTag(fingerprint.toByteArray()); } - - ByteString fingerprint = - nestedSetStore.computeFingerprintAndStore((Object[]) obj.rawChildren(), context); - codedOut.writeByteArrayNoTag(fingerprint.toByteArray()); } @Override @@ -84,19 +85,18 @@ public class NestedSetCodecWithStore<T> implements ObjectCodec<NestedSet<T>> { } Order order = context.deserialize(codedIn); - boolean isEmpty = codedIn.readBool(); - if (isEmpty) { - return NestedSetBuilder.emptySet(order); + NestedSetSize nestedSetSize = context.deserialize(codedIn); + switch (nestedSetSize) { + case EMPTY: + return NestedSetBuilder.emptySet(order); + case SINGLETON: + T contents = context.deserialize(codedIn); + return new NestedSet<>(order, contents); + case GROUP: + ByteString fingerprint = ByteString.copyFrom(codedIn.readByteArray()); + Object members = nestedSetStore.getContentsAndDeserialize(fingerprint, context); + return new NestedSet<>(order, members); } - - boolean isSingleton = codedIn.readBool(); - if (isSingleton) { - T contents = context.deserialize(codedIn); - return new NestedSet<T>(order, contents); - } - - ByteString fingerprint = ByteString.copyFrom(codedIn.readByteArray()); - Object members = nestedSetStore.getContentsAndDeserialize(fingerprint, context); - return new NestedSet<>(order, members); + throw new IllegalStateException("NestedSet size " + nestedSetSize + " not known"); } } |