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
486 "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]
853
854[[package]]
855name = "psm"
856version = "0.1.21"
857source = "registry+https://github.com/rust-lang/crates.io-index"
858checksum = "5787f7cda34e3033a72192c018bc5883100330f362ef279a8cbccfce8bb4e874"
859dependencies = [
860 "cc",
861]
852862
853[[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"
1088
1089[[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]
10781101
1079[[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"
1227
1228[[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]
1237
1238[[package]]
1239name = "winapi-i686-pc-windows-gnu"
1240version = "0.4.0"
1241source = "registry+https://github.com/rust-lang/crates.io-index"
1242checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
1243
1244[[package]]
1245name = "winapi-x86_64-pc-windows-gnu"
1246version = "0.4.0"
1247source = "registry+https://github.com/rust-lang/crates.io-index"
1248checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
12041249
1205[[package]]1250[[package]]
1206name = "windows-sys"1251name = "windows-sys"
modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
60# Bigint60# Bigint
61num-bigint = { workspace = true, features = ["serde"], optional = true }61num-bigint = { workspace = true, features = ["serde"], optional = true }
62derivative.workspace = true62derivative.workspace = true
63stacker = "0.1.15"
6364
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 }