Repository navigation
prism: follow ruby/prism 30a25553 (post-order serialization); run deeply nested code - #27
Merged
Merged
Conversation
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
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two commits.
prism: follow ruby/prism 30a25553 (post-order serialization)
prism's main changed its serialization layout after cd76b0d1, the revision CI and
release.ymlwere pinned to. ruby/prismab92f0eb4writes the tree in post-order, with a lazy-subtree header in front of aDefNode's children. ruby/prism01c1960e8adds an 8-byte checksum to the header. A libprism built from main failed at startup withUnknown prism node type: 228.generate.rbnow 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.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/Generatedis regenerated from 30a25553 (prism 2.0.0).Src/StdLib/ruby/4.0/prismis re-synced.ironruby.rbfollowsffi.rb:File.path, the "nearest" default version, andStringQuery.keyword?backed by a new P/Invoke.prism-2.0.0.PRISM_REFinci.ymlandrelease.yml,Src/Prism/README.mdand 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:TransformReadis now a guarded wrapper over the abstractTransformReadCore), and IronRuby's method JIT, which leaves too-deep bodies interpreted.Build/dlr-patches/0101-stack-guard.patchguards the interpreter compiler andFlowControlRewriter.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
MutableTuplefields were never created.New C# tests:
ManyLocals1,ManyLocals2, andDeepNesting1, which runs on a 1 MB thread.Testing
Run locally against a libprism built from 30a25553, compared with the previous build against cd76b0d1:
IronRuby.Testsdefault and/interpretIronRuby.TestswithIR_USE_PRISM=1spec/languageAll 31 nesting shapes I tried run at the deepest level prism accepts.
Not tested locally: Windows, which this PR's CI covers.
Known limits:
-X:CompilationThreshold 0) of trees thousands of levels deep reaches IL and JIT limits.Generated by Claude Code