Skip to content

Commit 0fe4f38

Browse files
committed
Intern valtree field vector
1 parent a4fbac1 commit 0fe4f38

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ rustc_queries! {
789789
/// return `None` if that is not possible.
790790
query const_to_valtree(
791791
key: ty::ParamEnvAnd<'tcx, ConstAlloc<'tcx>>
792-
) -> Option<ty::ValTree> {
792+
) -> Option<ty::ValTree<'tcx>> {
793793
desc { "destructure constant" }
794794
}
795795

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,16 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> {
333333
}
334334
}
335335

336+
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
337+
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
338+
Ok(decoder.tcx().arena.alloc_from_iter(
339+
(0..decoder.read_usize()?)
340+
.map(|_| Decodable::decode(decoder))
341+
.collect::<Result<Vec<_>, _>>()?,
342+
))
343+
}
344+
}
345+
336346
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation {
337347
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
338348
Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?))
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use super::ScalarInt;
22
use rustc_macros::HashStable;
33

4-
#[derive(Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
4+
#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
55
#[derive(HashStable)]
6-
pub enum ValTree {
6+
pub enum ValTree<'tcx> {
77
Leaf(ScalarInt),
8-
Branch(Vec<ValTree>),
8+
Branch(&'tcx [ValTree<'tcx>]),
99
}
1010

11-
impl ValTree {
11+
impl ValTree<'tcx> {
1212
pub fn zst() -> Self {
13-
Self::Branch(Vec::new())
13+
Self::Branch(&[])
1414
}
1515
}

compiler/rustc_mir/src/const_eval/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn const_to_valtree<'tcx>(
4343
tcx: TyCtxt<'tcx>,
4444
param_env: ty::ParamEnv<'tcx>,
4545
raw: ConstAlloc<'tcx>,
46-
) -> Option<ty::ValTree> {
46+
) -> Option<ty::ValTree<'tcx>> {
4747
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
4848
let place = ecx.raw_const_to_mplace(raw).unwrap();
4949
const_to_valtree_inner(&ecx, &place)
@@ -52,7 +52,7 @@ pub(crate) fn const_to_valtree<'tcx>(
5252
fn const_to_valtree_inner<'tcx>(
5353
ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
5454
place: &MPlaceTy<'tcx>,
55-
) -> Option<ty::ValTree> {
55+
) -> Option<ty::ValTree<'tcx>> {
5656
let branches = |n, variant| {
5757
let place = match variant {
5858
Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
@@ -64,7 +64,11 @@ fn const_to_valtree_inner<'tcx>(
6464
let field = ecx.mplace_field(&place, i).unwrap();
6565
const_to_valtree_inner(ecx, &field)
6666
});
67-
Some(ty::ValTree::Branch(variant.into_iter().chain(fields).collect::<Option<_>>()?))
67+
Some(ty::ValTree::Branch(
68+
ecx.tcx
69+
.arena
70+
.alloc_from_iter(variant.into_iter().chain(fields).collect::<Option<Vec<_>>>()?),
71+
))
6872
};
6973
match place.layout.ty.kind() {
7074
ty::FnDef(..) => Some(ty::ValTree::zst()),

0 commit comments

Comments
 (0)