Skip to content

Commit 6184a96

Browse files
committed
Auto merge of #104013 - notriddle:notriddle/rustdoc-sizeof, r=GuillaumeGomez
rustdoc: use `ThinVec` and `Box<str>` to shrink `clean::ItemKind`
2 parents 73c9eaf + e410cd2 commit 6184a96

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::iter::once;
44
use std::sync::Arc;
55

6-
use thin_vec::ThinVec;
6+
use thin_vec::{thin_vec, ThinVec};
77

88
use rustc_ast as ast;
99
use rustc_data_structures::fx::FxHashSet;
@@ -605,7 +605,7 @@ fn build_module_items(
605605
clean::ImportSource {
606606
path: clean::Path {
607607
res,
608-
segments: vec![clean::PathSegment {
608+
segments: thin_vec![clean::PathSegment {
609609
name: prim_ty.as_sym(),
610610
args: clean::GenericArgs::AngleBracketed {
611611
args: Default::default(),

src/librustdoc/clean/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub(crate) fn clean_middle_const<'tcx>(
226226
// FIXME: instead of storing the stringified expression, store `self` directly instead.
227227
Constant {
228228
type_: clean_middle_ty(constant.ty(), cx, None),
229-
kind: ConstantKind::TyConst { expr: constant.to_string() },
229+
kind: ConstantKind::TyConst { expr: constant.to_string().into() },
230230
}
231231
}
232232

@@ -1215,7 +1215,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
12151215
true
12161216
}
12171217
(GenericParamDefKind::Const { .. }, GenericArg::Const(c)) => match &c.kind {
1218-
ConstantKind::TyConst { expr } => expr == param.name.as_str(),
1218+
ConstantKind::TyConst { expr } => **expr == *param.name.as_str(),
12191219
_ => false,
12201220
},
12211221
_ => false,
@@ -1554,7 +1554,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
15541554
}
15551555
};
15561556

1557-
Array(Box::new(clean_ty(ty, cx)), length)
1557+
Array(Box::new(clean_ty(ty, cx)), length.into())
15581558
}
15591559
TyKind::Tup(tys) => Tuple(tys.iter().map(|ty| clean_ty(ty, cx)).collect()),
15601560
TyKind::OpaqueDef(item_id, _, _) => {
@@ -1626,7 +1626,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
16261626
ty::Array(ty, mut n) => {
16271627
n = n.eval(cx.tcx, ty::ParamEnv::reveal_all());
16281628
let n = print_const(cx, n);
1629-
Array(Box::new(clean_middle_ty(ty, cx, None)), n)
1629+
Array(Box::new(clean_middle_ty(ty, cx, None)), n.into())
16301630
}
16311631
ty::RawPtr(mt) => RawPointer(mt.mutbl, Box::new(clean_middle_ty(mt.ty, cx, None))),
16321632
ty::Ref(r, ty, mutbl) => BorrowedRef {

src/librustdoc/clean/types.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ pub(crate) enum Type {
16251625
/// An array type.
16261626
///
16271627
/// The `String` field is a stringified version of the array's length parameter.
1628-
Array(Box<Type>, String),
1628+
Array(Box<Type>, Box<str>),
16291629
/// A raw pointer type: `*const i32`, `*mut i32`
16301630
RawPointer(Mutability, Box<Type>),
16311631
/// A reference type: `&i32`, `&'a mut Foo`
@@ -2210,7 +2210,7 @@ impl Span {
22102210
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
22112211
pub(crate) struct Path {
22122212
pub(crate) res: Res,
2213-
pub(crate) segments: Vec<PathSegment>,
2213+
pub(crate) segments: ThinVec<PathSegment>,
22142214
}
22152215

22162216
impl Path {
@@ -2360,7 +2360,7 @@ pub(crate) enum ConstantKind {
23602360
///
23612361
/// Note that `ty::Const` includes generic parameters, and may not always be uniquely identified
23622362
/// by a DefId. So this field must be different from `Extern`.
2363-
TyConst { expr: String },
2363+
TyConst { expr: Box<str> },
23642364
/// A constant (expression) that's not an item or associated item. These are usually found
23652365
/// nested inside types (e.g., array lengths) or expressions (e.g., repeat counts), and also
23662366
/// used to define explicit discriminant values for enum variants.
@@ -2388,7 +2388,7 @@ impl Constant {
23882388
impl ConstantKind {
23892389
pub(crate) fn expr(&self, tcx: TyCtxt<'_>) -> String {
23902390
match *self {
2391-
ConstantKind::TyConst { ref expr } => expr.clone(),
2391+
ConstantKind::TyConst { ref expr } => expr.to_string(),
23922392
ConstantKind::Extern { def_id } => print_inlined_const(tcx, def_id),
23932393
ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
23942394
print_const_expr(tcx, body)
@@ -2574,13 +2574,13 @@ mod size_asserts {
25742574
// tidy-alphabetical-start
25752575
static_assert_size!(Crate, 72); // frequently moved by-value
25762576
static_assert_size!(DocFragment, 32);
2577-
static_assert_size!(GenericArg, 48);
2577+
static_assert_size!(GenericArg, 32);
25782578
static_assert_size!(GenericArgs, 32);
25792579
static_assert_size!(GenericParamDef, 56);
25802580
static_assert_size!(Generics, 16);
25812581
static_assert_size!(Item, 56);
2582-
static_assert_size!(ItemKind, 88);
2582+
static_assert_size!(ItemKind, 64);
25832583
static_assert_size!(PathSegment, 40);
2584-
static_assert_size!(Type, 48);
2584+
static_assert_size!(Type, 32);
25852585
// tidy-alphabetical-end
25862586
}

src/librustdoc/clean/utils.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
2121
use rustc_span::symbol::{kw, sym, Symbol};
2222
use std::fmt::Write as _;
2323
use std::mem;
24-
use thin_vec::ThinVec;
24+
use thin_vec::{thin_vec, ThinVec};
2525

2626
#[cfg(test)]
2727
mod tests;
@@ -136,7 +136,7 @@ pub(super) fn external_path<'tcx>(
136136
let name = cx.tcx.item_name(did);
137137
Path {
138138
res: Res::Def(def_kind, did),
139-
segments: vec![PathSegment {
139+
segments: thin_vec![PathSegment {
140140
name,
141141
args: external_generic_args(cx, did, has_self, bindings, substs),
142142
}],
@@ -242,19 +242,13 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
242242

243243
s
244244
}
245-
_ => {
246-
let mut s = n.to_string();
247-
// array lengths are obviously usize
248-
if s.ends_with("_usize") {
249-
let n = s.len() - "_usize".len();
250-
s.truncate(n);
251-
if s.ends_with(": ") {
252-
let n = s.len() - ": ".len();
253-
s.truncate(n);
254-
}
255-
}
256-
s
245+
// array lengths are obviously usize
246+
ty::ConstKind::Value(ty::ValTree::Leaf(scalar))
247+
if *n.ty().kind() == ty::Uint(ty::UintTy::Usize) =>
248+
{
249+
scalar.to_string()
257250
}
251+
_ => n.to_string(),
258252
}
259253
}
260254

src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl FromWithTcx<clean::Type> for Type {
485485
BareFunction(f) => Type::FunctionPointer(Box::new((*f).into_tcx(tcx))),
486486
Tuple(t) => Type::Tuple(t.into_tcx(tcx)),
487487
Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
488-
Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s },
488+
Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s.to_string() },
489489
ImplTrait(g) => Type::ImplTrait(g.into_tcx(tcx)),
490490
Infer => Type::Infer,
491491
RawPointer(mutability, type_) => Type::RawPointer {

0 commit comments

Comments
 (0)