This update introduces a solution to handle data alignment in custom extensions, ensuring that encoded data is aligned in memory according to specified requirements. The main benefit of this approach is enabling zero-copy deserialization, which significantly enhances performance when working with data types like Float32Array that have strict alignment needs.
Key Steps:
Alignment Handling in Encoding:
In the Encoder class, the code checks if an alignment requirement (align) is specified for the extension before encoding it.
The necessary padding is calculated to align the data properly in memory. Padding bytes (0xc1, known as noop bytes) are inserted before the extension type to ensure the data starts at an aligned memory position.
Padding Skipping in Decoding:
The Decoder class is enhanced to recognize and skip padding bytes (0xc1) when decoding an extension. This ensures the actual extension type is correctly identified, regardless of any alignment padding added during encoding.
Storing Alignment Information:
The ExtensionCodec class is modified to store the alignment requirement for each registered extension. This alignment information is utilized during encoding to ensure that data is correctly aligned in memory.
Modification of the ExtData Class:
The ExtData class is extended to include an optional align property. This property stores the alignment requirement for each extension, allowing the encoder to handle data alignment appropriately.
This update introduces a solution to handle data alignment in custom extensions, ensuring that encoded data is aligned in memory according to specified requirements. The main benefit of this approach is enabling zero-copy deserialization, which significantly enhances performance when working with data types like
Float32Array
that have strict alignment needs.Key Steps:
Alignment Handling in Encoding:
Encoder
class, the code checks if an alignment requirement (align
) is specified for the extension before encoding it.0xc1
, known as noop bytes) are inserted before the extension type to ensure the data starts at an aligned memory position.Padding Skipping in Decoding:
Decoder
class is enhanced to recognize and skip padding bytes (0xc1
) when decoding an extension. This ensures the actual extension type is correctly identified, regardless of any alignment padding added during encoding.Storing Alignment Information:
ExtensionCodec
class is modified to store the alignment requirement for each registered extension. This alignment information is utilized during encoding to ensure that data is correctly aligned in memory.Modification of the
ExtData
Class:ExtData
class is extended to include an optionalalign
property. This property stores the alignment requirement for each extension, allowing the encoder to handle data alignment appropriately.