fix the serializedSize and serialize bug for empty dynamic list#56
Merged
fix the serializedSize and serialize bug for empty dynamic list#56
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes a bug in the serializedSize function for slices containing dynamic items. The old implementation incorrectly added a fixed 4-byte overhead for the slice itself when children were dynamic, causing empty slices to report a non-zero size. The new implementation correctly adds 4 bytes for each dynamic child item, ensuring empty slices report zero size and non-empty slices have accurate size calculations.
Key changes:
- Fixed
serializedSizecalculation for slices of dynamic items by moving the 4-byte offset addition inside the loop, per item - Added comprehensive test coverage for empty and non-empty slices of dynamic items
- Added test for structs containing empty dynamic lists to verify correct integration
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/lib.zig | Fixed serializedSize calculation by adding 4-byte offset per dynamic item instead of once for the slice |
| src/tests.zig | Added three comprehensive tests verifying correct size calculation and serialization for empty/non-empty dynamic slices and structs with dynamic fields |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
79dac60 to
cffdb90
Compare
Signed-off-by: Chen Kai <281165273grape@gmail.com>
cffdb90 to
18a774e
Compare
gballet
reviewed
Dec 28, 2025
Signed-off-by: Chen Kai <281165273grape@gmail.com>
gballet
approved these changes
Jan 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 pull request improves the serialization logic for arrays and slices of dynamic (variable-sized) items, ensuring that offsets are encoded relative to the start of their containing data rather than as absolute positions. It also adds comprehensive tests to verify correct behavior for empty and non-empty dynamic arrays, slices, and nested structures.
Serialization logic improvements:
serializedSizeto correctly account for offset bytes in arrays and slices of dynamic items, adding 4 bytes per dynamic element as needed.serializeso that offsets for dynamic array and slice elements are written as relative (not absolute) to the start of their containing data, fixing potential deserialization issues with nested structures.Testing enhancements: