Skip to content

Commit

Permalink
BoundField.write memory optimization (#2325)
Browse files Browse the repository at this point in the history
* BoundField.write memory optimization

Declare and initialize the type adapter used for writing BoundFields outside of the anonymous class to ensure that a new TypeAdapterRuntimeTypeWrapper is not constructed each time a BoundField is written. This type adapter is only initialized if the BoundField will be used for serialization.

* Avoid confusing nullness-analysis tools
  • Loading branch information
sembseth authored Feb 28, 2023
1 parent 9f46977 commit 105f28f
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ private BoundField createBoundField(

@SuppressWarnings("unchecked")
final TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) mapped;
final TypeAdapter<Object> writeTypeAdapter;
if (serialize) {
writeTypeAdapter = jsonAdapterPresent ? typeAdapter
: new TypeAdapterRuntimeTypeWrapper<>(context, typeAdapter, fieldType.getType());
} else {
// Will never actually be used, but we set it to avoid confusing nullness-analysis tools
writeTypeAdapter = typeAdapter;
}
return new BoundField(name, field, serialize, deserialize) {
@Override void write(JsonWriter writer, Object source)
throws IOException, IllegalAccessException {
Expand Down Expand Up @@ -191,9 +199,7 @@ private BoundField createBoundField(
return;
}
writer.name(name);
TypeAdapter<Object> t = jsonAdapterPresent ? typeAdapter
: new TypeAdapterRuntimeTypeWrapper<>(context, typeAdapter, fieldType.getType());
t.write(writer, fieldValue);
writeTypeAdapter.write(writer, fieldValue);
}

@Override
Expand Down

0 comments on commit 105f28f

Please sign in to comment.