The strategy is used to determine whether values of type A
are a "fixed
length" encoding or "variable length" encoding.
The strategy is used to determine whether values of type A
are a "fixed
length" encoding or "variable length" encoding. In the case of fixed
length encodings, the same amount of bytes will always be used to
serialize a value. For example, all Int
values require 4 bytes. To
serialize a tensor, we can just encode all the values in row-major order.
For types with a variable length encoding, like ByteString
, each value
may require a different number of bytes. In this case, we need to be able
to quickly determine the size of a serialized value at runtime. This size
can be used when serializing a tensor to compute an index of offsets,
which can be serialized along with the data. The index allows for random
access into the serialized data.
Note: Ideally, we'd just have 2 sub-classes of ToBytes
to describe the
different strategies, but this doesn't play well with specialization and
pattern matching.
Returns the number of bytes required to serialize a
.
Returns the number of bytes required to serialize a
.
Note: pattern matching on strategy in a specialized context leads to compile time type errors in the specialized implementations. This method should be used the size is required, since this kind of indirection to get things working correctly.
A type class for serializing values of type
A
into bytes.