Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented binary marshalling/unmarshalling #228

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Andrew-Morozko
Copy link

@Andrew-Morozko Andrew-Morozko commented Jan 8, 2024

I had a similar issue raised by #192

I want to use semver.Version in an encoding/gob serialized structure, so it needs to implement encoding.BinaryMarshaler and encoding.BinaryUnmarshaler.

I decided to use the fact that it's specifically a binary encoding and used varints from "encoding/binary". This makes binary marshaling 11.2x faster and unmarshaling 8.9x faster than text-based operations.

If you don't like low-level (although tested and straightforward) code you can implement Binary in terms of Text marshaling like so:

func (v Version) MarshalBinary() ([]byte, error) {
	return v.MarshalText()
}

func (v *Version) UnmarshalBinary(data []byte) error {
	return v.UnmarshalText(data)
}

n += binary.PutUvarint(buf[n:], v.patch)
n += binary.PutUvarint(buf[n:], uint64(len(v.pre)))
n += copy(buf[n:], v.pre)
n += binary.PutUvarint(buf[n:], uint64(len(v.metadata)))
Copy link
Author

@Andrew-Morozko Andrew-Morozko Jan 8, 2024

Choose a reason for hiding this comment

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

Technically we can omit the last string length if we assume that the data slice is always correctly sized, but that seems to be a very niche optimization

// we can allocate a smaller buffer, assuming 5 Uvarints are (usually) <128
buf := make([]byte, 5*binary.MaxVarintLen64+len(v.pre)+len(v.metadata))
n := 0
n += binary.PutUvarint(buf[n:], v.major)
Copy link
Author

Choose a reason for hiding this comment

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

Is the major-minor-patch-pre-metadata combo completely set in stone? If not, then perhaps we should throw in a "binary format version" number.


// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (v Version) MarshalBinary() ([]byte, error) {
// Once semver has 1.19 as a minimal supported go version -
Copy link
Author

Choose a reason for hiding this comment

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

Or we can just copy the obvious 5-line long function from golang (if the license permits that, of course 😇)

Andrew-Morozko added a commit to blackstork-io/fabric that referenced this pull request Jan 8, 2024
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.

1 participant