Skip to content

Commit e0103a7

Browse files
committed
Micro-optimize
1 parent 2ae197d commit e0103a7

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
250250
}
251251

252252
impl<T: ParameterizedOverTcx> LazyValue<T> {
253+
#[inline]
253254
fn decode<'a, 'tcx, M: Metadata<'a, 'tcx>>(self, metadata: M) -> T::Value<'tcx>
254255
where
255256
T::Value<'tcx>: Decodable<DecodeContext<'a, 'tcx>>,
@@ -294,6 +295,7 @@ unsafe impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> TrustedLen
294295
}
295296

296297
impl<T: ParameterizedOverTcx> LazyArray<T> {
298+
#[inline]
297299
fn decode<'a, 'tcx, M: Metadata<'a, 'tcx>>(
298300
self,
299301
metadata: M,
@@ -420,25 +422,29 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> {
420422
}
421423

422424
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for CrateNum {
425+
#[inline]
423426
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> CrateNum {
424427
let cnum = CrateNum::from_u32(d.read_u32());
425428
d.map_encoded_cnum_to_current(cnum)
426429
}
427430
}
428431

429432
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefIndex {
433+
#[inline]
430434
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefIndex {
431435
DefIndex::from_u32(d.read_u32())
432436
}
433437
}
434438

435439
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnIndex {
440+
#[inline]
436441
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ExpnIndex {
437442
ExpnIndex::from_u32(d.read_u32())
438443
}
439444
}
440445

441446
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ast::AttrId {
447+
#[inline]
442448
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ast::AttrId {
443449
let sess = d.sess.expect("can't decode AttrId without Session");
444450
sess.parse_sess.attr_id_generator.mk_attr_id()
@@ -657,6 +663,7 @@ impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyValue<T> {
657663
}
658664

659665
impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
666+
#[inline]
660667
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
661668
let len = decoder.read_usize();
662669
if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }

compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ where
480480
for<'tcx> T::Value<'tcx>: FixedSizeEncoding<ByteArray = [u8; N]>,
481481
{
482482
/// Given the metadata, extract out the value at a particular index (if any).
483-
#[inline(never)]
484483
pub(super) fn get<'a, 'tcx, M: Metadata<'a, 'tcx>>(&self, metadata: M, i: I) -> T::Value<'tcx> {
485484
trace!("LazyTable::lookup: index={:?} len={:?}", i, self.len);
486485

@@ -494,9 +493,13 @@ where
494493
let end = start + width;
495494
let bytes = &metadata.blob()[start..end];
496495

497-
let mut fixed = [0u8; N];
498-
fixed[..width].copy_from_slice(bytes);
499-
FixedSizeEncoding::from_bytes(&fixed)
496+
if let Ok(fixed) = bytes.try_into() {
497+
FixedSizeEncoding::from_bytes(fixed)
498+
} else {
499+
let mut fixed = [0u8; N];
500+
fixed[..width].copy_from_slice(bytes);
501+
FixedSizeEncoding::from_bytes(&fixed)
502+
}
500503
}
501504

502505
/// Size of the table in entries, including possible gaps.

0 commit comments

Comments
 (0)