Skip to content

Commit 9203ee7

Browse files
committed
Auto merge of #65089 - nnethercote:optimize-integral-pattern-matching, r=Mark-Simulacrum
Optimize integral pattern matching Various improvements to integral pattern matching. Together they reduce instruction counts for `unicode_normalization-check-clean` by about 16%. r? @Mark-Simulacrum
2 parents 0358617 + 2a3a544 commit 9203ee7

File tree

3 files changed

+199
-165
lines changed

3 files changed

+199
-165
lines changed

src/librustc/mir/interpret/value.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,19 @@ impl<'tcx, Tag> Scalar<Tag> {
343343
}
344344
}
345345

346+
#[inline(always)]
347+
pub fn check_raw(data: u128, size: u8, target_size: Size) {
348+
assert_eq!(target_size.bytes(), size as u64);
349+
assert_ne!(size, 0, "you should never look at the bits of a ZST");
350+
Scalar::check_data(data, size);
351+
}
352+
346353
/// Do not call this method! Use either `assert_bits` or `force_bits`.
347354
#[inline]
348355
pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
349356
match self {
350357
Scalar::Raw { data, size } => {
351-
assert_eq!(target_size.bytes(), size as u64);
352-
assert_ne!(size, 0, "you should never look at the bits of a ZST");
353-
Scalar::check_data(data, size);
358+
Self::check_raw(data, size, target_size);
354359
Ok(data)
355360
}
356361
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),

src/librustc/ty/sty.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_macros::HashStable;
1313
use crate::ty::subst::{InternalSubsts, Subst, SubstsRef, GenericArg, GenericArgKind};
1414
use crate::ty::{self, AdtDef, Discr, DefIdTree, TypeFlags, Ty, TyCtxt, TypeFoldable};
1515
use crate::ty::{List, TyS, ParamEnvAnd, ParamEnv};
16-
use crate::ty::layout::{Size, Integer, IntegerExt, VariantIdx};
16+
use crate::ty::layout::VariantIdx;
1717
use crate::util::captures::Captures;
1818
use crate::mir::interpret::{Scalar, GlobalId};
1919

@@ -24,7 +24,6 @@ use std::marker::PhantomData;
2424
use std::ops::Range;
2525
use rustc_target::spec::abi;
2626
use syntax::ast::{self, Ident};
27-
use syntax::attr::{SignedInt, UnsignedInt};
2827
use syntax::symbol::{kw, InternedString};
2928

3029
use self::InferTy::*;
@@ -2300,20 +2299,7 @@ impl<'tcx> Const<'tcx> {
23002299
ty: Ty<'tcx>,
23012300
) -> Option<u128> {
23022301
assert_eq!(self.ty, ty);
2303-
// This is purely an optimization -- layout_of is a pretty expensive operation,
2304-
// but if we can determine the size without calling it, we don't need all that complexity
2305-
// (hashing, caching, etc.). As such, try to skip it.
2306-
let size = match ty.kind {
2307-
ty::Bool => Size::from_bytes(1),
2308-
ty::Char => Size::from_bytes(4),
2309-
ty::Int(ity) => {
2310-
Integer::from_attr(&tcx, SignedInt(ity)).size()
2311-
}
2312-
ty::Uint(uty) => {
2313-
Integer::from_attr(&tcx, UnsignedInt(uty)).size()
2314-
}
2315-
_ => tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size,
2316-
};
2302+
let size = tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size;
23172303
// if `ty` does not depend on generic parameters, use an empty param_env
23182304
self.eval(tcx, param_env).val.try_to_bits(size)
23192305
}

0 commit comments

Comments
 (0)