Skip to content

FlatExpression: track lambda, block-with-vars, goto/label, and try-catch node indices during construction#543

Merged
dadhi merged 3 commits intomasterfrom
copilot/explore-trycollectinfo-phase-improvements
Apr 30, 2026
Merged

FlatExpression: track lambda, block-with-vars, goto/label, and try-catch node indices during construction#543
dadhi merged 3 commits intomasterfrom
copilot/explore-trycollectinfo-phase-improvements

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

TryCollectInfo traverses the entire expression tree to discover nested lambdas, block-scoped variables, goto/label pairs, and try regions. For FlatExpression (FE), all of this work can be done during construction — the same way ClosureConstants is already pre-collected — eliminating the O(tree_size) scan.

Changes

  • ExprTree.LambdaNodes — tracks indices of every lambda node; the root is RootIndex, all others are nested lambdas
  • ExprTree.BlocksWithVariables — tracks indices of block nodes that carry explicit variable declarations (children.Count == 2), covering TryCollectInfo's PushBlockWithVars/PopBlock tracking
  • ExprTree.GotoNodes — tracks indices of ExpressionType.Goto nodes (goto/return/break/continue family), covering the goto-count side of TargetToGotosAndLabels
  • ExprTree.LabelNodes — tracks indices of ExpressionType.Label expression nodes, covering the label-count side of TargetToGotosAndLabels
  • ExprTree.TryCatchNodes — tracks indices of ExpressionType.Try nodes (try/catch, try/finally, try/fault), covering the HasComplexExpression = true marking

All five lists are populated in both the direct factory-API path and Builder.AddExpression (the FromExpression path), so the same metadata is available regardless of how the ExprTree was built.

Usage

var fe = default(ExprTree);
var x = fe.ParameterOf<int>("x");
var inner = fe.Lambda<Func<int>>(x);
fe.RootIndex = fe.Lambda<Func<int, Func<int>>>(inner, x);

// fe.LambdaNodes.Count == 2  (root + nested)
// nested lambdas = all LambdaNodes entries where index != fe.RootIndex

// fe.BlocksWithVariables — blocks that need PushBlockWithVars treatment
// fe.GotoNodes / fe.LabelNodes — for goto↔label linking without traversal
// fe.TryCatchNodes — try regions that force HasComplexExpression = true

Callers can now enumerate all of these node categories in O(node_count) without a full tree walk. This is the groundwork for eliminating the separate TryCollectInfo phase for FE-built expressions; subsequent work can leverage these lists to pre-compute non-passed parameters, block-local variables, and label linkage at construction time.

Copilot AI changed the title [WIP] Investigate moving work from TryCollectInfo to FE construction FlatExpression: track lambda node indices during construction to avoid TryCollectInfo traversal Apr 29, 2026
Copilot AI requested a review from dadhi April 29, 2026 14:00
@dadhi
Copy link
Copy Markdown
Owner

dadhi commented Apr 29, 2026

@copilot what about other things, blocks, try catch, goto/labels

… and TryCatchNodes during construction

Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/fbbd4fe6-1e27-488e-b48b-b5ff5dbc8626

Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 29, 2026

@copilot what about other things, blocks, try catch, goto/labels

Done in commit 60a4f93. Added three more pre-collected index lists to ExprTree, mirroring the LambdaNodes pattern:

  • BlocksWithVariables — block node indices that carry explicit variable declarations (children.Count == 2), covering TryCollectInfo's PushBlockWithVars/PopBlock tracking
  • GotoNodesExpressionType.Goto node indices (goto/return/break/continue family), covering the TargetToGotosAndLabels goto-count side
  • LabelNodesExpressionType.Label expression node indices, covering the label-count side
  • TryCatchNodesExpressionType.Try node indices (try/catch, try/finally, try/fault), covering the HasComplexExpression = true marking

All four are populated in both the direct factory-API path and Builder.AddExpression (FromExpression), with 6 new tests added.

Copilot AI changed the title FlatExpression: track lambda node indices during construction to avoid TryCollectInfo traversal FlatExpression: track lambda, block-with-vars, goto/label, and try-catch node indices during construction Apr 29, 2026
Copy link
Copy Markdown
Owner

@dadhi dadhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now we are just creating indexes so we can collecting the indexes for imporant node types.

@dadhi dadhi marked this pull request as ready for review April 30, 2026 05:53
@dadhi dadhi merged commit 2e22c94 into master Apr 30, 2026
2 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.

FlatExpression: Explore the ways to move the work from TryCollectInfo phase into the by-construction work for FE

2 participants