Skip to content

Rustdoc: reduce size of Clean types #92514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
@@ -3800,6 +3800,7 @@ dependencies = [
"stable_deref_trait",
"stacker",
"tempfile",
"thin-slice",
"tracing",
"winapi",
]
@@ -5247,6 +5248,12 @@ dependencies = [
"unicode-width",
]

[[package]]
name = "thin-slice"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaa81235c7058867fa8c0e7314f33dcce9c215f535d1913822a2b3f5e289f3c"

[[package]]
name = "thiserror"
version = "1.0.20"
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ measureme = "10.0.0"
libc = "0.2"
stacker = "0.1.14"
tempfile = "3.2"
thin-slice = "0.1.1"

[dependencies.parking_lot]
version = "0.11"
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
@@ -103,6 +103,7 @@ pub mod steal;
pub mod tagged_ptr;
pub mod temp_dir;
pub mod unhash;
pub mod thin_slice;

pub use ena::undo_log;
pub use ena::unify;
53 changes: 53 additions & 0 deletions compiler/rustc_data_structures/src/thin_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::ops::{Deref, DerefMut};
use thin_slice::ThinBoxedSlice;

#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct ThinSlice<T> {
slice: ThinBoxedSlice<T>,
}

impl<T> ThinSlice<T> {
pub fn into_vec(self) -> Vec<T> {
self.into()
}
}

impl<T> Default for ThinSlice<T> {
fn default() -> Self {
Self { slice: Default::default() }
}
}

impl<T> From<Vec<T>> for ThinSlice<T> {
fn from(vec: Vec<T>) -> Self {
Self { slice: vec.into_boxed_slice2().into() }
}
}

impl<T> From<ThinSlice<T>> for Vec<T> {
fn from(slice: ThinSlice<T>) -> Self {
let boxed: Box<[T]> = slice.slice.into();
boxed.into_vec()
}
}

impl<T> FromIterator<T> for ThinSlice<T> {
fn from_iter<I: IntoIterator<Item=T>>(iter: I) -> Self {
let vec: Vec<T> = iter.into_iter().collect();
vec.into()
}
}

impl<T> Deref for ThinSlice<T> {
type Target = [T];

fn deref(&self) -> &Self::Target {
self.slice.deref()
}
}

impl<T> DerefMut for ThinSlice<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.slice.deref_mut()
}
}
12 changes: 12 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
@@ -1002,6 +1002,18 @@ impl<T, A: Allocator> Vec<T, A> {
}
}

/// WIP
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_boxed_slice2(self) -> Box<[T], A> {
unsafe {
let me = ManuallyDrop::new(self);
let buf = ptr::read(&me.buf);
let len = me.len();
buf.into_box(len).assume_init()
}
}

/// Shortens the vector, keeping the first `len` elements and dropping
/// the rest.
///
23 changes: 12 additions & 11 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
@@ -122,7 +122,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
generics: new_generics,
trait_: Some(trait_ref.clean(self.cx)),
for_: ty.clean(self.cx),
items: Vec::new(),
items: Default::default(),
polarity,
kind: ImplKind::Auto,
}),
@@ -354,8 +354,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
if let Some(data) = ty_to_fn.get(&ty) {
let (poly_trait, output) =
(data.0.as_ref().unwrap().clone(), data.1.as_ref().cloned().map(Box::new));
let mut new_path = poly_trait.trait_.clone();
let last_segment = new_path.segments.pop().expect("segments were empty");
let new_path = poly_trait.trait_.clone();
let mut segments = new_path.segments.into_vec();
let last_segment = segments.pop().expect("segments were empty");

let (old_input, old_output) = match last_segment.args {
GenericArgs::AngleBracketed { args, .. } => {
@@ -377,10 +378,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {

let new_params = GenericArgs::Parenthesized { inputs: old_input, output };

new_path
.segments
segments
.push(PathSegment { name: last_segment.name, args: new_params });

let new_path = Path { segments: segments.into(), ..new_path };
bounds.insert(GenericBound::TraitBound(
PolyTrait { trait_: new_path, generic_params: poly_trait.generic_params },
hir::TraitBoundModifier::None,
@@ -396,15 +397,15 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
Some(WherePredicate::BoundPredicate {
ty,
bounds: bounds_vec,
bound_params: Vec::new(),
bound_params: Default::default(),
})
})
.chain(
lifetime_to_bounds.into_iter().filter(|&(_, ref bounds)| !bounds.is_empty()).map(
|(lifetime, bounds)| {
let mut bounds_vec = bounds.into_iter().collect();
self.sort_where_bounds(&mut bounds_vec);
WherePredicate::RegionPredicate { lifetime, bounds: bounds_vec }
WherePredicate::RegionPredicate { lifetime, bounds: bounds_vec.into() }
},
),
)
@@ -520,7 +521,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
GenericBound::TraitBound(ref mut p, _) => {
// Insert regions into the for_generics hash map first, to ensure
// that we don't end up with duplicate bounds (e.g., for<'b, 'b>)
for_generics.extend(p.generic_params.clone());
for_generics.extend(p.generic_params.clone().into_vec());
p.generic_params = for_generics.into_iter().collect();
self.is_fn_trait(&p.trait_)
}
@@ -542,7 +543,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
}
}
WherePredicate::RegionPredicate { lifetime, bounds } => {
lifetime_to_bounds.entry(lifetime).or_default().extend(bounds);
lifetime_to_bounds.entry(lifetime).or_default().extend(bounds.into_vec());
}
WherePredicate::EqPredicate { lhs, rhs } => {
match lhs {
@@ -586,7 +587,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
let bounds = ty_to_bounds.entry(*ty.clone()).or_default();

bounds.insert(GenericBound::TraitBound(
PolyTrait { trait_: new_trait, generic_params: Vec::new() },
PolyTrait { trait_: new_trait, generic_params: Default::default() },
hir::TraitBoundModifier::None,
));

@@ -595,7 +596,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
// duplicate bound like `T: Iterator + Iterator<Item=u8>`
// on the docs page.
bounds.remove(&GenericBound::TraitBound(
PolyTrait { trait_: trait_.clone(), generic_params: Vec::new() },
PolyTrait { trait_: trait_.clone(), generic_params: Default::default() },
hir::TraitBoundModifier::None,
));
// Avoid creating any new duplicate bounds later in the outer
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.associated_items(impl_def_id)
.in_definition_order()
.map(|x| x.clean(self.cx))
.collect::<Vec<_>>(),
.collect(),
polarity: ty::ImplPolarity::Positive,
kind: ImplKind::Blanket(box trait_ref.self_ty().clean(self.cx)),
}),
10 changes: 5 additions & 5 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
@@ -216,7 +216,7 @@ crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Tra
unsafety: cx.tcx.trait_def(did).unsafety,
generics,
items: trait_items,
bounds: supertrait_bounds,
bounds: supertrait_bounds.into(),
is_auto,
}
}
@@ -496,7 +496,7 @@ crate fn build_impl(
generics,
trait_,
for_,
items: trait_items,
items: trait_items.into(),
polarity,
kind: ImplKind::Normal,
}),
@@ -540,10 +540,10 @@ fn build_module(
segments: vec![clean::PathSegment {
name: prim_ty.as_sym(),
args: clean::GenericArgs::AngleBracketed {
args: Vec::new(),
args: Default::default(),
bindings: ThinVec::new(),
},
}],
}].into(),
},
did: None,
},
@@ -604,7 +604,7 @@ fn build_macro(
}
LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
kind: ext.macro_kind(),
helpers: ext.helper_attrs,
helpers: ext.helper_attrs.into(),
}),
}
}
27 changes: 14 additions & 13 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ use rustc_ast as ast;
use rustc_attr as attr;
use rustc_const_eval::const_eval::is_unstable_const_fn;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::thin_slice::ThinSlice;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -113,7 +114,7 @@ impl Clean<Option<GenericBound>> for hir::GenericBound<'_> {

let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings);
GenericBound::TraitBound(
PolyTrait { trait_, generic_params: vec![] },
PolyTrait { trait_, generic_params: Default::default() },
hir::TraitBoundModifier::None,
)
}
@@ -177,7 +178,7 @@ fn clean_poly_trait_ref_with_bindings(

let trait_ = clean_trait_ref_with_bindings(cx, poly_trait_ref.skip_binder(), bindings);
GenericBound::TraitBound(
PolyTrait { trait_, generic_params: late_bound_regions },
PolyTrait { trait_, generic_params: late_bound_regions.into() },
hir::TraitBoundModifier::None,
)
}
@@ -314,7 +315,7 @@ impl<'a> Clean<Option<WherePredicate>> for ty::PolyTraitPredicate<'a> {
Some(WherePredicate::BoundPredicate {
ty: poly_trait_ref.skip_binder().self_ty().clean(cx),
bounds: vec![poly_trait_ref.clean(cx)],
bound_params: Vec::new(),
bound_params: Default::default(),
})
}
}
@@ -331,7 +332,7 @@ impl<'tcx> Clean<Option<WherePredicate>>

Some(WherePredicate::RegionPredicate {
lifetime: a.clean(cx).expect("failed to clean lifetime"),
bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))],
bounds: vec![GenericBound::Outlives(b.clean(cx).expect("failed to clean bounds"))].into(),
})
}
}
@@ -347,7 +348,7 @@ impl<'tcx> Clean<Option<WherePredicate>> for ty::OutlivesPredicate<Ty<'tcx>, ty:
Some(WherePredicate::BoundPredicate {
ty: ty.clean(cx),
bounds: vec![GenericBound::Outlives(lt.clean(cx).expect("failed to clean lifetimes"))],
bound_params: Vec::new(),
bound_params: Default::default(),
})
}
}
@@ -691,7 +692,7 @@ fn clean_ty_generics(
where_predicates.push(WherePredicate::BoundPredicate {
ty: Type::Generic(tp.name),
bounds: vec![GenericBound::maybe_sized(cx)],
bound_params: Vec::new(),
bound_params: Default::default(),
})
}
}
@@ -750,7 +751,7 @@ fn clean_fn_or_proc_macro(
}
}
}
ProcMacroItem(ProcMacro { kind, helpers })
ProcMacroItem(ProcMacro { kind, helpers: helpers.into() })
}
None => {
let mut func = clean_function(cx, sig, generics, body_id);
@@ -1437,7 +1438,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let decl = clean_fn_decl_from_did_and_sig(cx, def_id, sig);
BareFunction(box BareFunctionDecl {
unsafety: sig.unsafety(),
generic_params: Vec::new(),
generic_params: Default::default(),
decl,
abi: sig.abi(),
})
@@ -1481,7 +1482,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, did, false, vec![], empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
let bound = PolyTrait { trait_: path, generic_params: Vec::new() };
let bound = PolyTrait { trait_: path, generic_params: Default::default() };
bounds.push(bound);
}

@@ -1494,7 +1495,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
}

let path = external_path(cx, did, false, bindings, substs);
bounds.insert(0, PolyTrait { trait_: path, generic_params: Vec::new() });
bounds.insert(0, PolyTrait { trait_: path, generic_params: Default::default() });

DynTrait(bounds, lifetime)
}
@@ -1663,8 +1664,8 @@ impl Clean<VariantStruct> for rustc_hir::VariantData<'_> {
}
}

impl Clean<Vec<Item>> for hir::VariantData<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Vec<Item> {
impl Clean<ThinSlice<Item>> for hir::VariantData<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> ThinSlice<Item> {
self.fields().iter().map(|x| x.clean(cx)).collect()
}
}
@@ -1876,7 +1877,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
generics: impl_.generics.clean(cx),
trait_,
for_,
items,
items: items.into(),
polarity: tcx.impl_polarity(def_id),
kind: ImplKind::Normal,
});
4 changes: 2 additions & 2 deletions src/librustdoc/clean/simplify.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
clean::Generic(s) => {
let (b, p) = params.entry(s).or_default();
b.extend(bounds);
p.extend(bound_params);
p.extend(bound_params.into_vec());
}
t => tybounds.push((t, (bounds, bound_params))),
},
@@ -76,7 +76,7 @@ crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
clauses.extend(params.into_iter().map(|(k, (bounds, params))| WP::BoundPredicate {
ty: clean::Generic(k),
bounds,
bound_params: params,
bound_params: params.into(),
}));
clauses.extend(tybounds.into_iter().map(|(ty, (bounds, bound_params))| WP::BoundPredicate {
ty,
Loading