Skip to content

Conversation

@hicqu
Copy link

@hicqu hicqu commented Oct 20, 2020

By default there is an internal buffer in CodedOutputStream, which is always used when serializing a Message. It's useful when the output stream is a disk file or pipe. However if the output is an another memory buffer, redundant memcpy calls are involved.
This PR add a new method write_to_writer_without_buffer, which can avoid such buffer mechanism.

@hicqu hicqu changed the title Less alloc less allocate and memcpy in serialization Oct 20, 2020
fn write_to_vec(&self, v: &mut Vec<u8>) -> ProtobufResult<()> {
let size = self.compute_size() as usize;
if v.capacity() - v.len() < size {
v.reserve(size - v.len());
Copy link

Choose a reason for hiding this comment

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

I think something is off here. Does this function end up writing to the start of v or to v.len() - 1? It seems that line 115 assumes the latter and line 116 assumes the former.

self.check_initialized()?;

// cache sizes
self.compute_size();
Copy link

Choose a reason for hiding this comment

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

I think this is necessary?

Copy link
Author

Choose a reason for hiding this comment

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

No indeed. the cached internal size is not used later.

Signed-off-by: qupeng <[email protected]>
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.

2 participants