From 006247911eaf2ffb0376c7ac52fcd6fa23a85f4f Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Fri, 13 Jul 2018 15:07:24 +0200 Subject: [PATCH] Add inline annotations on Vec's Deref and DerefMut While doing profiling on https://github.com/BurntSushi/aho-corasick I noticed that `Vec::deref_mut` would show up as a hot function despite the fact that it should be a no-op that should be trivially inlined. (as the layout of `Vec` is that of `&[T]` + a capacity after). I'd expect that the method(s) would usually be inlined despite the lack of an inline in most cases but for the crate in question I observed a 1.2x speedup if I replaced the deref with `::std::mem::transmute_copy` so there was definitely a noticeable performance regression. Also, I recognize that it is likely that, were I to use different version of rustc, then the problem could go away. But the fact remains that (evidently) some versions of rustc misses this optimization and maybe an inline annotation can help to avoid this. --- src/liballoc/vec.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 5efe1e23309a7..a4e2fd4455c8c 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -1734,6 +1734,7 @@ where impl ops::Deref for Vec { type Target = [T]; + #[inline] fn deref(&self) -> &[T] { unsafe { let p = self.buf.ptr(); @@ -1745,6 +1746,7 @@ impl ops::Deref for Vec { #[stable(feature = "rust1", since = "1.0.0")] impl ops::DerefMut for Vec { + #[inline] fn deref_mut(&mut self) -> &mut [T] { unsafe { let ptr = self.buf.ptr();