git.delta.rocks / jrsonnet / refs/commits / 74ea504236fd

difftreelog

feat grow os stack on demand

Yaroslav Bolyukin2024-06-23parent: #914d5f4.patch.diff
in: master

3 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -486,6 +486,7 @@
  "pathdiff",
  "rustc-hash",
  "serde",
+ "stacker",
  "static_assertions",
  "strsim",
  "thiserror",
@@ -851,6 +852,15 @@
 ]
 
 [[package]]
+name = "psm"
+version = "0.1.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"
+dependencies = [
+ "cc",
+]
+
+[[package]]
 name = "quote"
 version = "1.0.36"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1077,6 +1087,19 @@
 checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
 
 [[package]]
+name = "stacker"
+version = "0.1.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c886bd4480155fd3ef527d45e9ac8dd7118a898a46530b7b94c3e21866259fce"
+dependencies = [
+ "cc",
+ "cfg-if",
+ "libc",
+ "psm",
+ "winapi",
+]
+
+[[package]]
 name = "static_assertions"
 version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1203,6 +1226,28 @@
 checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
 
 [[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
+[[package]]
 name = "windows-sys"
 version = "0.52.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
modifiedcrates/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"
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
25pub mod destructure;25pub mod destructure;
26pub mod operator;26pub mod operator;
27
28// This is the amount of bytes that need to be left on the stack before increasing the size.
29// It must be at least as large as the stack required by any code that does not call
30// `ensure_sufficient_stack`.
31const RED_ZONE: usize = 100 * 1024; // 100k
32
33// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
34// on. This flag has performance relevant characteristics. Don't set it too high.
35const STACK_PER_RECURSION: usize = 1024 * 1024; // 1MB
36
37/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations
38/// to "break up" recursive calls. E.g. almost any call to `visit_expr` or equivalent can benefit
39/// from this.
40///
41/// Should not be sprinkled around carelessly, as it causes a little bit of overhead.
42#[inline]
43pub fn ensure_sufficient_stack<R>(f: impl FnOnce() -> R) -> R {
44 stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f)
45}
2746
28pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {47pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {
29 fn is_trivial(expr: &LocExpr) -> bool {48 fn is_trivial(expr: &LocExpr) -> bool {
463 || format!("variable <{name}> access"),482 || format!("variable <{name}> access"),
464 || ctx.binding(name.clone())?.evaluate(),483 || ctx.binding(name.clone())?.evaluate(),
465 )?,484 )?,
466 Index { indexable, parts } => {485 Index { indexable, parts } => ensure_sufficient_stack(|| {
467 let mut parts = parts.iter();486 let mut parts = parts.iter();
468 let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {487 let mut indexable = if matches!(indexable.expr(), Expr::Literal(LiteralType::Super)) {
469 let part = parts.next().expect("at least part should exist");488 let part = parts.next().expect("at least part should exist");
574 (v, _) => bail!(CantIndexInto(v.value_type())),593 (v, _) => bail!(CantIndexInto(v.value_type())),
575 };594 };
576 }595 }
577 indexable596 Ok(indexable)
578 }597 })?,
579 LocalExpr(bindings, returned) => {598 LocalExpr(bindings, returned) => {
580 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =599 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =
581 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());600 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());
636 &evaluate(ctx.clone(), a)?,655 &evaluate(ctx.clone(), a)?,
637 &Val::Obj(evaluate_object(ctx, b)?),656 &Val::Obj(evaluate_object(ctx, b)?),
638 )?,657 )?,
639 Apply(value, args, tailstrict) => {658 Apply(value, args, tailstrict) => ensure_sufficient_stack(|| {
640 evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)?659 evaluate_apply(ctx, value, args, CallLocation::new(&loc), *tailstrict)
641 }660 })?,
642 Function(params, body) => {661 Function(params, body) => {
643 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())662 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())
644 }663 }