Skip to content

Commit

Permalink
Skip type checking in homogeneous collections
Browse files Browse the repository at this point in the history
  • Loading branch information
finnegancarroll committed Oct 17, 2024
1 parent 8f05921 commit 8394a96
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,16 +700,26 @@ public final void writeOptionalInstant(@Nullable Instant instant) throws IOExcep
@SuppressWarnings("rawtypes")
final List list = (List) v;
o.writeVInt(list.size());

Writer<Object> writer = null;
for (Object item : list) {
o.writeGenericValue(item);
if (writer == null) {
writer = getWriter(getGenericType(item));
}
o.writeKnownValue(item, writer);
}
});
writers.put(Object[].class, (o, v) -> {
o.writeByte((byte) 8);
final Object[] list = (Object[]) v;
o.writeVInt(list.length);

Writer<Object> writer = null;
for (Object item : list) {
o.writeGenericValue(item);
if (writer == null) {
writer = getWriter(getGenericType(item));
}
o.writeKnownValue(item, writer);
}
});
writers.put(Map.class, (o, v) -> {
Expand Down Expand Up @@ -778,7 +788,14 @@ public final void writeOptionalInstant(@Nullable Instant instant) throws IOExcep
} else {
o.writeByte((byte) 25);
}
o.writeCollection((Set<?>) v, StreamOutput::writeGenericValue);

Writer<Object> writer = null;
for (Object e : (Set<?>) v) {
if (writer == null) {
writer = getWriter(getGenericType(e));
}
o.writeKnownValue(e, writer);
}
});
// TODO: improve serialization of BigInteger
writers.put(BigInteger.class, (o, v) -> {
Expand Down Expand Up @@ -844,6 +861,14 @@ public void writeGenericValue(@Nullable Object value) throws IOException {
writer.write(this, value);
}

public void writeKnownValue(@Nullable Object value, Writer<Object> writer) throws IOException {
if (value == null) {
writeByte((byte) -1);
return;
}
writer.write(this, value);
}

public static void checkWriteable(@Nullable Object value) throws IllegalArgumentException {
if (value == null) {
return;
Expand Down

0 comments on commit 8394a96

Please sign in to comment.