summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-10-12 01:10:15 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2022-10-12 01:12:53 +0200
commit1a50be2f91ecb5408e68c6fed424ef95bad47162 (patch)
tree50f94e82109a9ef7cea0dbe39bebd4a395868d3b /vespajlib
parentd81ca5ed1e254bc0dc4fe33a00d7ac641d9715aa (diff)
Reuse scratch buffer to avoid allocating and clearing a new buffer for serializing every tensor.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java18
1 files changed, 8 insertions, 10 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java b/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java
index 951c23310dc..28cbf4ef64b 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/TypedBinaryFormat.java
@@ -33,8 +33,7 @@ public class TypedBinaryFormat {
public static byte[] encode(Tensor tensor) {
GrowableByteBuffer buffer = new GrowableByteBuffer();
- encode(tensor, buffer);
- return asByteArray(buffer);
+ return asByteArray(encode(tensor, buffer));
}
public static GrowableByteBuffer encode(Tensor tensor, GrowableByteBuffer buffer) {
BinaryFormat encoder = getFormatEncoder(buffer, tensor);
@@ -56,8 +55,8 @@ public class TypedBinaryFormat {
}
private static BinaryFormat getFormatEncoder(GrowableByteBuffer buffer, Tensor tensor) {
- boolean hasMappedDimensions = tensor.type().dimensions().stream().anyMatch(d -> d.isMapped());
- boolean hasIndexedDimensions = tensor.type().dimensions().stream().anyMatch(d -> d.isIndexed());
+ boolean hasMappedDimensions = tensor.type().dimensions().stream().anyMatch(TensorType.Dimension::isMapped);
+ boolean hasIndexedDimensions = tensor.type().dimensions().stream().anyMatch(TensorType.Dimension::isIndexed);
boolean isMixed = hasMappedDimensions && hasIndexedDimensions;
// TODO: Encoding as indexed if the implementation is mixed is not yet supported so use mixed format instead
@@ -116,12 +115,11 @@ public class TypedBinaryFormat {
private static void encodeValueType(GrowableByteBuffer buffer, TensorType.Value valueType) {
switch (valueType) {
- case DOUBLE: buffer.putInt1_4Bytes(DOUBLE_VALUE_TYPE); break;
- case FLOAT: buffer.putInt1_4Bytes(FLOAT_VALUE_TYPE); break;
- case BFLOAT16: buffer.putInt1_4Bytes(BFLOAT16_VALUE_TYPE); break;
- case INT8: buffer.putInt1_4Bytes(INT8_VALUE_TYPE); break;
- default:
- throw new IllegalArgumentException("Attempt to encode unknown tensor value type: " + valueType);
+ case DOUBLE -> buffer.putInt1_4Bytes(DOUBLE_VALUE_TYPE);
+ case FLOAT -> buffer.putInt1_4Bytes(FLOAT_VALUE_TYPE);
+ case BFLOAT16 -> buffer.putInt1_4Bytes(BFLOAT16_VALUE_TYPE);
+ case INT8 -> buffer.putInt1_4Bytes(INT8_VALUE_TYPE);
+ default -> throw new IllegalArgumentException("Attempt to encode unknown tensor value type: " + valueType);
}
}