Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

refactor(experimental): add support for variable-sized items in remainder-sized codecs #2020

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

lorisleiva
Copy link
Contributor

@lorisleiva lorisleiva commented Jan 9, 2024

This PR allows Array, Map and Set codecs of "remainder" size to have variable-size items (as recently updated on Umi's serializers).

These array-like codecs support the following three configurable size strategies:

  • { size: 42 } means a fixed-size array of 42 items.
  • { size: getU32Codec() } means the array is prefixed by a u32 number storing its number of items.
  • { size: "remainder" } means the remainder of the bytes should be used to encode and/or decode the items inside the array.

Prior to this PR, we were enforcing arrays of "remainder" size to have fixed-size items. This allowed us to pre-compute the size of the array by dividing the size of what's left in the bytes by the fixed size of its item codec. If the modulo of that division was not zero, we would throw an error.

However, this restriction is not actually necessary and prevents valid use cases. This PR lifts this restriction and allows arrays of "remainder" size to have variable-size items. When decoding the bytes, instead of pre-computing the size of the array, we simply decode the rest of the bytes continuously until we've reached the end of the bytes. If anything goes wrong during that process, the item codec will throw an error as it won't get the expected number of bytes.

Copy link
Contributor Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@mergify mergify bot added the community label Jan 9, 2024
@mergify mergify bot requested a review from a team January 9, 2024 18:20
@lorisleiva lorisleiva self-assigned this Jan 9, 2024
Copy link
Contributor

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

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

Nice! This sheds a lot of code!

snip

@lorisleiva lorisleiva merged commit 510ead7 into master Jan 10, 2024
7 checks passed
@lorisleiva lorisleiva deleted the loris/variable-items-in-remainder-codecs branch January 10, 2024 11:05
Copy link
Contributor

🎉 This PR is included in version 1.89.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants