Skip to content

Commit

Permalink
Use small exception regions when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateo Torres Ruiz committed Oct 14, 2021
1 parent 6afadd3 commit 9011ad4
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ public int Write(ModuleWritingContext writeContext)
MethodBodyBlock bodyBlock = _module.PEReader.GetMethodBody(rva);
var exceptionRegions = bodyBlock.ExceptionRegions;

// Use small exception regions when the code size of the try block and
// the handler code are less than 256 bytes and offsets smaller than 65536 bytes.
bool useSmallExceptionRegions = ExceptionRegionEncoder.IsSmallRegionCount(exceptionRegions.Length);
if (useSmallExceptionRegions)
{
foreach (var exceptionRegion in exceptionRegions)
{
if (!ExceptionRegionEncoder.IsSmallExceptionRegion(exceptionRegion.TryOffset, exceptionRegion.TryLength) ||
!ExceptionRegionEncoder.IsSmallExceptionRegion(exceptionRegion.HandlerOffset, exceptionRegion.HandlerLength))
{
useSmallExceptionRegions = false;
break;
}
}
}

BlobBuilder outputBodyBuilder = writeContext.GetSharedBlobBuilder();
byte[] bodyBytes = bodyBlock.GetILBytes();
ILReader ilReader = new ILReader(bodyBytes);
Expand Down Expand Up @@ -187,7 +203,7 @@ public int Write(ModuleWritingContext writeContext)
outputBodyBuilder.Count,
bodyBlock.MaxStack,
exceptionRegionCount: exceptionRegions.Length,
hasSmallExceptionRegions: false,
hasSmallExceptionRegions: useSmallExceptionRegions,
(StandaloneSignatureHandle)writeContext.TokenMap.MapToken(bodyBlock.LocalSignature),
bodyBlock.LocalVariablesInitialized ? MethodBodyAttributes.InitLocals : MethodBodyAttributes.None);
BlobWriter instructionsWriter = new(bodyEncoder.Instructions);
Expand Down

0 comments on commit 9011ad4

Please sign in to comment.