Skip to content

Optimize proc macro bridge #85390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions library/proc_macro/src/bridge/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Buffer<T: Copy> {
data: *mut T,
len: usize,
capacity: usize,
extend_from_slice: extern "C" fn(Buffer<T>, Slice<'_, T>) -> Buffer<T>,
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>,
Copy link
Member

Choose a reason for hiding this comment

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

This is the last use of Slice and now Slice can be removed (not sure why it didn't warn/error as unused).

drop: extern "C" fn(Buffer<T>),
}

Expand Down Expand Up @@ -78,18 +78,44 @@ 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]) {
// Fast path to avoid going through an FFI call.
if let Some(final_len) = self.len.checked_add(xs.len()) {
if final_len <= self.capacity {
let dst = unsafe { slice::from_raw_parts_mut(self.data, self.capacity) };
dst[self.len..][..xs.len()].copy_from_slice(xs);
self.len = final_len;
return;
}
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 push(&mut self, v: T) {
// The code here is taken from Vec::push, and we know that reserve()
// will panic if we're exceeding isize::MAX bytes and so there's no need
// to check for overflow.
if self.len == self.capacity {
let b = self.take();
*self = (b.reserve)(b, 1);
}
unsafe {
*self.data.add(self.len) = v;
self.len += 1;
}
let b = self.take();
*self = (b.extend_from_slice)(b, Slice::from(xs));
}
}

Expand Down Expand Up @@ -131,16 +157,16 @@ impl<T: Copy> From<Vec<T>> for Buffer<T> {
}
}

extern "C" fn extend_from_slice<T: Copy>(b: Buffer<T>, xs: Slice<'_, T>) -> Buffer<T> {
extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> {
let mut v = to_vec(b);
v.extend_from_slice(&xs);
v.reserve(additional);
Buffer::from(v)
}

extern "C" fn drop<T: Copy>(b: Buffer<T>) {
mem::drop(to_vec(b));
}

Buffer { data, len, capacity, extend_from_slice, drop }
Buffer { data, len, capacity, reserve, drop }
}
}
4 changes: 2 additions & 2 deletions library/proc_macro/src/bridge/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl<S> DecodeMut<'_, '_, S> for () {

impl<S> Encode<S> for u8 {
fn encode(self, w: &mut Writer, _: &mut S) {
w.write_all(&[self]).unwrap();
w.push(self);
}
}

Expand Down