Skip to content

Add encode_in_place() to encode messages in-place.#5

Open
de-vri-es wants to merge 1 commit intocbiffle:mainfrom
de-vri-es:encode-in-place
Open

Add encode_in_place() to encode messages in-place.#5
de-vri-es wants to merge 1 commit intocbiffle:mainfrom
de-vri-es:encode-in-place

Conversation

@de-vri-es
Copy link
Copy Markdown

@de-vri-es de-vri-es commented Apr 6, 2026

This is the encoding counterpart of decode_in_place. It is slower than encode_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 than encode_buf():

image

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