Skip to content

Commit b351e0e

Browse files
Add max_stack_depth to --debug-stats output (#639)
## Summary - Tracks the high-water mark of the evaluation stack depth counter and reports it as `max_stack_depth` in `--debug-stats` output. - Useful for understanding how deep the call stack gets for a given Jsonnet program and tuning `--max-stack` accordingly. ## Test plan - [x] JVM tests pass (140/140) - [x] Verified output manually with `--debug-stats`
1 parent 8cb7cfe commit b351e0e

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

sjsonnet/src/sjsonnet/DebugStats.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ final class DebugStats {
1616
var functionCalls: Long = 0
1717
var builtinCalls: Long = 0
1818

19+
// -- Stack --
20+
var maxStackDepth: Int = 0
21+
1922
// -- Comprehensions --
2023
var arrayCompIterations: Long = 0
2124
var objectCompIterations: Long = 0
@@ -39,6 +42,7 @@ final class DebugStats {
3942
sb.append(formatLine("lazy_created", lazyCreated))
4043
sb.append(formatLine("function_calls", functionCalls))
4144
sb.append(formatLine("builtin_calls", builtinCalls))
45+
sb.append(formatLine("max_stack_depth", maxStackDepth))
4246
sb.append(formatLine("array_comp_iterations", arrayCompIterations))
4347
sb.append(formatLine("object_comp_iterations", objectCompIterations))
4448
sb.append(formatLine("files_parsed", filesParsed))

sjsonnet/src/sjsonnet/Evaluator.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Evaluator(
3333

3434
@inline private[sjsonnet] final def checkStackDepth(pos: Position): Unit = {
3535
stackDepth += 1
36+
if (debugStats != null && stackDepth > debugStats.maxStackDepth)
37+
debugStats.maxStackDepth = stackDepth
3638
if (stackDepth > maxStack)
3739
Error.fail("Max stack frames exceeded.", pos)
3840
}

0 commit comments

Comments
 (0)