Skip to content

Commit

Permalink
Make DynamicCodec always memoize. Since it replaces Java serializatio…
Browse files Browse the repository at this point in the history
…n, which memoizes, we should too.

PiperOrigin-RevId: 191813677
  • Loading branch information
janakdr authored and Copybara-Service committed Apr 5, 2018
1 parent 538e5c6 commit fdee70e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public MemoizationStrategy getStrategy() {
@Override
public void serialize(SerializationContext context, Object obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
// TODO(janakr,shahan): Remove when memoization is on by default.
context = context.getMemoizingContext();
for (Map.Entry<Field, Long> entry : offsets.entrySet()) {
serializeField(context, codedOut, obj, entry.getKey().getType(), entry.getValue());
}
Expand Down Expand Up @@ -135,6 +137,11 @@ public Object deserialize(DeserializationContext context, CodedInputStream coded
throw new SerializationException("Could not instantiate object of type: " + type, e);
}
context.registerInitialValue(instance);
// We start memoizing if we weren't already doing so. We can't start before registering the
// initial value because the memoizer would get confused, since it doesn't have a tag for this
// object.
// TODO(janakr,shahan): Remove when memoization is on by default.
context = context.getMemoizingContext();
for (Map.Entry<Field, Long> entry : offsets.entrySet()) {
deserializeField(context, codedIn, instance, entry.getKey().getType(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ public void testArray() throws Exception {
new char[] {'a', 'b', 'c', 'x', 'y', 'z'},
new long[] {Long.MAX_VALUE, Long.MIN_VALUE, 27983741982341L, 52893748523495834L}))
.addCodec(new DynamicCodec(ArrayExample.class))
.makeMemoizing()
.runTests();
}

Expand Down Expand Up @@ -262,7 +261,6 @@ public void testNestedArray() throws Exception {
}),
new NestedArrayExample(new int[][] {{1, 2, 3}, null, {7}}))
.addCodec(new DynamicCodec(NestedArrayExample.class))
.makeMemoizing()
.runTests();
}

Expand Down

0 comments on commit fdee70e

Please sign in to comment.