Skip to content

Commit 753ea76

Browse files
authored
Auto merge of rust-lang#37083 - nnethercote:uleb128, r=eddyb
Inline read_{un,}signed_leb128 and opaque::Decoder functions. `read_unsigned_leb128` is hot within rustc because it's heavily used during the reading of crate metadata. This commit tweaks its signature (and that of `read_signed_leb128`, for consistency) so it can increment the buffer index directly instead of maintaining its own copy, the change in which is then used by the caller to advance the index. This reduces the instruction count (as measured by Cachegrind) for some benchmarks a bit, e.g. hyper-0.5.0 by 0.7%.
2 parents 3543a0f + 6a4bb35 commit 753ea76

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/libserialize/leb128.rs

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn write_unsigned_leb128(out: &mut Vec<u8>, start_position: usize, mut value
3838
return position - start_position;
3939
}
4040

41+
#[inline]
4142
pub fn read_unsigned_leb128(data: &[u8], start_position: usize) -> (u64, usize) {
4243
let mut result = 0;
4344
let mut shift = 0;
@@ -78,6 +79,7 @@ pub fn write_signed_leb128(out: &mut Vec<u8>, start_position: usize, mut value:
7879
return position - start_position;
7980
}
8081

82+
#[inline]
8183
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i64, usize) {
8284
let mut result = 0;
8385
let mut shift = 0;

src/libserialize/opaque.rs

+16
Original file line numberDiff line numberDiff line change
@@ -179,74 +179,90 @@ macro_rules! read_sleb128 {
179179
impl<'a> serialize::Decoder for Decoder<'a> {
180180
type Error = String;
181181

182+
#[inline]
182183
fn read_nil(&mut self) -> Result<(), Self::Error> {
183184
Ok(())
184185
}
185186

187+
#[inline]
186188
fn read_u64(&mut self) -> Result<u64, Self::Error> {
187189
read_uleb128!(self, u64)
188190
}
189191

192+
#[inline]
190193
fn read_u32(&mut self) -> Result<u32, Self::Error> {
191194
read_uleb128!(self, u32)
192195
}
193196

197+
#[inline]
194198
fn read_u16(&mut self) -> Result<u16, Self::Error> {
195199
read_uleb128!(self, u16)
196200
}
197201

202+
#[inline]
198203
fn read_u8(&mut self) -> Result<u8, Self::Error> {
199204
let value = self.data[self.position];
200205
self.position += 1;
201206
Ok(value)
202207
}
203208

209+
#[inline]
204210
fn read_usize(&mut self) -> Result<usize, Self::Error> {
205211
read_uleb128!(self, usize)
206212
}
207213

214+
#[inline]
208215
fn read_i64(&mut self) -> Result<i64, Self::Error> {
209216
read_sleb128!(self, i64)
210217
}
211218

219+
#[inline]
212220
fn read_i32(&mut self) -> Result<i32, Self::Error> {
213221
read_sleb128!(self, i32)
214222
}
215223

224+
#[inline]
216225
fn read_i16(&mut self) -> Result<i16, Self::Error> {
217226
read_sleb128!(self, i16)
218227
}
219228

229+
#[inline]
220230
fn read_i8(&mut self) -> Result<i8, Self::Error> {
221231
let as_u8 = self.data[self.position];
222232
self.position += 1;
223233
unsafe { Ok(::std::mem::transmute(as_u8)) }
224234
}
225235

236+
#[inline]
226237
fn read_isize(&mut self) -> Result<isize, Self::Error> {
227238
read_sleb128!(self, isize)
228239
}
229240

241+
#[inline]
230242
fn read_bool(&mut self) -> Result<bool, Self::Error> {
231243
let value = self.read_u8()?;
232244
Ok(value != 0)
233245
}
234246

247+
#[inline]
235248
fn read_f64(&mut self) -> Result<f64, Self::Error> {
236249
let bits = self.read_u64()?;
237250
Ok(unsafe { ::std::mem::transmute(bits) })
238251
}
239252

253+
#[inline]
240254
fn read_f32(&mut self) -> Result<f32, Self::Error> {
241255
let bits = self.read_u32()?;
242256
Ok(unsafe { ::std::mem::transmute(bits) })
243257
}
244258

259+
#[inline]
245260
fn read_char(&mut self) -> Result<char, Self::Error> {
246261
let bits = self.read_u32()?;
247262
Ok(::std::char::from_u32(bits).unwrap())
248263
}
249264

265+
#[inline]
250266
fn read_str(&mut self) -> Result<Cow<str>, Self::Error> {
251267
let len = self.read_usize()?;
252268
let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();

0 commit comments

Comments
 (0)