mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-31 14:31:55 +00:00
Specialize single-element writes to buffer
copy_from_slice generally falls back to memcpy/memmove, which is much more expensive than we need to write a single element in. This saves 0.26% instructions on the diesel benchmark.
This commit is contained in:
parent
9f75dbfa69
commit
299ac75894
@ -91,6 +91,21 @@ impl<T: Copy> Buffer<T> {
|
|||||||
let b = self.take();
|
let b = self.take();
|
||||||
*self = (b.extend_from_slice)(b, Slice::from(xs));
|
*self = (b.extend_from_slice)(b, Slice::from(xs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn push(&mut self, v: T) {
|
||||||
|
// Fast path to avoid going through an FFI call.
|
||||||
|
if let Some(final_len) = self.len.checked_add(1) {
|
||||||
|
if final_len <= self.capacity {
|
||||||
|
unsafe {
|
||||||
|
*self.data.add(self.len) = v;
|
||||||
|
}
|
||||||
|
self.len = final_len;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let b = self.take();
|
||||||
|
*self = (b.extend_from_slice)(b, Slice::from(std::slice::from_ref(&v)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Write for Buffer<u8> {
|
impl Write for Buffer<u8> {
|
||||||
|
@ -114,7 +114,7 @@ impl<S> DecodeMut<'_, '_, S> for () {
|
|||||||
|
|
||||||
impl<S> Encode<S> for u8 {
|
impl<S> Encode<S> for u8 {
|
||||||
fn encode(self, w: &mut Writer, _: &mut S) {
|
fn encode(self, w: &mut Writer, _: &mut S) {
|
||||||
w.write_all(&[self]).unwrap();
|
w.push(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user