Skip to content

Commit a5c5365

Browse files
nagisamati865
authored andcommitted
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.
1 parent 968f442 commit a5c5365

File tree

16 files changed

+33
-33
lines changed

16 files changed

+33
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3161,7 +3161,6 @@ checksum = "81dfcfbb0ddfd533abf8c076e3b49d1e5042d1962526a12ce2c66d514b24cca3"
31613161
dependencies = [
31623162
"rustc-ap-rustc_data_structures",
31633163
"smallvec 1.0.0",
3164-
"stacker",
31653164
]
31663165

31673166
[[package]]
@@ -3706,6 +3705,7 @@ dependencies = [
37063705
"serialize",
37073706
"smallvec 1.0.0",
37083707
"stable_deref_trait",
3708+
"stacker",
37093709
"winapi 0.3.8",
37103710
]
37113711

src/librustc_ast_lowering/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericAr
33
use rustc_ast::ast::*;
44
use rustc_ast::attr;
55
use rustc_ast::ptr::P as AstP;
6+
use rustc_data_structures::stack::ensure_sufficient_stack;
67
use rustc_data_structures::thin_vec::ThinVec;
78
use rustc_errors::struct_span_err;
89
use rustc_hir as hir;
910
use rustc_hir::def::Res;
10-
use rustc_middle::limits::ensure_sufficient_stack;
1111
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1212
use rustc_span::symbol::{sym, Symbol};
1313

src/librustc_ast_lowering/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use super::{ImplTraitContext, LoweringContext, ParamMode};
22

33
use rustc_ast::ast::*;
44
use rustc_ast::ptr::P;
5+
use rustc_data_structures::stack::ensure_sufficient_stack;
56
use rustc_hir as hir;
67
use rustc_hir::def::Res;
7-
use rustc_middle::limits::ensure_sufficient_stack;
88
use rustc_span::{source_map::Spanned, Span};
99

1010
impl<'a, 'hir> LoweringContext<'a, 'hir> {

src/librustc_data_structures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rustc_index = { path = "../librustc_index", package = "rustc_index" }
2828
bitflags = "1.2.1"
2929
measureme = "0.7.1"
3030
libc = "0.2"
31+
stacker = "0.1.6"
3132

3233
[dependencies.parking_lot]
3334
version = "0.10"

src/librustc_data_structures/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub mod stable_set;
8080
#[macro_use]
8181
pub mod stable_hasher;
8282
pub mod sharded;
83+
pub mod stack;
8384
pub mod sync;
8485
pub mod thin_vec;
8586
pub mod tiny_list;

src/librustc_data_structures/stack.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This is the amount of bytes that need to be left on the stack before increasing the size.
2+
// It must be at least as large as the stack required by any code that does not call
3+
// `ensure_sufficient_stack`.
4+
const RED_ZONE: usize = 100 * 1024; // 100k
5+
6+
// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
7+
// on. This flag has performance relevant characteristics. Don't set it too high.
8+
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
9+
10+
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
11+
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
12+
/// from this.
13+
///
14+
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
15+
pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
16+
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
17+
}

src/librustc_middle/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@ byteorder = { version = "1.3" }
3434
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
3535
measureme = "0.7.1"
3636
rustc_session = { path = "../librustc_session" }
37-
stacker = "0.1.6"

src/librustc_middle/middle/limits.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@ use rustc_span::symbol::{sym, Symbol};
1313

1414
use std::num::IntErrorKind;
1515

16-
// This is the amount of bytes that need to be left on the stack before increasing the size.
17-
// It must be at least as large as the stack required by any code that does not call
18-
// `ensure_sufficient_stack`.
19-
const RED_ZONE: usize = 100 * 1024; // 100k
20-
21-
// Ony the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
22-
// on. This flag has performance relevant characteristics. Don't set it too high.
23-
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
24-
25-
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
26-
/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
27-
/// from this.
28-
///
29-
/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
30-
pub fn ensure_sufficient_stack<R, F: FnOnce() -> R>(f: F) -> R {
31-
stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
32-
}
33-
3416
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
3517
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
3618
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);

src/librustc_middle/ty/inhabitedness/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
pub use self::def_id_forest::DefIdForest;
22

3-
use crate::middle::limits::ensure_sufficient_stack;
43
use crate::ty;
54
use crate::ty::context::TyCtxt;
65
use crate::ty::TyKind::*;
76
use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef};
87
use crate::ty::{AdtKind, Visibility};
98
use crate::ty::{DefId, SubstsRef};
9+
use rustc_data_structures::stack::ensure_sufficient_stack;
1010

1111
mod def_id_forest;
1212

src/librustc_middle/ty/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl QueryContext for TyCtxt<'tcx> {
6969

7070
// Use the `ImplicitCtxt` while we execute the query.
7171
tls::enter_context(&new_icx, |_| {
72-
crate::middle::limits::ensure_sufficient_stack(|| compute(*self))
72+
rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
7373
})
7474
})
7575
}

0 commit comments

Comments
 (0)