Skip to content

Improve perfromance in some hotspots - #868

Open
tkaitchuck wants to merge 1 commit into
tokio-rs:masterfrom
tkaitchuck:master
Open

tkaitchuck wants to merge 1 commit into
tokio-rs:masterfrom
tkaitchuck:master

Conversation

@tkaitchuck

@tkaitchuck tkaitchuck commented Oct 29, 2026 •

Copy link
Copy Markdown

This creates a slightly simpler implementation of two 'hot' methods. These implementations should be equivalent to the prior ones, but with better performance. I would consider both of these changes small and maintainable.

In my testing these improve the serialization performance of serde_json by 28%

Signed-off-by: Tom Kaitchuck <[email protected]>

@GlenDC GlenDC left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

~30% on serde_json -> BytesMut here too, few nits inline.

Comment thread src/buf/writer.rs
}

#[inline]
fn write_all(&mut self, src: &[u8]) -> io::Result<()> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no tests for Writer at all, maybe add one for write_all incl. partial write + WriteZero (&mut [u8] / .limit(n))?

Comment thread src/buf/writer.rs
self.buf.put_slice(src);
Ok(())
} else {
let n = self.buf.remaining_mut();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: could read remaining_mut() once up front

Comment thread src/buf/writer.rs
} else {
let n = self.buf.remaining_mut();
self.buf.put_slice(&src[..n]);
Err(io::ErrorKind::WriteZero.into())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

msg differs from std's default ("write zero" vs "failed to write whole buffer"), maybe io::Error::new(WriteZero, "failed to write whole buffer")?

Comment thread src/bytes_mut.rs
unsafe {
self.advance_mut(cnt);
// Reserved above
self.set_len(self.len() + cnt);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

put_bytes does the same advance_mut after reserve, same change works there

@GlenDC

GlenDC commented Nov 9, 2026

Copy link
Copy Markdown
Member

@tkaitchuck can you also rebase this once again on top of master? It fixes the current loom CI issue.

This branch has not been deployed

No deployments
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