Skip to content

Commit

Permalink
Auto merge of #97604 - nnethercote:inline-bridge-Buffer-methods, r=eddyb
Browse files Browse the repository at this point in the history
Inline `bridge::Buffer` methods.

This fixes a performance regression caused by making `Buffer`
non-generic in #97004.

r? `@eddyb`
  • Loading branch information
bors committed Jun 4, 2022
2 parents c3384ea + dee353d commit cb0584f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions library/proc_macro/src/bridge/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,39 @@ unsafe impl Sync for Buffer {}
unsafe impl Send for Buffer {}

impl Default for Buffer {
#[inline]
fn default() -> Self {
Self::from(vec![])
}
}

impl Deref for Buffer {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self.data as *const u8, self.len) }
}
}

impl DerefMut for Buffer {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
}
}

impl Buffer {
#[inline]
pub(super) fn new() -> Self {
Self::default()
}

#[inline]
pub(super) fn clear(&mut self) {
self.len = 0;
}

#[inline]
pub(super) fn take(&mut self) -> Self {
mem::take(self)
}
Expand All @@ -53,6 +59,7 @@ impl Buffer {
// 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.
#[inline]
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
if xs.len() > (self.capacity - self.len) {
let b = self.take();
Expand All @@ -64,6 +71,7 @@ impl Buffer {
}
}

#[inline]
pub(super) fn extend_from_slice(&mut self, xs: &[u8]) {
if xs.len() > (self.capacity - self.len) {
let b = self.take();
Expand All @@ -75,6 +83,7 @@ impl Buffer {
}
}

#[inline]
pub(super) fn push(&mut self, v: u8) {
// 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
Expand All @@ -91,22 +100,26 @@ impl Buffer {
}

impl Write for Buffer {
#[inline]
fn write(&mut self, xs: &[u8]) -> io::Result<usize> {
self.extend_from_slice(xs);
Ok(xs.len())
}

#[inline]
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> {
self.extend_from_slice(xs);
Ok(())
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

impl Drop for Buffer {
#[inline]
fn drop(&mut self) {
let b = self.take();
(b.drop)(b);
Expand Down

0 comments on commit cb0584f

Please sign in to comment.