Skip to content

Commit 105f73e

Browse files
Copilotdadhi
andauthored
Use constant flag instead of closure marker sentinel
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/e1226c7b-fa3d-4d90-8da1-40fc77f4ae55 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
1 parent 02fdc70 commit 105f73e

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/FastExpressionCompiler.LightExpression/FlatExpression.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ private void SetFlags(byte flags)
156156
/// <summary>Stores an expression tree as a flat node array plus out-of-line closure constants.</summary>
157157
public struct ExprTree
158158
{
159-
private static readonly object ClosureConstantMarker = new();
160159
private const byte ParameterByRefFlag = 1;
160+
private const byte ConstantInClosureFlag = 2;
161161
private const int UnboundParameterPosition = ushort.MaxValue;
162162
private const byte BinaryLiftedToNullFlag = 1;
163163
private const byte LoopHasBreakFlag = 1;
@@ -172,7 +172,7 @@ public struct ExprTree
172172
/// <summary>Gets or sets the flat node storage.</summary>
173173
public SmallList<ExprNode, Stack16<ExprNode>, NoArrayPool<ExprNode>> Nodes;
174174

175-
/// <summary>Gets or sets closure constants that are referenced from constant nodes.</summary>
175+
/// <summary>Gets or sets non-inlined constants referenced from constant nodes via <see cref="ExprNode.ChildIdx"/>.</summary>
176176
public SmallList<object, Stack16<object>, NoArrayPool<object>> ClosureConstants;
177177

178178
/// <summary>Adds a parameter node and returns its index.</summary>
@@ -203,7 +203,7 @@ public int Constant(object value, Type type)
203203
return AddRawExpressionNode(type, value, ExpressionType.Constant);
204204

205205
var constantIndex = ClosureConstants.Add(value);
206-
return AddRawExpressionNodeWithChildIndex(type, ClosureConstantMarker, ExpressionType.Constant, constantIndex);
206+
return AddRawExpressionNodeWithChildIndex(type, null, ExpressionType.Constant, ConstantInClosureFlag, constantIndex);
207207
}
208208

209209
/// <summary>Adds a null constant node.</summary>
@@ -696,8 +696,8 @@ private int AddRawExpressionNode(Type type, object obj, ExpressionType nodeType,
696696
private int AddRawLeafExpressionNode(Type type, object obj, ExpressionType nodeType, byte flags = 0, int childIdx = 0, int childCount = 0) =>
697697
AddLeafNode(type, obj, nodeType, ExprNodeKind.Expression, flags, childIdx, childCount);
698698

699-
private int AddRawExpressionNodeWithChildIndex(Type type, object obj, ExpressionType nodeType, int childIdx) =>
700-
AddRawLeafExpressionNode(type, obj, nodeType, childIdx: childIdx);
699+
private int AddRawExpressionNodeWithChildIndex(Type type, object obj, ExpressionType nodeType, byte flags, int childIdx) =>
700+
AddRawLeafExpressionNode(type, obj, nodeType, flags, childIdx, 0);
701701

702702
private int AddFactoryAuxNode(Type type, object obj, ExprNodeKind kind, byte flags, int child) =>
703703
AddNode(type, obj, ExpressionType.Extension, kind, flags, CloneChild(child));
@@ -1037,7 +1037,7 @@ private int AddConstant(System.Linq.Expressions.ConstantExpression constant)
10371037
return _tree.AddRawExpressionNode(constant.Type, constant.Value, constant.NodeType);
10381038

10391039
var constantIndex = _tree.ClosureConstants.Add(constant.Value);
1040-
return _tree.AddRawExpressionNodeWithChildIndex(constant.Type, ClosureConstantMarker, constant.NodeType, constantIndex);
1040+
return _tree.AddRawExpressionNodeWithChildIndex(constant.Type, null, constant.NodeType, ConstantInClosureFlag, constantIndex);
10411041
}
10421042

10431043
private int AddSwitchCase(SysSwitchCase switchCase)
@@ -1256,6 +1256,7 @@ private int AddNode(Type type, object obj, ExpressionType nodeType, ExprNodeKind
12561256

12571257
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12581258
private static bool ShouldInlineConstant(object value, Type type) =>
1259+
// Inlined constants are stored directly in ExprNode.Obj (boxed for value types).
12591260
value == null || value is string || value is Type || type.IsEnum || Type.GetTypeCode(type) != TypeCode.Object;
12601261

12611262
private static Type GetMemberType(System.Reflection.MemberInfo member) => member switch
@@ -1360,7 +1361,7 @@ public SysExpr ReadExpression(int index)
13601361
switch (node.NodeType)
13611362
{
13621363
case ExpressionType.Constant:
1363-
return SysExpr.Constant(ReferenceEquals(node.Obj, ClosureConstantMarker)
1364+
return SysExpr.Constant(node.HasFlag(ConstantInClosureFlag)
13641365
? _tree.ClosureConstants[node.ChildIdx]
13651366
: node.Obj, node.Type);
13661367
case ExpressionType.Default:

0 commit comments

Comments
 (0)