Skip to content

Commit

Permalink
Move ensure_sufficient_stack to data_structures
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nagisa authored and mati865 committed May 2, 2020
1 parent 968f442 commit a5c5365
Show file tree
Hide file tree
Showing 16 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock
Expand Up @@ -3161,7 +3161,6 @@ checksum = "81dfcfbb0ddfd533abf8c076e3b49d1e5042d1962526a12ce2c66d514b24cca3"
dependencies = [
"rustc-ap-rustc_data_structures",
"smallvec 1.0.0",
"stacker",
]

[[package]]
Expand Down Expand Up @@ -3706,6 +3705,7 @@ dependencies = [
"serialize",
"smallvec 1.0.0",
"stable_deref_trait",
"stacker",
"winapi 0.3.8",
]

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/expr.rs
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/pat.rs
Expand Up @@ -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> {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/Cargo.toml
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/librustc_data_structures/stack.rs
@@ -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)
}
1 change: 0 additions & 1 deletion src/librustc_middle/Cargo.toml
Expand Up @@ -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"
18 changes: 0 additions & 18 deletions src/librustc_middle/middle/limits.rs
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/inhabitedness/mod.rs
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/query/plumbing.rs
Expand Up @@ -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))
})
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/monomorphize/collector.rs
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/build/expr/as_temp.rs
Expand Up @@ -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::*;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trait_selection/traits/project.rs
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trait_selection/traits/query/normalize.rs
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trait_selection/traits/select.rs
Expand Up @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_traits/dropck_outlives.rs
Expand Up @@ -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,
Expand All @@ -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)?;
}
Expand Down

0 comments on commit a5c5365

Please sign in to comment.