]> git.lizzy.rs Git - rust.git/commitdiff
Intern valtree field vector
authorOli Scherer <github35764891676564198441@oli-obk.de>
Mon, 22 Feb 2021 14:34:23 +0000 (14:34 +0000)
committerOli Scherer <github35764891676564198441@oli-obk.de>
Fri, 12 Mar 2021 12:19:17 +0000 (12:19 +0000)
compiler/rustc_middle/src/query/mod.rs
compiler/rustc_middle/src/ty/codec.rs
compiler/rustc_middle/src/ty/consts/valtree.rs
compiler/rustc_mir/src/const_eval/mod.rs

index b0c066fb42f8a512dd635c5b60401d94b718c562..49ab1084e583f83aaecd87687bee880aec10ef62 100644 (file)
     /// return `None` if that is not possible.
     query const_to_valtree(
         key: ty::ParamEnvAnd<'tcx, ConstAlloc<'tcx>>
-    ) -> Option<ty::ValTree> {
+    ) -> Option<ty::ValTree<'tcx>> {
         desc { "destructure constant" }
     }
 
index 73ad87a9ef219149776dcc5fd76c6c60507f781c..f796534c2e1188d62f86a41a32ac5a60d521bdc0 100644 (file)
@@ -333,6 +333,16 @@ fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
     }
 }
 
+impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
+    fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
+        Ok(decoder.tcx().arena.alloc_from_iter(
+            (0..decoder.read_usize()?)
+                .map(|_| Decodable::decode(decoder))
+                .collect::<Result<Vec<_>, _>>()?,
+        ))
+    }
+}
+
 impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation {
     fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
         Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?))
index 9b42023f0540e0cc1211f37181a2ba621bc8ebcd..6c8eea8c768158738cbcd2378014b9777e1f5be5 100644 (file)
@@ -1,15 +1,15 @@
 use super::ScalarInt;
 use rustc_macros::HashStable;
 
-#[derive(Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
+#[derive(Copy, Clone, Debug, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
 #[derive(HashStable)]
-pub enum ValTree {
+pub enum ValTree<'tcx> {
     Leaf(ScalarInt),
-    Branch(Vec<ValTree>),
+    Branch(&'tcx [ValTree<'tcx>]),
 }
 
-impl ValTree {
+impl ValTree<'tcx> {
     pub fn zst() -> Self {
-        Self::Branch(Vec::new())
+        Self::Branch(&[])
     }
 }
index fbd2d5d78a72a9e490b3d36939caf49b18cfc26b..5e77cc9daf38ac19f5f1a20a75fd6ec3bd826171 100644 (file)
@@ -43,7 +43,7 @@ pub(crate) fn const_to_valtree<'tcx>(
     tcx: TyCtxt<'tcx>,
     param_env: ty::ParamEnv<'tcx>,
     raw: ConstAlloc<'tcx>,
-) -> Option<ty::ValTree> {
+) -> Option<ty::ValTree<'tcx>> {
     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
     let place = ecx.raw_const_to_mplace(raw).unwrap();
     const_to_valtree_inner(&ecx, &place)
@@ -52,7 +52,7 @@ pub(crate) fn const_to_valtree<'tcx>(
 fn const_to_valtree_inner<'tcx>(
     ecx: &CompileTimeEvalContext<'tcx, 'tcx>,
     place: &MPlaceTy<'tcx>,
-) -> Option<ty::ValTree> {
+) -> Option<ty::ValTree<'tcx>> {
     let branches = |n, variant| {
         let place = match variant {
             Some(variant) => ecx.mplace_downcast(&place, variant).unwrap(),
@@ -64,7 +64,11 @@ fn const_to_valtree_inner<'tcx>(
             let field = ecx.mplace_field(&place, i).unwrap();
             const_to_valtree_inner(ecx, &field)
         });
-        Some(ty::ValTree::Branch(variant.into_iter().chain(fields).collect::<Option<_>>()?))
+        Some(ty::ValTree::Branch(
+            ecx.tcx
+                .arena
+                .alloc_from_iter(variant.into_iter().chain(fields).collect::<Option<Vec<_>>>()?),
+        ))
     };
     match place.layout.ty.kind() {
         ty::FnDef(..) => Some(ty::ValTree::zst()),