Skip to content

Commit

Permalink
Rollup merge of rust-lang#53393 - BurntPizza:serialize-inlines, r=ale…
Browse files Browse the repository at this point in the history
…xcrichton

Mark libserialize functions as inline

Got to thinking: "what if that big pile of tiny functions isn't inlining as it should?"
So a few `replace-regex` later the local perf run says this:
<details>

![](https://i.imgur.com/gvdJEgG.png)
</details>
Not huge, but still a win, which is interesting. Want to verify with the real perf run, but I understand there's a backlog.

I didn't notice any increase in compile time or binary sizes for rustc/libs.
  • Loading branch information
kennytm committed Aug 21, 2018
2 parents f9e3af7 + 1540e8c commit b21e956
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
32 changes: 15 additions & 17 deletions src/libserialize/collection_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use std::collections::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSe
use std::rc::Rc;
use std::sync::Arc;

impl<
T: Encodable
> Encodable for LinkedList<T> {
impl<T: Encodable> Encodable for LinkedList<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
for (i, e) in self.iter().enumerate() {
Expand Down Expand Up @@ -65,10 +63,10 @@ impl<T:Decodable> Decodable for VecDeque<T> {
}
}

impl<
K: Encodable + PartialEq + Ord,
V: Encodable
> Encodable for BTreeMap<K, V> {
impl<K, V> Encodable for BTreeMap<K, V>
where K: Encodable + PartialEq + Ord,
V: Encodable
{
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
Expand All @@ -82,10 +80,10 @@ impl<
}
}

impl<
K: Decodable + PartialEq + Ord,
V: Decodable
> Decodable for BTreeMap<K, V> {
impl<K, V> Decodable for BTreeMap<K, V>
where K: Decodable + PartialEq + Ord,
V: Decodable
{
fn decode<D: Decoder>(d: &mut D) -> Result<BTreeMap<K, V>, D::Error> {
d.read_map(|d, len| {
let mut map = BTreeMap::new();
Expand All @@ -99,9 +97,9 @@ impl<
}
}

impl<
T: Encodable + PartialEq + Ord
> Encodable for BTreeSet<T> {
impl<T> Encodable for BTreeSet<T>
where T: Encodable + PartialEq + Ord
{
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_seq(self.len(), |s| {
let mut i = 0;
Expand All @@ -114,9 +112,9 @@ impl<
}
}

impl<
T: Decodable + PartialEq + Ord
> Decodable for BTreeSet<T> {
impl<T> Decodable for BTreeSet<T>
where T: Decodable + PartialEq + Ord
{
fn decode<D: Decoder>(d: &mut D) -> Result<BTreeSet<T>, D::Error> {
d.read_seq(|d, len| {
let mut set = BTreeSet::new();
Expand Down
1 change: 1 addition & 0 deletions src/libserialize/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
}
}

#[inline]
pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
write_signed_leb128_to(value, |v| write_to_vec(out, v))
}
Expand Down
3 changes: 3 additions & 0 deletions src/libserialize/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Encoder {
self.data
}

#[inline]
pub fn emit_raw_bytes(&mut self, s: &[u8]) {
self.data.extend_from_slice(s);
}
Expand Down Expand Up @@ -193,6 +194,7 @@ impl<'a> Decoder<'a> {
self.position += bytes;
}

#[inline]
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
let start = self.position;
let end = start + s.len();
Expand Down Expand Up @@ -326,6 +328,7 @@ impl<'a> serialize::Decoder for Decoder<'a> {
Ok(Cow::Borrowed(s))
}

#[inline]
fn error(&mut self, err: &str) -> Self::Error {
err.to_string()
}
Expand Down
7 changes: 4 additions & 3 deletions src/libserialize/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub trait Encoder {
self.emit_enum("Option", f)
}

#[inline]
fn emit_option_none(&mut self) -> Result<(), Self::Error> {
self.emit_enum_variant("None", 0, 0, |_| Ok(()))
}
Expand Down Expand Up @@ -560,14 +561,12 @@ impl< T: Decodable> Decodable for Box<[T]> {
}

impl<T:Encodable> Encodable for Rc<T> {
#[inline]
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
(**self).encode(s)
}
}

impl<T:Decodable> Decodable for Rc<T> {
#[inline]
fn decode<D: Decoder>(d: &mut D) -> Result<Rc<T>, D::Error> {
Ok(Rc::new(Decodable::decode(d)?))
}
Expand Down Expand Up @@ -618,7 +617,9 @@ impl<'a, T:Encodable> Encodable for Cow<'a, [T]> where [T]: ToOwned<Owned = Vec<
}
}

impl<T:Decodable+ToOwned> Decodable for Cow<'static, [T]> where [T]: ToOwned<Owned = Vec<T>> {
impl<T:Decodable+ToOwned> Decodable for Cow<'static, [T]>
where [T]: ToOwned<Owned = Vec<T>>
{
fn decode<D: Decoder>(d: &mut D) -> Result<Cow<'static, [T]>, D::Error> {
d.read_seq(|d, len| {
let mut v = Vec::with_capacity(len);
Expand Down

0 comments on commit b21e956

Please sign in to comment.