Replace rustc_data_structures::thin_vec::ThinVec with thin_vec::ThinVec.

`rustc_data_structures::thin_vec::ThinVec` looks like this:
```
pub struct ThinVec<T>(Option<Box<Vec<T>>>);
```
It's just a zero word if the vector is empty, but requires two
allocations if it is non-empty. So it's only usable in cases where the
vector is empty most of the time.

This commit removes it in favour of `thin_vec::ThinVec`, which is also
word-sized, but stores the length and capacity in the same allocation as
the elements. It's good in a wider variety of situation, e.g. in enum
variants where the vector is usually/always non-empty.

The commit also:
- Sorts some `Cargo.toml` dependency lists, to make additions easier.
- Sorts some `use` item lists, to make additions easier.
- Changes `clean_trait_ref_with_bindings` to take a
  `ThinVec<TypeBinding>` rather than a `&[TypeBinding]`, because this
  avoid some unnecessary allocations.
This commit is contained in:
Nicholas Nethercote 2022-08-17 14:22:30 +10:00
parent 223d16ebbd
commit b38106b6d8
42 changed files with 174 additions and 366 deletions

View file

@ -10,18 +10,19 @@ path = "lib.rs"
arrayvec = { version = "0.7", default-features = false }
askama = { version = "0.11", default-features = false, features = ["config"] }
atty = "0.2"
pulldown-cmark = { version = "0.9.2", default-features = false }
minifier = "0.2.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
smallvec = "1.8.1"
tempfile = "3"
itertools = "0.10.1"
minifier = "0.2.2"
once_cell = "1.10.0"
pulldown-cmark = { version = "0.9.2", default-features = false }
regex = "1"
rustdoc-json-types = { path = "../rustdoc-json-types" }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
smallvec = "1.8.1"
tempfile = "3"
thin-vec = "0.2.8"
tracing = "0.1"
tracing-tree = "0.2.0"
once_cell = "1.10.0"
[dependencies.tracing-subscriber]
version = "0.3.3"

View file

@ -123,7 +123,7 @@ where
kind: Box::new(ImplItem(Box::new(Impl {
unsafety: hir::Unsafety::Normal,
generics: new_generics,
trait_: Some(clean_trait_ref_with_bindings(self.cx, trait_ref, &[])),
trait_: Some(clean_trait_ref_with_bindings(self.cx, trait_ref, ThinVec::new())),
for_: clean_middle_ty(ty, self.cx, None),
items: Vec::new(),
polarity,

View file

@ -115,7 +115,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
),
// FIXME(eddyb) compute both `trait_` and `for_` from
// the post-inference `trait_ref`, as it's more accurate.
trait_: Some(clean_trait_ref_with_bindings(cx, trait_ref.0, &[])),
trait_: Some(clean_trait_ref_with_bindings(cx, trait_ref.0, ThinVec::new())),
for_: clean_middle_ty(ty.0, cx, None),
items: cx.tcx
.associated_items(impl_def_id)

View file

@ -3,9 +3,10 @@
use std::iter::once;
use std::sync::Arc;
use thin_vec::ThinVec;
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
@ -461,7 +462,7 @@ pub(crate) fn build_impl(
),
};
let polarity = tcx.impl_polarity(did);
let trait_ = associated_trait.map(|t| clean_trait_ref_with_bindings(cx, t, &[]));
let trait_ = associated_trait.map(|t| clean_trait_ref_with_bindings(cx, t, ThinVec::new()));
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
super::build_deref_target_impls(cx, &trait_items, ret);
}

View file

@ -33,7 +33,8 @@ use std::collections::hash_map::Entry;
use std::collections::BTreeMap;
use std::default::Default;
use std::hash::Hash;
use std::{mem, vec};
use std::mem;
use thin_vec::ThinVec;
use crate::core::{self, DocContext, ImplTraitParam};
use crate::formats::item_type::ItemType;
@ -125,7 +126,7 @@ fn clean_generic_bound<'tcx>(
bug!("clean: parenthesized `GenericBound::LangItemTrait`");
};
let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings);
let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, bindings);
GenericBound::TraitBound(
PolyTrait { trait_, generic_params: vec![] },
hir::TraitBoundModifier::None,
@ -147,14 +148,14 @@ fn clean_generic_bound<'tcx>(
pub(crate) fn clean_trait_ref_with_bindings<'tcx>(
cx: &mut DocContext<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
bindings: &[TypeBinding],
bindings: ThinVec<TypeBinding>,
) -> Path {
let kind = cx.tcx.def_kind(trait_ref.def_id).into();
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
span_bug!(cx.tcx.def_span(trait_ref.def_id), "`TraitRef` had unexpected kind {:?}", kind);
}
inline::record_extern_fqn(cx, trait_ref.def_id, kind);
let path = external_path(cx, trait_ref.def_id, true, bindings.to_vec(), trait_ref.substs);
let path = external_path(cx, trait_ref.def_id, true, bindings, trait_ref.substs);
debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs);
@ -164,7 +165,7 @@ pub(crate) fn clean_trait_ref_with_bindings<'tcx>(
fn clean_poly_trait_ref_with_bindings<'tcx>(
cx: &mut DocContext<'tcx>,
poly_trait_ref: ty::PolyTraitRef<'tcx>,
bindings: &[TypeBinding],
bindings: ThinVec<TypeBinding>,
) -> GenericBound {
let poly_trait_ref = poly_trait_ref.lift_to_tcx(cx.tcx).unwrap();
@ -327,7 +328,7 @@ fn clean_poly_trait_predicate<'tcx>(
let poly_trait_ref = pred.map_bound(|pred| pred.trait_ref);
Some(WherePredicate::BoundPredicate {
ty: clean_middle_ty(poly_trait_ref.skip_binder().self_ty(), cx, None),
bounds: vec![clean_poly_trait_ref_with_bindings(cx, poly_trait_ref, &[])],
bounds: vec![clean_poly_trait_ref_with_bindings(cx, poly_trait_ref, ThinVec::new())],
bound_params: Vec::new(),
})
}
@ -402,7 +403,7 @@ fn clean_projection<'tcx>(
def_id: Option<DefId>,
) -> Type {
let lifted = ty.lift_to_tcx(cx.tcx).unwrap();
let trait_ = clean_trait_ref_with_bindings(cx, lifted.trait_ref(cx.tcx), &[]);
let trait_ = clean_trait_ref_with_bindings(cx, lifted.trait_ref(cx.tcx), ThinVec::new());
let self_type = clean_middle_ty(ty.self_ty(), cx, None);
let self_def_id = if let Some(def_id) = def_id {
cx.tcx.opt_parent(def_id).or(Some(def_id))
@ -1588,12 +1589,12 @@ pub(crate) fn clean_middle_ty<'tcx>(
AdtKind::Enum => ItemType::Enum,
};
inline::record_extern_fqn(cx, did, kind);
let path = external_path(cx, did, false, vec![], substs);
let path = external_path(cx, did, false, ThinVec::new(), substs);
Type::Path { path }
}
ty::Foreign(did) => {
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
let path = external_path(cx, did, false, vec![], InternalSubsts::empty());
let path = external_path(cx, did, false, ThinVec::new(), InternalSubsts::empty());
Type::Path { path }
}
ty::Dynamic(obj, ref reg) => {
@ -1617,7 +1618,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
let mut bounds = dids
.map(|did| {
let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, did, false, vec![], empty);
let path = external_path(cx, did, false, ThinVec::new(), empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
PolyTrait { trait_: path, generic_params: Vec::new() }
})
@ -1693,7 +1694,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
}
}
let bindings: Vec<_> = bounds
let bindings: ThinVec<_> = bounds
.iter()
.filter_map(|bound| {
if let ty::PredicateKind::Projection(proj) = bound.kind().skip_binder()
@ -1714,7 +1715,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
})
.collect();
Some(clean_poly_trait_ref_with_bindings(cx, trait_ref, &bindings))
Some(clean_poly_trait_ref_with_bindings(cx, trait_ref, bindings))
})
.collect::<Vec<_>>();
bounds.extend(regions);
@ -1845,12 +1846,8 @@ fn clean_generic_args<'tcx>(
})
.collect::<Vec<_>>()
.into();
let bindings = generic_args
.bindings
.iter()
.map(|x| clean_type_binding(x, cx))
.collect::<Vec<_>>()
.into();
let bindings =
generic_args.bindings.iter().map(|x| clean_type_binding(x, cx)).collect::<ThinVec<_>>();
GenericArgs::AngleBracketed { args, bindings }
}
}

View file

@ -8,6 +8,7 @@ use std::sync::OnceLock as OnceCell;
use std::{cmp, fmt, iter};
use arrayvec::ArrayVec;
use thin_vec::ThinVec;
use rustc_ast::attr;
use rustc_ast::util::comments::beautify_doc_string;
@ -15,7 +16,6 @@ use rustc_ast::{self as ast, AttrStyle};
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel};
use rustc_const_eval::const_eval::is_unstable_const_fn;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@ -1303,7 +1303,7 @@ impl GenericBound {
pub(crate) fn maybe_sized(cx: &mut DocContext<'_>) -> GenericBound {
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, did, false, vec![], empty);
let path = external_path(cx, did, false, ThinVec::new(), empty);
inline::record_extern_fqn(cx, did, ItemType::Trait);
GenericBound::TraitBound(
PolyTrait { trait_: path, generic_params: Vec::new() },

View file

@ -12,7 +12,6 @@ use crate::visit_lib::LibEmbargoVisitor;
use rustc_ast as ast;
use rustc_ast::tokenstream::TokenTree;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
@ -23,6 +22,7 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
use rustc_span::symbol::{kw, sym, Symbol};
use std::fmt::Write as _;
use std::mem;
use thin_vec::ThinVec;
#[cfg(test)]
mod tests;
@ -102,7 +102,7 @@ fn external_generic_args<'tcx>(
cx: &mut DocContext<'tcx>,
did: DefId,
has_self: bool,
bindings: Vec<TypeBinding>,
bindings: ThinVec<TypeBinding>,
substs: SubstsRef<'tcx>,
) -> GenericArgs {
let args = substs_to_args(cx, substs, has_self);
@ -112,7 +112,7 @@ fn external_generic_args<'tcx>(
// The trait's first substitution is the one after self, if there is one.
match substs.iter().nth(if has_self { 1 } else { 0 }).unwrap().expect_ty().kind() {
ty::Tuple(tys) => tys.iter().map(|t| clean_middle_ty(t, cx, None)).collect::<Vec<_>>().into(),
_ => return GenericArgs::AngleBracketed { args: args.into(), bindings: bindings.into() },
_ => return GenericArgs::AngleBracketed { args: args.into(), bindings },
};
let output = None;
// FIXME(#20299) return type comes from a projection now
@ -130,7 +130,7 @@ pub(super) fn external_path<'tcx>(
cx: &mut DocContext<'tcx>,
did: DefId,
has_self: bool,
bindings: Vec<TypeBinding>,
bindings: ThinVec<TypeBinding>,
substs: SubstsRef<'tcx>,
) -> Path {
let def_kind = cx.tcx.def_kind(did);