Skip to content

Commit 83123e9

Browse files
Copilotdadhi
andauthored
Refine FE owner metadata lookups
Agent-Logs-Url: https://github.com/dadhi/FastExpressionCompiler/sessions/be12d6ed-b5f1-46be-828a-a7673aba6a54 Co-authored-by: dadhi <39516+dadhi@users.noreply.github.com>
1 parent e1c528b commit 83123e9

2 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/FastExpressionCompiler.LightExpression/FlatExpression.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,23 @@ public struct ExprTree
217217
/// <summary>Gets or sets owner-side metadata for block nodes, populated during FE construction.</summary>
218218
public SmallList<BlockInfo, Stack16<BlockInfo>, NoArrayPool<BlockInfo>> Blocks;
219219

220+
private SmallMap16<int, int, IntEq> _lambdaInfoIndexesByNodeIndex;
221+
private SmallMap16<int, int, IntEq> _blockInfoIndexesByNodeIndex;
222+
220223
/// <summary>Returns the owner-side metadata for the specified lambda node, if present.</summary>
221224
public bool TryGetLambdaInfo(int lambdaIndex, out LambdaInfo info)
222225
{
223-
for (var i = 0; i < Lambdas.Count; ++i)
224-
{
225-
info = Lambdas.GetSurePresentRef(i);
226-
if (info.LambdaIndex == lambdaIndex)
227-
return true;
228-
}
229-
230-
info = default;
231-
return false;
226+
ref var infoIndex = ref _lambdaInfoIndexesByNodeIndex.Map.TryGetValueRef(lambdaIndex, out var found);
227+
info = found ? Lambdas.GetSurePresentRef(infoIndex) : default;
228+
return found;
232229
}
233230

234231
/// <summary>Returns the owner-side metadata for the specified block node, if present.</summary>
235232
public bool TryGetBlockInfo(int blockIndex, out BlockInfo info)
236233
{
237-
for (var i = 0; i < Blocks.Count; ++i)
238-
{
239-
info = Blocks.GetSurePresentRef(i);
240-
if (info.BlockIndex == blockIndex)
241-
return true;
242-
}
243-
244-
info = default;
245-
return false;
234+
ref var infoIndex = ref _blockInfoIndexesByNodeIndex.Map.TryGetValueRef(blockIndex, out var found);
235+
info = found ? Blocks.GetSurePresentRef(infoIndex) : default;
236+
return found;
246237
}
247238

248239
/// <summary>Adds a parameter node and returns its index.</summary>
@@ -1368,12 +1359,32 @@ private void BindParameterDeclaration(int ownerIndex, int declarationIndex, int
13681359
}
13691360

13701361
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1371-
private void AddLambdaInfo(int lambdaIndex, int bodyIndex, in ChildList declarations) =>
1372-
Lambdas.Add(new LambdaInfo(lambdaIndex, bodyIndex, declarations));
1362+
private void AddLambdaInfo(int lambdaIndex, int bodyIndex, in ChildList declarations)
1363+
{
1364+
ref var infoIndex = ref _lambdaInfoIndexesByNodeIndex.Map.AddOrGetValueRef(lambdaIndex, out var found);
1365+
var info = new LambdaInfo(lambdaIndex, bodyIndex, declarations);
1366+
if (found)
1367+
Lambdas.GetSurePresentRef(infoIndex) = info;
1368+
else
1369+
{
1370+
infoIndex = Lambdas.Count;
1371+
Lambdas.Add(info);
1372+
}
1373+
}
13731374

13741375
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1375-
private void AddBlockInfo(int blockIndex, int bodyExpressionsIndex, in ChildList declarations) =>
1376-
Blocks.Add(new BlockInfo(blockIndex, bodyExpressionsIndex, declarations));
1376+
private void AddBlockInfo(int blockIndex, int bodyExpressionsIndex, in ChildList declarations)
1377+
{
1378+
ref var infoIndex = ref _blockInfoIndexesByNodeIndex.Map.AddOrGetValueRef(blockIndex, out var found);
1379+
var info = new BlockInfo(blockIndex, bodyExpressionsIndex, declarations);
1380+
if (found)
1381+
Blocks.GetSurePresentRef(infoIndex) = info;
1382+
else
1383+
{
1384+
infoIndex = Blocks.Count;
1385+
Blocks.Add(info);
1386+
}
1387+
}
13771388

13781389
private int AddParameterUsageNode(in ExprNode node, int declarationIndex) =>
13791390
AddRawLeafExpressionNode(node.Type, node.Obj, ExpressionType.Parameter,

test/FastExpressionCompiler.LightExpression.UnitTests/LightExpressionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ public int Run()
3131
Can_roundtrip_light_expression_through_flat_expression();
3232
Flat_expression_preserves_parameter_and_label_identity_and_collects_closure_constants();
3333
Flat_expression_uses_parameter_declarations_parent_links_and_inline_constants();
34+
Flat_expression_collects_lambda_and_block_owner_metadata_when_flattening_system_expression();
3435
Can_convert_dynamic_runtime_variables_and_debug_info_to_light_expression_and_flat_expression();
3536
Can_build_flat_expression_directly_with_light_expression_like_api();
3637
Can_build_flat_expression_control_flow_directly();
3738
Can_property_test_generated_flat_expression_roundtrip_structurally();
38-
return 18;
39+
return 19;
3940
}
4041

4142

0 commit comments

Comments
 (0)