Skip to content

Commit 881c989

Browse files
authored
Rollup merge of #109324 - cjgillot:fixed-unused-params, r=Nilstrieb
Implement FixedSizeEncoding for UnusedGenericParams. Using a `Lazy` for actually a `u32` value is 50% overhead, so let's encode the bitset directly.
2 parents 1f0fcf1 + 38be6f2 commit 881c989

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,7 @@ provide! { tcx, def_id, other, cdata,
226226
lookup_default_body_stability => { table }
227227
lookup_deprecation_entry => { table }
228228
params_in_repr => { table }
229-
// FIXME: Could be defaulted, but `LazyValue<UnusedGenericParams>` is not `FixedSizeEncoding`..
230-
unused_generic_params => {
231-
cdata
232-
.root
233-
.tables
234-
.unused_generic_params
235-
.get(cdata, def_id.index)
236-
.map_or_else(|| ty::UnusedGenericParams::new_all_used(), |lazy| lazy.decode((cdata, tcx)))
237-
}
229+
unused_generic_params => { cdata.root.tables.unused_generic_params.get(cdata, def_id.index) }
238230
opt_def_kind => { table_direct }
239231
impl_parent => { table }
240232
impl_polarity => { table_direct }

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1440,9 +1440,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14401440
let instance =
14411441
ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id.to_def_id()));
14421442
let unused = tcx.unused_generic_params(instance);
1443-
if !unused.all_used() {
1444-
record!(self.tables.unused_generic_params[def_id.to_def_id()] <- unused);
1445-
}
1443+
self.tables.unused_generic_params.set(def_id.local_def_index, unused);
14461444
}
14471445

14481446
// Encode all the deduced parameter attributes for everything that has MIR, even for items

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ define_tables! {
356356
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
357357
associated_items_for_impl_trait_in_trait: Table<DefIndex, LazyArray<DefId>>,
358358
opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
359+
unused_generic_params: Table<DefIndex, UnusedGenericParams>,
359360

360361
- optional:
361362
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
@@ -398,7 +399,6 @@ define_tables! {
398399
trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
399400
trait_item_def_id: Table<DefIndex, RawDefId>,
400401
expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
401-
unused_generic_params: Table<DefIndex, LazyValue<UnusedGenericParams>>,
402402
params_in_repr: Table<DefIndex, LazyValue<BitSet<u32>>>,
403403
repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
404404
// `def_keys` and `def_path_hashes` represent a lazy version of a

compiler/rustc_metadata/src/rmeta/table.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::rmeta::*;
33
use rustc_data_structures::fingerprint::Fingerprint;
44
use rustc_hir::def::{CtorKind, CtorOf};
55
use rustc_index::vec::Idx;
6-
use rustc_middle::ty::ParameterizedOverTcx;
6+
use rustc_middle::ty::{ParameterizedOverTcx, UnusedGenericParams};
77
use rustc_serialize::opaque::FileEncoder;
88
use rustc_serialize::Encoder as _;
99
use rustc_span::hygiene::MacroKind;
@@ -50,6 +50,16 @@ impl IsDefault for DefPathHash {
5050
}
5151
}
5252

53+
impl IsDefault for UnusedGenericParams {
54+
fn is_default(&self) -> bool {
55+
// UnusedGenericParams encodes the *un*usedness as a bitset.
56+
// This means that 0 corresponds to all bits used, which is indeed the default.
57+
let is_default = self.bits() == 0;
58+
debug_assert_eq!(is_default, self.all_used());
59+
is_default
60+
}
61+
}
62+
5363
/// Helper trait, for encoding to, and decoding from, a fixed number of bytes.
5464
/// Used mainly for Lazy positions and lengths.
5565
/// Unchecked invariant: `Self::default()` should encode as `[0; BYTE_LEN]`,
@@ -271,6 +281,21 @@ impl FixedSizeEncoding for bool {
271281
}
272282
}
273283

284+
impl FixedSizeEncoding for UnusedGenericParams {
285+
type ByteArray = [u8; 4];
286+
287+
#[inline]
288+
fn from_bytes(b: &[u8; 4]) -> Self {
289+
let x: u32 = u32::from_bytes(b);
290+
UnusedGenericParams::from_bits(x)
291+
}
292+
293+
#[inline]
294+
fn write_to_bytes(self, b: &mut [u8; 4]) {
295+
self.bits().write_to_bytes(b);
296+
}
297+
}
298+
274299
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
275300
// generic `LazyValue<T>` impl, but in the general case we might not need / want
276301
// to fit every `usize` in `u32`.

compiler/rustc_middle/src/ty/instance.rs

+14
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,12 @@ fn needs_fn_once_adapter_shim(
781781
#[derive(Debug, Copy, Clone, Eq, PartialEq, Decodable, Encodable, HashStable)]
782782
pub struct UnusedGenericParams(FiniteBitSet<u32>);
783783

784+
impl Default for UnusedGenericParams {
785+
fn default() -> Self {
786+
UnusedGenericParams::new_all_used()
787+
}
788+
}
789+
784790
impl UnusedGenericParams {
785791
pub fn new_all_unused(amount: u32) -> Self {
786792
let mut bitset = FiniteBitSet::new_empty();
@@ -807,4 +813,12 @@ impl UnusedGenericParams {
807813
pub fn all_used(&self) -> bool {
808814
self.0.is_empty()
809815
}
816+
817+
pub fn bits(&self) -> u32 {
818+
self.0.0
819+
}
820+
821+
pub fn from_bits(bits: u32) -> UnusedGenericParams {
822+
UnusedGenericParams(FiniteBitSet(bits))
823+
}
810824
}

0 commit comments

Comments
 (0)