Skip to content

Commit

Permalink
Improve linker performance by avoiding IndexOf (dotnet#90721)
Browse files Browse the repository at this point in the history
* Improve linker performance by avoiding IndexOf

* Fix typos
  • Loading branch information
Youssef1313 authored Aug 18, 2023
1 parent 5c92887 commit 54fbd47
Showing 1 changed file with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,39 @@ static bool IsMethodSupported (MethodDefinition method)
return true;
}

static bool HasJumpIntoTargetRange (Collection<Instruction> instructions, int firstInstr, int lastInstr, Func<Instruction, int>? mapping = null)
static bool HasJumpIntoTargetRange (Collection<Instruction> instructions, int firstInstr, int lastInstr, Func<Instruction, int?>? mapping = null)
{
foreach (var instr in instructions) {
switch (instr.OpCode.FlowControl) {
case FlowControl.Branch:
case FlowControl.Cond_Branch:
if (instr.Operand is Instruction target) {
int index = mapping == null ? instructions.IndexOf (target) : mapping (target);
if (index >= firstInstr && index <= lastInstr)
return true;
if (mapping != null && mapping (target) is int index) {
if (index >= firstInstr && index <= lastInstr) {
return true;
}
}
else {
for (int i = firstInstr; i <= lastInstr; i++) {
if (instructions[i] == target) {
return true;
}
}
}
} else {
foreach (var rtarget in (Instruction[]) instr.Operand) {
int index = mapping == null ? instructions.IndexOf (rtarget) : mapping (rtarget);
if (index >= firstInstr && index <= lastInstr)
return true;
if (mapping != null && mapping (rtarget) is int index) {
if (index >= firstInstr && index <= lastInstr) {
return true;
}
}
else {
for (int i = firstInstr; i <= lastInstr; i++) {
if (instructions[i] == rtarget) {
return true;
}
}
}
}
}

Expand Down Expand Up @@ -1175,6 +1193,15 @@ int GetInstructionIndex (Instruction instruction)
return idx;
}

int? TryGetInstructionIndex (Instruction instruction)
{
Debug.Assert (mapping != null);
if (mapping.TryGetValue (instruction, out int idx))
return idx;

return null;
}

bool GetOperandsConstantValues (int index, out object? left, out object? right)
{
Debug.Assert (FoldedInstructions != null);
Expand Down Expand Up @@ -1213,7 +1240,7 @@ static bool IsPairedStlocLdloc (Instruction first, Instruction second)
bool IsJumpTargetRange (int firstInstr, int lastInstr)
{
Debug.Assert (FoldedInstructions != null);
return HasJumpIntoTargetRange (FoldedInstructions, firstInstr, lastInstr, GetInstructionIndex);
return HasJumpIntoTargetRange (FoldedInstructions, firstInstr, lastInstr, TryGetInstructionIndex);
}
}

Expand Down

0 comments on commit 54fbd47

Please sign in to comment.