Skip to main content

Module depth

Module depth 

Source
Expand description

Call stack depth: where upstream’s recursion is bounded only by V8’s stack, the port’s stops where node’s does.

Upstream (markdown-it’s block and inline rules, the tag grammar, the parser’s handleToken, walk(), the validator’s walkWithParents, Node.resolve and resolve, the transformer, the HTML renderer, String() of arrays, JSON.stringify) recurses until V8’s stack runs out, and then throws RangeError: Maximum call stack size exceeded. How deep that is depends on what nests: each level of a shape puts the same frames on the stack, and different shapes put different frames (a blockquote costs less than a list item, an array in a tag’s value less than a hash). This module models that stack as a budget per thread, charged at each recursion site the port shares with upstream:

  • STACK is node’s stack and every site’s cost is a share of it, chosen so that each shape on its own, called from the top of a script, stops where a fresh node 22 process throws (the shape’s “pure” limit; see per_level). Shapes that mix sites (a tag’s value inside nested blockquotes, a list inside partials, a value resolved deep in a document) then stop near node’s too, as V8’s frames add up the same way. Nested calls share the budget as they share node’s stack: a debug() deep in a document has less of it left than one at the top. The port’s own loops (the parser’s, the validator’s, resolve’s, the HTML renderer’s, String()’s and JSON.stringify’s walks) charge their path the same way without recursing.
  • Where the recursion mirrors upstream’s (the engine’s rules, the grammar, the transform, the values’ resolution, serde of the AST), grow! runs the next level on a new stack segment when the thread’s stack runs low (the stacker crate), so that the budget, not the thread’s stack, is the limit, whatever stack the caller runs on: a 2 MiB thread in a debug build reaches node’s limits as a release build on the main thread does.
  • Where the stack cannot grow (can_grow: the stack-growth feature off, a target without stack switching such as wasm, whose call stack is the host’s, or a thread whose stack bounds are unknown), each of those recursions keeps the fixed limit it had before the budget, which fits a 2 MiB stack in a debug build with a quarter spare: there those limits, below node’s, are the host’s limit. The loops keep node’s limits everywhere.

Node’s own limits move with how warm V8 is (optimized frames are smaller, and a call that does much work before it nests warms V8 up within the call) and with the stack below the call, so around each limit there is a band where a fresh process throws and a warmed-up one does not; the port’s limits are the fresh process’s (CONFORMANCE.md gives the table).

What it costs: a charge is a thread-local add and compare, and grow! reads the stack pointer and the thread’s stack limit; both run once per level of what nests (a blockquote, a value in a tag, a node transformed), never per character, and segments are allocated only past the thread’s own stack.

Constants§

STACK
Node’s stack in the budget’s units: V8’s default --stack-size (984 KB), in 2^-20ths of a byte (fine enough that n levels of per_level(n) fit and n + 1 do not for any n up to a million). Only the ratios of the costs to it matter.

Functions§

can_grow
Whether recursion grows the stack on this thread: the stack-growth feature is on, the target can switch stacks (not wasm) and the thread’s stack bounds are known (they are on Linux, macOS, Windows and the BSDs). Where it cannot, the recursions that mirror upstream’s stop at their fixed limits (each module’s MAX_* constants say which) instead of node’s.