Skip to content

Commit 8c20808

Browse files
Write primitive types via array buffers
This allows a more efficient implementation (avoiding a fallback to memmove, which is not optimal for short writes). This saves 0.29% on diesel.
1 parent 92b2894 commit 8c20808

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

library/proc_macro/src/bridge/buffer.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,23 @@ impl<T: Copy> Buffer<T> {
7878
mem::take(self)
7979
}
8080

81+
// We have the array method separate from extending from a slice. This is
82+
// because in the case of small arrays, codegen can be more efficient
83+
// (avoiding a memmove call). With extend_from_slice, LLVM at least
84+
// currently is not able to make that optimization.
85+
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) {
86+
if xs.len() > (self.capacity - self.len) {
87+
let b = self.take();
88+
*self = (b.reserve)(b, xs.len());
89+
}
90+
unsafe {
91+
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len());
92+
self.len += xs.len();
93+
}
94+
}
95+
8196
pub(super) fn extend_from_slice(&mut self, xs: &[T]) {
82-
if xs.len() > self.capacity.wrapping_sub(self.len) {
97+
if xs.len() > (self.capacity - self.len) {
8398
let b = self.take();
8499
*self = (b.reserve)(b, xs.len());
85100
}

library/proc_macro/src/bridge/rpc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ macro_rules! rpc_encode_decode {
2727
(le $ty:ty) => {
2828
impl<S> Encode<S> for $ty {
2929
fn encode(self, w: &mut Writer, _: &mut S) {
30-
w.write_all(&self.to_le_bytes()).unwrap();
30+
w.extend_from_array(&self.to_le_bytes());
3131
}
3232
}
3333

0 commit comments

Comments
 (0)