msgpack-javascript
msgpack-javascript copied to clipboard
Implement Alignment Support for Custom Extensions in Encoder/Decoder
trafficstars
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
Encoderclass, 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.
- In the
-
Padding Skipping in Decoding:
- The
Decoderclass 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.
- The
-
Storing Alignment Information:
- The
ExtensionCodecclass 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.
- The
-
Modification of the
ExtDataClass:- The
ExtDataclass is extended to include an optionalalignproperty. This property stores the alignment requirement for each extension, allowing the encoder to handle data alignment appropriately.
- The