Skip to content

Commit b678694

Browse files
Copilotdadhi
andauthored
refactor: use short indexes for flat lambda closure usage entries
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/775f1746-e6c9-4aef-8b08-84f5734431e5 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
1 parent 84157d2 commit b678694

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/FastExpressionCompiler.LightExpression/FlatExpression.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ public enum ExprNodeKind : byte
4646
}
4747

4848
/// <summary>Maps a lambda node to a parameter identity used from an outer scope and therefore captured in closure.</summary>
49+
[StructLayout(LayoutKind.Sequential, Pack = 2)]
4950
public readonly struct LambdaClosureParameterUsage
5051
{
5152
/// <summary>The lambda node index containing the parameter usage.</summary>
52-
public readonly int LambdaNodeIndex;
53+
public readonly short LambdaNodeIndex;
5354

5455
/// <summary>The parameter identity id (<see cref="ExprNode.ChildIdx"/>) referenced from outer scope.</summary>
55-
public readonly int ParameterId;
56+
public readonly short ParameterId;
5657

57-
public LambdaClosureParameterUsage(int lambdaNodeIndex, int parameterId)
58+
public LambdaClosureParameterUsage(short lambdaNodeIndex, short parameterId)
5859
{
5960
LambdaNodeIndex = lambdaNodeIndex;
6061
ParameterId = parameterId;
@@ -1170,8 +1171,9 @@ private void CollectLambdaClosureParameterUsages(System.Linq.Expressions.LambdaE
11701171

11711172
var captured = collector.CapturedParameters;
11721173
for (var i = 0; i < captured.Count; ++i)
1173-
_tree.LambdaClosureParameterUsages.Add(new LambdaClosureParameterUsage(lambdaNodeIndex,
1174-
GetId(ref _parameterIds, captured[i])));
1174+
_tree.LambdaClosureParameterUsages.Add(new LambdaClosureParameterUsage(
1175+
checked((short)lambdaNodeIndex),
1176+
checked((short)GetId(ref _parameterIds, captured[i]))));
11751177
}
11761178

11771179
private sealed class LambdaClosureUsageCollector : System.Linq.Expressions.ExpressionVisitor

test/FastExpressionCompiler.LightExpression.UnitTests/LightExpressionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ public void Flat_lambda_closure_parameter_usages_excludes_nested_lambda_locals()
972972
Asserts.AreEqual(pId, usage.ParameterId);
973973
}
974974

975-
private static int GetSingleNestedLambdaIndex(ref ExprTree fe)
975+
private static short GetSingleNestedLambdaIndex(ref ExprTree fe)
976976
{
977977
var nestedLambdaIndex = -1;
978978
for (var i = 0; i < fe.LambdaNodes.Count; ++i)
@@ -984,16 +984,16 @@ private static int GetSingleNestedLambdaIndex(ref ExprTree fe)
984984
throw new InvalidOperationException("Expected a single nested lambda.");
985985
nestedLambdaIndex = lambdaIndex;
986986
}
987-
return nestedLambdaIndex;
987+
return checked((short)nestedLambdaIndex);
988988
}
989989

990-
private static int GetParameterIdByName(ref ExprTree fe, string name)
990+
private static short GetParameterIdByName(ref ExprTree fe, string name)
991991
{
992992
for (var i = 0; i < fe.Nodes.Count; ++i)
993993
{
994994
ref var node = ref fe.Nodes[i];
995995
if (node.NodeType == ExpressionType.Parameter && string.Equals((string)node.Obj, name, StringComparison.Ordinal))
996-
return node.ChildIdx;
996+
return checked((short)node.ChildIdx);
997997
}
998998
throw new InvalidOperationException($"Parameter node '{name}' was not found.");
999999
}

0 commit comments

Comments
 (0)