mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 08:13:41 +00:00
Move ensure_sufficient_stack to data_structures
We anticipate this to have uses in all sorts of crates and keeping it in `rustc_data_structures` enables access to it from more locations without necessarily pulling in the large `librustc` crate.
This commit is contained in:
parent
968f442c7c
commit
a5c5365031
@ -3161,7 +3161,6 @@ checksum = "81dfcfbb0ddfd533abf8c076e3b49d1e5042d1962526a12ce2c66d514b24cca3"
|
||||
dependencies = [
|
||||
"rustc-ap-rustc_data_structures",
|
||||
"smallvec 1.0.0",
|
||||
"stacker",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -3706,6 +3705,7 @@ dependencies = [
|
||||
"serialize",
|
||||
"smallvec 1.0.0",
|
||||
"stable_deref_trait",
|
||||
"stacker",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
|
@ -3,11 +3,11 @@ use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericAr
|
||||
use rustc_ast::ast::*;
|
||||
use rustc_ast::attr;
|
||||
use rustc_ast::ptr::P as AstP;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_data_structures::thin_vec::ThinVec;
|
||||
use rustc_errors::struct_span_err;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_middle::limits::ensure_sufficient_stack;
|
||||
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
|
||||
use rustc_span::symbol::{sym, Symbol};
|
||||
|
||||
|
@ -2,9 +2,9 @@ use super::{ImplTraitContext, LoweringContext, ParamMode};
|
||||
|
||||
use rustc_ast::ast::*;
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_middle::limits::ensure_sufficient_stack;
|
||||
use rustc_span::{source_map::Spanned, Span};
|
||||
|
||||
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
@ -28,6 +28,7 @@ rustc_index = { path = "../librustc_index", package = "rustc_index" }
|
||||
bitflags = "1.2.1"
|
||||
measureme = "0.7.1"
|
||||
libc = "0.2"
|
||||
stacker = "0.1.6"
|
||||
|
||||
[dependencies.parking_lot]
|
||||
version = "0.10"
|
||||
|
@ -80,6 +80,7 @@ pub mod stable_set;
|
||||
#[macro_use]
|
||||
pub mod stable_hasher;
|
||||
pub mod sharded;
|
||||
pub mod stack;
|
||||
pub mod sync;
|
||||
pub mod thin_vec;
|
||||
pub mod tiny_list;
|
||||
|
17
src/librustc_data_structures/stack.rs
Normal file
17
src/librustc_data_structures/stack.rs
Normal file
@ -0,0 +1,17 @@
|
||||
// This is the amount of bytes that need to be left on the stack before increasing the size.
|
||||
// It must be at least as large as the stack required by any code that does not call
|
||||
// `ensure_sufficient_stack`.
|
||||
const RED_ZONE: usize = 100 * 1024; // 100k
|
||||
|
||||
// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
|
||||
// on. This flag has performance relevant characteristics. Don't set it too high.
|
||||
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
|
||||
|
||||
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
|
||||
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
|
||||
/// from this.
|
||||
///
|
||||
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
|
||||
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
|
||||
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
|
||||
}
|
@ -34,4 +34,3 @@ byteorder = { version = "1.3" }
|
||||
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
|
||||
measureme = "0.7.1"
|
||||
rustc_session = { path = "../librustc_session" }
|
||||
stacker = "0.1.6"
|
||||
|
@ -13,24 +13,6 @@ use rustc_span::symbol::{sym, Symbol};
|
||||
|
||||
use std::num::IntErrorKind;
|
||||
|
||||
// This is the amount of bytes that need to be left on the stack before increasing the size.
|
||||
// It must be at least as large as the stack required by any code that does not call
|
||||
// `ensure_sufficient_stack`.
|
||||
const RED_ZONE: usize = 100 * 1024; // 100k
|
||||
|
||||
// Ony the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
|
||||
// on. This flag has performance relevant characteristics. Don't set it too high.
|
||||
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
|
||||
|
||||
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
|
||||
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
|
||||
/// from this.
|
||||
///
|
||||
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
|
||||
pub fn ensure_sufficient_stack<R, F: FnOnce() -> R>(f: F) -> R {
|
||||
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
|
||||
}
|
||||
|
||||
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
|
||||
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
|
||||
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);
|
||||
|
@ -1,12 +1,12 @@
|
||||
pub use self::def_id_forest::DefIdForest;
|
||||
|
||||
use crate::middle::limits::ensure_sufficient_stack;
|
||||
use crate::ty;
|
||||
use crate::ty::context::TyCtxt;
|
||||
use crate::ty::TyKind::*;
|
||||
use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef};
|
||||
use crate::ty::{AdtKind, Visibility};
|
||||
use crate::ty::{DefId, SubstsRef};
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
|
||||
mod def_id_forest;
|
||||
|
||||
|
@ -69,7 +69,7 @@ impl QueryContext for TyCtxt<'tcx> {
|
||||
|
||||
// Use the `ImplicitCtxt` while we execute the query.
|
||||
tls::enter_context(&new_icx, |_| {
|
||||
crate::middle::limits::ensure_sufficient_stack(|| compute(*self))
|
||||
rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -369,7 +369,7 @@ fn collect_items_rec<'tcx>(
|
||||
recursion_depth_reset = Some(check_recursion_limit(tcx, instance, recursion_depths));
|
||||
check_type_length_limit(tcx, instance);
|
||||
|
||||
rustc::middle::limits::ensure_sufficient_stack(|| {
|
||||
rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
||||
collect_neighbours(tcx, instance, &mut neighbors);
|
||||
});
|
||||
}
|
||||
@ -1148,7 +1148,7 @@ fn collect_miri<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut Vec<Mon
|
||||
Some(GlobalAlloc::Memory(alloc)) => {
|
||||
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
|
||||
for &((), inner) in alloc.relocations().values() {
|
||||
rustc_middle::limits::ensure_sufficient_stack(|| {
|
||||
rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
||||
collect_miri(tcx, inner, output);
|
||||
});
|
||||
}
|
||||
|
@ -3,8 +3,8 @@
|
||||
use crate::build::scope::DropKind;
|
||||
use crate::build::{BlockAnd, BlockAndExtension, Builder};
|
||||
use crate::hair::*;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_hir as hir;
|
||||
use rustc_middle::limits::ensure_sufficient_stack;
|
||||
use rustc_middle::middle::region;
|
||||
use rustc_middle::mir::*;
|
||||
|
||||
|
@ -18,9 +18,9 @@ use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
||||
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
|
||||
use crate::traits::error_reporting::InferCtxtExt;
|
||||
use rustc_ast::ast::Ident;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_errors::ErrorReported;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::limits::ensure_sufficient_stack;
|
||||
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
|
||||
use rustc_middle::ty::subst::{InternalSubsts, Subst};
|
||||
use rustc_middle::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
|
||||
|
@ -7,8 +7,8 @@ use crate::infer::canonical::OriginalQueryValues;
|
||||
use crate::infer::{InferCtxt, InferOk};
|
||||
use crate::traits::error_reporting::InferCtxtExt;
|
||||
use crate::traits::{Obligation, ObligationCause, PredicateObligation, Reveal};
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_infer::traits::Normalized;
|
||||
use rustc_middle::limits::ensure_sufficient_stack;
|
||||
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
|
||||
use rustc_middle::ty::subst::Subst;
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
|
@ -37,12 +37,12 @@ use crate::traits::error_reporting::InferCtxtExt;
|
||||
use crate::traits::project::ProjectionCacheKeyExt;
|
||||
use rustc_ast::attr;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::lang_items;
|
||||
use rustc_index::bit_set::GrowableBitSet;
|
||||
use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
|
||||
use rustc_middle::limits::ensure_sufficient_stack;
|
||||
use rustc_middle::ty::fast_reject;
|
||||
use rustc_middle::ty::relate::TypeRelation;
|
||||
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
|
||||
|
@ -191,12 +191,12 @@ fn dtorck_constraint_for_ty<'tcx>(
|
||||
|
||||
ty::Array(ety, _) | ty::Slice(ety) => {
|
||||
// single-element containers, behave like their element
|
||||
rustc_middle::limits::ensure_sufficient_stack(|| {
|
||||
rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
||||
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ety, constraints)
|
||||
})?;
|
||||
}
|
||||
|
||||
ty::Tuple(tys) => rustc_middle::limits::ensure_sufficient_stack(|| {
|
||||
ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
||||
for ty in tys.iter() {
|
||||
dtorck_constraint_for_ty(
|
||||
tcx,
|
||||
@ -210,7 +210,7 @@ fn dtorck_constraint_for_ty<'tcx>(
|
||||
Ok::<_, NoSolution>(())
|
||||
})?,
|
||||
|
||||
ty::Closure(_, substs) => rustc_middle::limits::ensure_sufficient_stack(|| {
|
||||
ty::Closure(_, substs) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
|
||||
for ty in substs.as_closure().upvar_tys() {
|
||||
dtorck_constraint_for_ty(tcx, span, for_ty, depth + 1, ty, constraints)?;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user