Implement Alignment Support for Custom Extensions in Encoder/Decoder #245
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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
Float32Arraythat have strict alignment needs.Key Steps:
Alignment Handling in Encoding:
Encoderclass, 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:
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.Storing Alignment Information:
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.Modification of the
ExtDataClass:ExtDataclass is extended to include an optionalalignproperty. This property stores the alignment requirement for each extension, allowing the encoder to handle data alignment appropriately.