From e3bbd8d58b0821362297bfdda73875f1e8156f66 Mon Sep 17 00:00:00 2001 From: Max Charlamb Date: Tue, 17 Mar 2026 13:53:04 -0400 Subject: [PATCH] cDAC: Use BinaryThenLinearSearch in GetExceptionClauses Refactor GetExceptionClauses in ReadyToRunJitManager to use the shared BinaryThenLinearSearch helper instead of an inline binary+linear search, reducing code duplication. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...ecutionManagerCore.ReadyToRunJitManager.cs | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ExecutionManager/ExecutionManagerCore.ReadyToRunJitManager.cs b/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ExecutionManager/ExecutionManagerCore.ReadyToRunJitManager.cs index ff08e588e2823e..5f9b6a9d593dfb 100644 --- a/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ExecutionManager/ExecutionManagerCore.ReadyToRunJitManager.cs +++ b/src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/ExecutionManager/ExecutionManagerCore.ReadyToRunJitManager.cs @@ -276,36 +276,23 @@ private void GetExceptionClauses(TargetPointer exceptionLookupTableAddr, uint co if (count < 2) return; - uint low = 0; - uint high = count - 2; uint entrySize = Target.GetTypeInfo(DataType.ExceptionLookupTableEntry).Size!.Value; - while (high - low > 10) - { - uint mid = low + ((high - low) / 2); - Data.ExceptionLookupTableEntry middleEntry = Target.ProcessedData.GetOrAdd(exceptionLookupTableAddr + (mid * entrySize)); - if (methodRVA < middleEntry.MethodStartRVA) - { - high = mid - 1; - } - else - { - low = mid; - } - } + Data.ExceptionLookupTableEntry GetEntry(uint index) + => Target.ProcessedData.GetOrAdd(exceptionLookupTableAddr + (index * entrySize)); - for (uint i = low; i <= high; i++) - { - Data.ExceptionLookupTableEntry entry = Target.ProcessedData.GetOrAdd(exceptionLookupTableAddr + (i * entrySize)); - if (entry.MethodStartRVA == methodRVA) - { - Data.ExceptionLookupTableEntry nextEntry = Target.ProcessedData.GetOrAdd(exceptionLookupTableAddr + ((i + 1) * entrySize)); - startExInfoRVA = new TargetPointer(entry.ExceptionInfoRVA + rangeStart); - endExInfoRVA = new TargetPointer(nextEntry.ExceptionInfoRVA + rangeStart); - return; - } - } + if (!BinaryThenLinearSearch.Search( + 0, + count - 2, + index => methodRVA < GetEntry(index).MethodStartRVA, + index => methodRVA == GetEntry(index).MethodStartRVA, + out uint foundIndex)) + return; + Data.ExceptionLookupTableEntry entry = GetEntry(foundIndex); + Data.ExceptionLookupTableEntry nextEntry = GetEntry(foundIndex + 1); + startExInfoRVA = new TargetPointer(entry.ExceptionInfoRVA + rangeStart); + endExInfoRVA = new TargetPointer(nextEntry.ExceptionInfoRVA + rangeStart); } public override void GetExceptionClauses(RangeSection range, CodeBlockHandle cbh, out TargetPointer startAddr, out TargetPointer endAddr)