Skip to content

Commit 07bdbae

Browse files
committed
Auto merge of #149054 - oli-obk:min-encode, r=fee1-dead
Avoid encoding non-constness or non-asyncness in metadata r? `@fee1-dead` Let's see if we can get any benefit (even just metadata size) from not encoding the common case. Inspired by #148434 (comment)
2 parents 140044c + 357fd66 commit 07bdbae

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,8 +4224,10 @@ impl fmt::Display for Safety {
42244224
}
42254225

42264226
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)]
4227+
#[derive(Default)]
42274228
pub enum Constness {
42284229
Const,
4230+
#[default]
42294231
NotConst,
42304232
}
42314233

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,10 +1523,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15231523
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
15241524
}
15251525
if should_encode_constness(def_kind) {
1526-
self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
1526+
let constness = self.tcx.constness(def_id);
1527+
match constness {
1528+
hir::Constness::Const => self.tables.constness.set(def_id.index, constness),
1529+
hir::Constness::NotConst => {}
1530+
}
15271531
}
15281532
if let DefKind::Fn | DefKind::AssocFn = def_kind {
1529-
self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id));
1533+
let asyncness = tcx.asyncness(def_id);
1534+
match asyncness {
1535+
ty::Asyncness::Yes => self.tables.asyncness.set(def_id.index, asyncness),
1536+
ty::Asyncness::No => {}
1537+
}
15301538
record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id));
15311539
}
15321540
if let Some(name) = tcx.intrinsic(def_id) {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ define_tables! {
400400
// individually instead of `DefId`s.
401401
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
402402
cross_crate_inlinable: Table<DefIndex, bool>,
403+
asyncness: Table<DefIndex, ty::Asyncness>,
404+
constness: Table<DefIndex, hir::Constness>,
403405

404406
- optional:
405407
attributes: Table<DefIndex, LazyArray<hir::Attribute>>,
@@ -433,15 +435,13 @@ define_tables! {
433435
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
434436
thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
435437
impl_parent: Table<DefIndex, RawDefId>,
436-
constness: Table<DefIndex, hir::Constness>,
437438
const_conditions: Table<DefIndex, LazyValue<ty::ConstConditions<'static>>>,
438439
defaultness: Table<DefIndex, hir::Defaultness>,
439440
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?
440441
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
441442
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
442443
rendered_const: Table<DefIndex, LazyValue<String>>,
443444
rendered_precise_capturing_args: Table<DefIndex, LazyArray<PreciseCapturingArgKind<Symbol, Symbol>>>,
444-
asyncness: Table<DefIndex, ty::Asyncness>,
445445
fn_arg_idents: Table<DefIndex, LazyArray<Option<Ident>>>,
446446
coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
447447
coroutine_for_closure: Table<DefIndex, RawDefId>,

compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ impl IsDefault for bool {
2525
}
2626
}
2727

28+
impl IsDefault for ty::Asyncness {
29+
fn is_default(&self) -> bool {
30+
match self {
31+
ty::Asyncness::Yes => false,
32+
ty::Asyncness::No => true,
33+
}
34+
}
35+
}
36+
37+
impl IsDefault for hir::Constness {
38+
fn is_default(&self) -> bool {
39+
match self {
40+
rustc_hir::Constness::Const => false,
41+
rustc_hir::Constness::NotConst => true,
42+
}
43+
}
44+
}
45+
2846
impl IsDefault for u32 {
2947
fn is_default(&self) -> bool {
3048
*self == 0
@@ -295,6 +313,50 @@ impl FixedSizeEncoding for bool {
295313
}
296314
}
297315

316+
impl FixedSizeEncoding for ty::Asyncness {
317+
type ByteArray = [u8; 1];
318+
319+
#[inline]
320+
fn from_bytes(b: &[u8; 1]) -> Self {
321+
match b[0] {
322+
0 => ty::Asyncness::No,
323+
1 => ty::Asyncness::Yes,
324+
_ => unreachable!(),
325+
}
326+
}
327+
328+
#[inline]
329+
fn write_to_bytes(self, b: &mut [u8; 1]) {
330+
debug_assert!(!self.is_default());
331+
b[0] = match self {
332+
ty::Asyncness::No => 0,
333+
ty::Asyncness::Yes => 1,
334+
}
335+
}
336+
}
337+
338+
impl FixedSizeEncoding for hir::Constness {
339+
type ByteArray = [u8; 1];
340+
341+
#[inline]
342+
fn from_bytes(b: &[u8; 1]) -> Self {
343+
match b[0] {
344+
0 => hir::Constness::NotConst,
345+
1 => hir::Constness::Const,
346+
_ => unreachable!(),
347+
}
348+
}
349+
350+
#[inline]
351+
fn write_to_bytes(self, b: &mut [u8; 1]) {
352+
debug_assert!(!self.is_default());
353+
b[0] = match self {
354+
hir::Constness::NotConst => 0,
355+
hir::Constness::Const => 1,
356+
}
357+
}
358+
}
359+
298360
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
299361
// generic `LazyValue<T>` impl, but in the general case we might not need / want
300362
// to fit every `usize` in `u32`.

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,10 @@ pub struct ImplTraitHeader<'tcx> {
250250
}
251251

252252
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
253-
#[derive(TypeFoldable, TypeVisitable)]
253+
#[derive(TypeFoldable, TypeVisitable, Default)]
254254
pub enum Asyncness {
255255
Yes,
256+
#[default]
256257
No,
257258
}
258259

0 commit comments

Comments
 (0)