difftreelog
feat grow os stack on demand
in: master
3 files changed
Cargo.lockdiffbeforeafterboth486 "pathdiff",486 "pathdiff",487 "rustc-hash",487 "rustc-hash",488 "serde",488 "serde",489 "stacker",489 "static_assertions",490 "static_assertions",490 "strsim",491 "strsim",491 "thiserror",492 "thiserror",850 "unicode-ident",851 "unicode-ident",851]852]853854[[package]]855name = "psm"856version = "0.1.21"857source = "registry+https://github.com/rust-lang/crates.io-index"858checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"859dependencies = [860 "cc",861]852862853[[package]]863[[package]]854name = "quote"864name = "quote"1076source = "registry+https://github.com/rust-lang/crates.io-index"1086source = "registry+https://github.com/rust-lang/crates.io-index"1077checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"1087checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"10881089[[package]]1090name = "stacker"1091version = "0.1.15"1092source = "registry+https://github.com/rust-lang/crates.io-index"1093checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce"1094dependencies = [1095 "cc",1096 "cfg-if",1097 "libc",1098 "psm",1099 "winapi",1100]107811011079[[package]]1102[[package]]1080name = "static_assertions"1103name = "static_assertions"1202source = "registry+https://github.com/rust-lang/crates.io-index"1225source = "registry+https://github.com/rust-lang/crates.io-index"1203checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"1226checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"12271228[[package]]1229name = "winapi"1230version = "0.3.9"1231source = "registry+https://github.com/rust-lang/crates.io-index"1232checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"1233dependencies = [1234 "winapi-i686-pc-windows-gnu",1235 "winapi-x86_64-pc-windows-gnu",1236]12371238[[package]]1239name = "winapi-i686-pc-windows-gnu"1240version = "0.4.0"1241source = "registry+https://github.com/rust-lang/crates.io-index"1242checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"12431244[[package]]1245name = "winapi-x86_64-pc-windows-gnu"1246version = "0.4.0"1247source = "registry+https://github.com/rust-lang/crates.io-index"1248checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"120412491205[[package]]1250[[package]]1206name = "windows-sys"1251name = "windows-sys"crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -60,3 +60,4 @@
# Bigint
num-bigint = { workspace = true, features = ["serde"], optional = true }
derivative.workspace = true
+stacker = "0.1.15"
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -25,6 +25,25 @@
pub mod destructure;
pub mod operator;
+// 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 = 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.
+#[inline]
+pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
+ stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
+}
+
pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {
fn is_trivial(expr: &LocExpr) -> bool {
match expr.expr() {
@@ -463,7 +482,7 @@
|| format!("variable <{name}> access"),
|| ctx.binding(name.clone())?.evaluate(),
)?,
- Index { indexable, parts } => {
+ Index { indexable, parts } => ensure_sufficient_stack(|| {
let mut parts = parts.iter();
let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {
let part = parts.next().expect("at least part should exist");
@@ -574,8 +593,8 @@
(v, _) => bail!(CantIndexInto(v.value_type())),
};
}
- indexable
- }
+ Ok(indexable)
+ })?,
LocalExpr(bindings, returned) => {
let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =
GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());
@@ -636,9 +655,9 @@
&evaluate(ctx.clone(), a)?,
&Val::Obj(evaluate_object(ctx, b)?),
)?,
- Apply(value, args, tailstrict) => {
- evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)?
- }
+ Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {
+ evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)
+ })?,
Function(params, body) => {
evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
}