Declaration
public static void WriteDecimalOptim(
this Stream stream,
decimal val
)
Summary
Encodes a decimal in a variable number of bytes, using fewer bytes for frequently-occurring low-precision
values.
Remarks
The first byte is a "header" byte. Its top bit indicates the sign of the value, while the remaining 7 bits
encode the scale and the length, in bytes, of the mantissa component. Since the scale can be anything
between 0..28 and the length can be up to 12, this number is simply an index into a lookup table which
contains specific combinations of both values. These combinations were selected by analyzing the actual
distribution of mantissa length + exponent pairs making a few assumptions about the likely inputs into
arithmetic operations. The encoder makes sure to select a value representing the exact scale and the
minimum representable mantissa length.
The result is always at most 13 bytes long, which is the same as discarding the three unused bytes of the
raw representation.