Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54fbd47

Browse files
authored
Improve linker performance by avoiding IndexOf (#90721)
* Improve linker performance by avoiding IndexOf * Fix typos
1 parent 5c92887 commit 54fbd47

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/tools/illink/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,39 @@ static bool IsMethodSupported (MethodDefinition method)
9292
return true;
9393
}
9494

95-
static bool HasJumpIntoTargetRange (Collection<Instruction> instructions, int firstInstr, int lastInstr, Func<Instruction, int>? mapping = null)
95+
static bool HasJumpIntoTargetRange (Collection<Instruction> instructions, int firstInstr, int lastInstr, Func<Instruction, int?>? mapping = null)
9696
{
9797
foreach (var instr in instructions) {
9898
switch (instr.OpCode.FlowControl) {
9999
case FlowControl.Branch:
100100
case FlowControl.Cond_Branch:
101101
if (instr.Operand is Instruction target) {
102-
int index = mapping == null ? instructions.IndexOf (target) : mapping (target);
103-
if (index >= firstInstr && index <= lastInstr)
104-
return true;
102+
if (mapping != null && mapping (target) is int index) {
103+
if (index >= firstInstr && index <= lastInstr) {
104+
return true;
105+
}
106+
}
107+
else {
108+
for (int i = firstInstr; i <= lastInstr; i++) {
109+
if (instructions[i] == target) {
110+
return true;
111+
}
112+
}
113+
}
105114
} else {
106115
foreach (var rtarget in (Instruction[]) instr.Operand) {
107-
int index = mapping == null ? instructions.IndexOf (rtarget) : mapping (rtarget);
108-
if (index >= firstInstr && index <= lastInstr)
109-
return true;
116+
if (mapping != null && mapping (rtarget) is int index) {
117+
if (index >= firstInstr && index <= lastInstr) {
118+
return true;
119+
}
120+
}
121+
else {
122+
for (int i = firstInstr; i <= lastInstr; i++) {
123+
if (instructions[i] == rtarget) {
124+
return true;
125+
}
126+
}
127+
}
110128
}
111129
}
112130

@@ -1175,6 +1193,15 @@ int GetInstructionIndex (Instruction instruction)
11751193
return idx;
11761194
}
11771195

1196+
int? TryGetInstructionIndex (Instruction instruction)
1197+
{
1198+
Debug.Assert (mapping != null);
1199+
if (mapping.TryGetValue (instruction, out int idx))
1200+
return idx;
1201+
1202+
return null;
1203+
}
1204+
11781205
bool GetOperandsConstantValues (int index, out object? left, out object? right)
11791206
{
11801207
Debug.Assert (FoldedInstructions != null);
@@ -1213,7 +1240,7 @@ static bool IsPairedStlocLdloc (Instruction first, Instruction second)
12131240
bool IsJumpTargetRange (int firstInstr, int lastInstr)
12141241
{
12151242
Debug.Assert (FoldedInstructions != null);
1216-
return HasJumpIntoTargetRange (FoldedInstructions, firstInstr, lastInstr, GetInstructionIndex);
1243+
return HasJumpIntoTargetRange (FoldedInstructions, firstInstr, lastInstr, TryGetInstructionIndex);
12171244
}
12181245
}
12191246

0 commit comments

Comments
 (0)