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 /library/proc_macro/src/bridge/buffer.rs | |
| 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.
Diffstat (limited to 'library/proc_macro/src/bridge/buffer.rs')
| -rw-r--r-- | library/proc_macro/src/bridge/buffer.rs | 17 | 
1 files changed, 16 insertions, 1 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()); } | 
