Add encode_in_place() to encode messages in-place.#5
Open
de-vri-es wants to merge 1 commit intocbiffle:mainfrom
Open
Add encode_in_place() to encode messages in-place.#5de-vri-es wants to merge 1 commit intocbiffle:mainfrom
de-vri-es wants to merge 1 commit intocbiffle:mainfrom
Conversation
9325fa7 to
6802568
Compare
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.
This is the encoding counterpart of
decode_in_place. It is slower thanencode_buf, but on embedded platforms it may be more important to save memory than to be as fast as possible.Encoding in-place is slightly more complicated than decoding, because the message will grow while encoding it, and we must prevent overwriting data that hasn't been encoded yet.
To do that, the encoding works backwards: encode the message back to front. And do to that, it firsts computes where the encoded message will end so it can start writing there.
An alternative approaches would be to move the entire message to the back of the buffer first, and then encode from the front as usual. However, I wanted to avoid copying the whole message. I'm assuming that computing the output length is faster than writing to RAM (especially on embedded CPUs).
I added fuzzing and some test cases to ensure the implementation is correct.
Added a benchmark to compare it with the existing encoders: on my laptop it's still significantly faster than
encode_iter()and indeed slower thanencode_buf():