Implemented binary marshalling/unmarshalling#228
Open
Andrew-Morozko wants to merge 7 commits intoMasterminds:masterfrom
Open
Implemented binary marshalling/unmarshalling#228Andrew-Morozko wants to merge 7 commits intoMasterminds:masterfrom
Andrew-Morozko wants to merge 7 commits intoMasterminds:masterfrom
Conversation
Andrew-Morozko
commented
Jan 8, 2024
| 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))) |
Author
There was a problem hiding this comment.
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
Andrew-Morozko
commented
Jan 8, 2024
| // 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) |
Author
There was a problem hiding this comment.
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.
Andrew-Morozko
commented
Jan 8, 2024
|
|
||
| // MarshalBinary implements the encoding.BinaryMarshaler interface. | ||
| func (v Version) MarshalBinary() ([]byte, error) { | ||
| // Once semver has 1.19 as a minimal supported go version - |
Author
There was a problem hiding this comment.
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
Will be made obsolete when Masterminds/semver#228 is merged
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.
I had a similar issue raised by #192
I want to use
semver.Versionin 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
Binaryin terms ofTextmarshaling like so: