Skip to content

prism: follow ruby/prism 30a25553 (post-order serialization); run deeply nested code - #27

Merged
Largo merged 3 commits into
masterfrom
claude/great-cori-er3i0g
Oct 27, 2026
Merged

Largo merged 3 commits into
masterfrom
claude/great-cori-er3i0g

Conversation

@Largo

@Largo Largo commented Oct 27, 2026

Copy link
Copy Markdown
Owner

Two commits.

prism: follow ruby/prism 30a25553 (post-order serialization)

prism's main changed its serialization layout after cd76b0d1, the revision CI and release.yml were pinned to. ruby/prism ab92f0eb4 writes the tree in post-order, with a lazy-subtree header in front of a DefNode's children. ruby/prism 01c1960e8 adds an 8-byte checksum to the header. A libprism built from main failed at startup with Unknown prism node type: 228.

  • generate.rb now emits a loader that builds the tree bottom-up on an explicit stack, as prism's Java loader does. No loading path recurses any more.
  • Lazy method bodies still work. A DefNode's body is skimmed for what the bridge needs at parse time and loaded from its byte range on first call. The skim rules are unchanged.
  • Src/Prism/Generated is regenerated from 30a25553 (prism 2.0.0).
  • The vendored Ruby half of prism in Src/StdLib/ruby/4.0/prism is re-synced. ironruby.rb follows ffi.rb: File.path, the "nearest" default version, and StringQuery.keyword? backed by a new P/Invoke.
  • The default gemspec becomes prism-2.0.0.
  • PRISM_REF in ci.yml and release.yml, Src/Prism/README.md and the README build steps all name 30a25553.

I checked the new loader against the cd76b0d1 build by dumping every field of every node, eager and lazy. Across 2081 files the trees are identical. The only difference is two warnings that prism now reports in a different order.

Run deeply nested code instead of overflowing the stack

Prism accepts nesting up to 10000 levels, and call chains (a.b.c…) have no limit. Every stage that turns prism's output into code recursed per level, so a few hundred levels killed the process with an uncatchable stack overflow. Each stage now checks the stack and continues on a fresh thread when it's nearly used up:

  • Prism's parser: it is recursive C, so every source over 128 bytes is parsed on one long-lived thread with a 64 MB stack.
  • IronRuby's side: the bridge, the AST transform (TransformRead is now a guarded wrapper over the abstract TransformReadCore), and IronRuby's method JIT, which leaves too-deep bodies interpreted.
  • The DLR: a new Build/dlr-patches/0101-stack-guard.patch guards the interpreter compiler and FlowControlRewriter.

This also fixes a separate bug found along the way. A scope with more than 128 locals threw a NullReferenceException with either parser, because the nested MutableTuple fields were never created.

New C# tests: ManyLocals1, ManyLocals2, and DeepNesting1, which runs on a 1 MB thread.

Testing

Run locally against a libprism built from 30a25553, compared with the previous build against cd76b0d1:

Check Result
CI smoke script, net8.0 and net10.0 passes
IronRuby.Tests default and /interpret 0 failures
IronRuby.Tests with IR_USE_PRISM=1 the same 88 failures as before
spec/language 2935 examples, the same single failure as before
mspec run, stdlib sweeps same as before

All 31 nesting shapes I tried run at the deepest level prism accepts.

Not tested locally: Windows, which this PR's CI covers.

Known limits:

  • Code whose calls nest thousands deep can still exhaust a small stack.
  • Forced IL compilation (-X:CompilationThreshold 0) of trees thousands of levels deep reaches IL and JIT limits.

Generated by Claude Code

prism's main has changed its serialization layout twice since cd76b0d1,
the revision CI and release.yml were pinned to:

- ab92f0eb4 "Serialize using post-order traversal": the subtrees of a
  node's node/node?/node[] fields now come first, in field order (an
  absent node? as a 0 byte, a node[] as its elements), followed by the
  node's own record, in which a node[] is just its count. A DefNode's
  children are preceded by a 255 marker and two raw uint32s, the byte
  length of the subtrees before its body and the length of the body.
- 01c1960e8 "Add checksum": the header has an 8-byte checksum of the
  source after the start line.

A libprism from main therefore failed at startup with "Unknown prism
node type: 228" while the loader read the old pre-order layout.

generate.rb now emits a LoadNodes loop that builds the tree bottom-up on
an explicit node stack, as prism's own Java loader does: each record
reads its own fields, pops its children off the top of the stack (last
child field first) and pushes itself. With lazy method bodies on, a
DefNode's marker records where its body starts; when the loader gets
there it skims the body and pushes a placeholder, which the DefNode's
record turns into DeferredBodyOffset/DeferredBodyLength/BodyContents.
LoadDeferredBody loads exactly that byte range later.

The skimmer reads the same layout linearly, with a stack of Contents*
bits per subtree instead of nodes, so the rules carry over unchanged:
a DefNode passes on its receiver's bits and only the eager bit of its
parameters and body, and a super whose block child is absent uses the
method's block. PrismLoader.Load skips the checksum. No loading path
recurses any more.

Checked against the cd76b0d1 build by dumping every field of every
node, eager and lazy (bodies forced, BodyContents included), for the
2081 .rb files in Src/StdLib, spec/language and mspec/lib: the output
is identical except that prism now reports two "assigned but unused
variable" warnings in a different order. Lazy equals eager on the new
build. The CI smoke script, the stdlib sweeps, IronRuby.Tests (0
failures; 88 with IR_USE_PRISM=1 on both builds, the same tests) and
spec/language (2935 examples, the same single failure) match the
cd76b0d1 baseline.

The rest of the revision comes along:

- Src/StdLib/ruby/4.0/prism is prism's lib/ at 30a25553 (ffi.rb,
  node.rb, parse_result.rb, serialize.rb, string_query.rb changed);
  prism.rb keeps IronRuby's backend branch.
- Src/StdLib/ironruby/prism/ironruby.rb follows ffi.rb: paths go
  through File.path, a missing version means "nearest", and
  StringQuery.keyword? is backed by a new pm_string_query_keyword
  P/Invoke.
- prism reports 2.0.0 now, so the default gemspec is regenerated as
  prism-2.0.0 (Util/gen-default-gemspecs.rb refuses one that disagrees
  with Prism::VERSION).
- PRISM_REF in ci.yml and release.yml, Src/Prism/README.md and the
  README build instructions name 30a25553.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GHevkQ9iLfbndjQHHcFQL6
prism accepts nesting up to 10000 levels, and a chain of calls
(a.b.c...) is a tree as deep as it is long with no limit at all. Every
stage that turns prism's output into code recursed once or more per
level, so a few hundred levels took the process down with an
uncatchable stack overflow: `x = 1 + (1 + (...))` 300 deep, blocks
nested 300 deep, a call chain of 50000. CRuby runs all of these.

Each recursive stage now checks the stack, and when it is close to
running out carries on on a fresh thread while the current one waits -
what System.Linq.Expressions' own compilers do:

- prism's parser is recursive C needing about half a KB of native stack
  per level (some 5 MB at prism's limit; a .NET thread has 1.5 MB, the
  Windows main thread 1 MB). Every source over 128 bytes is parsed on
  one long-lived thread with a 64 MB stack (PrismNative.WithParserStack;
  a thread per parse cost ~600 ms of startup).
- the bridge: Expr, Target (nested assignment targets and destructured
  parameters) and PatternTest.
- IronRuby's AST transformation: Expression.TransformRead becomes a
  guarded wrapper over the abstract TransformReadCore every expression
  implements (53 overrides renamed), plus TransformStatements,
  TransformStatementsToExpression, TransformCondition and
  CompoundLeftValue's nested target evaluation and writes.
- IronRuby's method JIT: a body too deep for the stack is screened out
  or bailed from, and stays interpreted.
- the DLR (Build/dlr-patches/0101-stack-guard.patch): LightCompiler's
  Compile/CompileAsVoid and FlowControlRewriter, through a new
  Microsoft.Scripting.Utils.StackGuard.

IronRuby.Runtime.StackGuard holds the helpers on this side.

Found on the way: a scope of more than 128 locals threw a
NullReferenceException as soon as one was stored. MutableTuple keeps
fields past 128 in nested tuples, and ScopeBuilder created the locals
tuple with a bare New that left them null - in any scope, with either
parser, and in prism's pattern matching and safe navigation, whose
temporaries are locals (a pattern 41 levels deep, a &. chain of 130).
ScopeBuilder now builds such a tuple with MutableTuple.Create.

Checked with 31 shapes at the deepest prism accepts (arrays 9996,
masgn 9995, unary 9995, patterns 3332, nested classes 4996, ...) and
call chains of 50000: all run on Linux's 8 MB main stack, and on a
1 MB one everything does except code whose calls nest as deeply at run
time (a block calling a block ... 2000 deep), which is Ruby call depth,
not nesting. Nesting past prism's limit is its "nesting too deep"
SyntaxError, as in CRuby. Forced IL compilation (-X:CompilationThreshold
0) of trees thousands of levels deep still reaches limits of IL and the
JIT (a 50000-call chain is an invalid program); the default adaptive
mode keeps such code interpreted.

New IronRuby.Tests: ManyLocals1 (300 locals in a top-level, method,
block and eval scope), ManyLocals2 (a &. chain of 200) and DeepNesting1,
which runs on a 1 MB thread at depths where every guarded stage
overflowed before. IronRuby.Tests (default, /interpret and
IR_USE_PRISM=1), spec/language, the mspec run, the stdlib sweeps and
the CI smoke script match the previous results apart from the new tests
passing; startup time is unchanged within noise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GHevkQ9iLfbndjQHHcFQL6
@coderabbitai

coderabbitai Bot commented Oct 27, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 36893a2c-4b6c-45be-a9bb-ad93aa63a39a
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Largo
Largo merged commit d8088c6 into master Oct 27, 2026
18 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants