diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2021-05-15 21:39:45 -0400 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2021-05-29 12:52:06 -0400 |
| commit | 8c2080886fa46434e097c503d3d1ee9309eadc7d (patch) | |
| tree | 3cdbf3455c24eb31a10086d9765bdc8e8d73ef9b | |
| parent | 92b2894d313b5d514d077bf65560df29cda35d13 (diff) | |
| download | rust-8c2080886fa46434e097c503d3d1ee9309eadc7d.tar.gz rust-8c2080886fa46434e097c503d3d1ee9309eadc7d.zip | |
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.
| -rw-r--r-- | library/proc_macro/src/bridge/buffer.rs | 17 | ||||
| -rw-r--r-- | library/proc_macro/src/bridge/rpc.rs | 2 |
2 files changed, 17 insertions, 2 deletions
diff --git a/library/proc_macro/src/bridge/buffer.rs b/library/proc_macro/src/bridge/buffer.rs index a2030b9b8bf..717201aef10 100644 --- a/library/proc_macro/src/bridge/buffer.rs +++ b/library/proc_macro/src/bridge/buffer.rs @@ -78,8 +78,23 @@ impl<T: Copy> Buffer<T> { mem::take(self) } + // We have the array method separate from extending from a slice. This is + // because in the case of small arrays, codegen can be more efficient + // (avoiding a memmove call). With extend_from_slice, LLVM at least + // currently is not able to make that optimization. + pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) { + if xs.len() > (self.capacity - self.len) { + let b = self.take(); + *self = (b.reserve)(b, xs.len()); + } + unsafe { + xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len()); + self.len += xs.len(); + } + } + pub(super) fn extend_from_slice(&mut self, xs: &[T]) { - if xs.len() > self.capacity.wrapping_sub(self.len) { + if xs.len() > (self.capacity - self.len) { let b = self.take(); *self = (b.reserve)(b, xs.len()); } diff --git a/library/proc_macro/src/bridge/rpc.rs b/library/proc_macro/src/bridge/rpc.rs index ee9a2cf9a97..588e6ded0f4 100644 --- a/library/proc_macro/src/bridge/rpc.rs +++ b/library/proc_macro/src/bridge/rpc.rs @@ -27,7 +27,7 @@ macro_rules! rpc_encode_decode { (le $ty:ty) => { impl<S> Encode<S> for $ty { fn encode(self, w: &mut Writer, _: &mut S) { - w.write_all(&self.to_le_bytes()).unwrap(); + w.extend_from_array(&self.to_le_bytes()); } } |
