FlatExpression POC: data-oriented flat expression tree#511
Draft
FlatExpression POC: data-oriented flat expression tree#511
Conversation
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/ac75b2c5-61a5-4e7b-b468-5bfff97971ac Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add data-oriented expression representation with optimization
FlatExpression POC: data-oriented flat expression tree
Apr 12, 2026
dadhi
requested changes
Apr 12, 2026
Owner
dadhi
left a comment
There was a problem hiding this comment.
Too many comments. Keep only important stuff explaining why/how abd remove trivial what.
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/cb3f142a-4345-45e1-af61-2f4161da4ed9 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
Contributor
Author
Done in d565578. Removed all comments that just describe what the code does. Kept only the three non-obvious why/how notes: the Lambda params-in-Info design decision, the |
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.
Explores the idea from #512: represent an expression tree as a single flat array of fat structs with integer index references instead of object-graph pointers — enabling stack allocation for small trees, trivial serialization, and O(1) structural equality.
Core types (
src/FastExpressionCompiler/FlatExpression.cs)Idx— 1-basedintindex intoNodes;default(It == 0) is the nil sentinelExpressionNode— sequential fat struct:NodeType,Type,Info,ConstantIndex,NextIdx(next sibling),ChildIdx(first child),ExtraIdx(second child slot)ExpressionTree— holds nodes inSmallList<ExpressionNode, Stack16<…>, NoArrayPool<…>>(first 16 nodes on the call-stack) and closure constants inSmallList<object, Stack4<…>, …>; factory methods forConstant,Parameter,Unary,Binary,New,Call,Lambda,Conditional,BlockToSystemExpression()— converts toSystem.Linq.Expressionsso existing FEC compilation path is reachableStructurallyEqual()— O(n) structural comparison via a single pass over the flat arrays; no traversal neededKey design insight surfaced
Lambda parameters cannot be chained via
NextIdx— the same parameter node may already have itsNextIdxoccupied as part of aNew/Callargument chain. Lambda stores its parameters asIdx[]inInfoinstead. This is the central intrusive-list tension: one smallIdx[]allocation per lambda avoids silent list corruption at construction time. A future optimisation could replace it with a (start, count) slice into a dedicated side array.Wins
Stack16)Gaps / obstacles
System.Linq.Expressionswithout the adapterInfofield boxesMethodBase/string— one allocation per call/new/parameter nodeTests (
test/FastExpressionCompiler.UnitTests/FlatExpressionTests.cs)22 tests covering node construction, parameter identity, structural equality,
ToSystemExpressionconversion, round-trip compile+invoke, and the mutable-closure-constant use case. Guarded with#if !LIGHT_EXPRESSIONsinceFlatExpression.csis not included in the LightExpression variant.