-
Notifications
You must be signed in to change notification settings - Fork 151
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
base: master
Are you sure you want to change the base?
Conversation
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))) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 - |
There was a problem hiding this comment.
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 😇)
Will be made obsolete when Masterminds/semver#228 is merged
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 ofText
marshaling like so: