Skip to content

Conversation

@GrapeBaBa
Copy link
Member

@GrapeBaBa GrapeBaBa commented Dec 21, 2025

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:

  • Updated serializedSize to correctly account for offset bytes in arrays and slices of dynamic items, adding 4 bytes per dynamic element as needed.
  • Modified serialize so 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:

  • Added extensive tests covering empty and non-empty slices/arrays of dynamic items, structs containing dynamic lists, and deeply nested dynamic structures, ensuring correct size calculations and relative offset encoding in all cases.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 serializedSize calculation 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.

@GrapeBaBa GrapeBaBa force-pushed the grapebaba/empty_dynamic_list_size branch from 79dac60 to cffdb90 Compare December 21, 2025 06:43
Signed-off-by: Chen Kai <281165273grape@gmail.com>
@GrapeBaBa GrapeBaBa force-pushed the grapebaba/empty_dynamic_list_size branch from cffdb90 to 18a774e Compare December 21, 2025 10:22
@GrapeBaBa GrapeBaBa changed the title fix: fix the serializedSize bug for empty dynamic list fix: fix the serializedSize and serialize bug for empty dynamic list Dec 21, 2025
Copy link
Member

@g11tech g11tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me @gballet have a look

@gballet gballet changed the title fix: fix the serializedSize and serialize bug for empty dynamic list fix the serializedSize and serialize bug for empty dynamic list Dec 31, 2025
Signed-off-by: Chen Kai <281165273grape@gmail.com>
@gballet gballet merged commit 5ce7322 into master Jan 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants