Skip to content

Commit 8e618c3

Browse files
authored
Merge pull request #308 from sharwell/corelib-hits
Avoid unnecessary null checks in RecordHit
2 parents a9c90c5 + c3bc94a commit 8e618c3

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/coverlet.core/Instrumentation/Instrumenter.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class Instrumenter
2323
private readonly string[] _includeFilters;
2424
private readonly string[] _excludedFiles;
2525
private readonly string[] _excludedAttributes;
26+
private readonly bool _isCoreLibrary;
2627
private InstrumenterResult _result;
2728
private FieldDefinition _customTrackerHitsFilePath;
2829
private FieldDefinition _customTrackerHitsArray;
@@ -41,6 +42,8 @@ public Instrumenter(string module, string identifier, string[] excludeFilters, s
4142
_includeFilters = includeFilters;
4243
_excludedFiles = excludedFiles ?? Array.Empty<string>();
4344
_excludedAttributes = excludedAttributes;
45+
46+
_isCoreLibrary = Path.GetFileNameWithoutExtension(_module) == "System.Private.CoreLib";
4447
}
4548

4649
public bool CanInstrument() => InstrumentationHelper.HasPdb(_module);
@@ -74,8 +77,7 @@ private void InstrumentModule()
7477
{
7578
resolver.AddSearchDirectory(Path.GetDirectoryName(_module));
7679
var parameters = new ReaderParameters { ReadSymbols = true, AssemblyResolver = resolver };
77-
bool isCoreLib = Path.GetFileNameWithoutExtension(_module) == "System.Private.CoreLib";
78-
if (isCoreLib)
80+
if (_isCoreLibrary)
7981
{
8082
parameters.MetadataImporterProvider = new CoreLibMetadataImporterProvider();
8183
}
@@ -97,7 +99,7 @@ private void InstrumentModule()
9799
var actualType = type.DeclaringType ?? type;
98100
if (!actualType.CustomAttributes.Any(IsExcludeAttribute)
99101
// Instrumenting Interlocked which is used for recording hits would cause an infinite loop.
100-
&& (!isCoreLib || actualType.FullName != "System.Threading.Interlocked")
102+
&& (!_isCoreLibrary || actualType.FullName != "System.Threading.Interlocked")
101103
&& !InstrumentationHelper.IsTypeExcluded(_module, actualType.FullName, _excludeFilters)
102104
&& InstrumentationHelper.IsTypeIncluded(_module, actualType.FullName, _includeFilters))
103105
InstrumentType(type);
@@ -430,9 +432,12 @@ private Instruction AddInstrumentationInstructions(MethodDefinition method, ILPr
430432
{
431433
if (_customTrackerRecordHitMethod == null)
432434
{
435+
var recordHitMethodName = _isCoreLibrary
436+
? nameof(ModuleTrackerTemplate.RecordHitInCoreLibrary)
437+
: nameof(ModuleTrackerTemplate.RecordHit);
433438
_customTrackerRecordHitMethod = new MethodReference(
434-
"RecordHit", method.Module.TypeSystem.Void, _customTrackerTypeDef);
435-
_customTrackerRecordHitMethod.Parameters.Add(new ParameterDefinition(method.Module.TypeSystem.Int32));
439+
recordHitMethodName, method.Module.TypeSystem.Void, _customTrackerTypeDef);
440+
_customTrackerRecordHitMethod.Parameters.Add(new ParameterDefinition("hitLocationIndex", ParameterAttributes.None, method.Module.TypeSystem.Int32));
436441
}
437442

438443
var indxInstr = Instruction.Create(OpCodes.Ldc_I4, hitEntryIndex);

src/coverlet.template/ModuleTrackerTemplate.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics.CodeAnalysis;
43
using System.IO;
54
using System.IO.MemoryMappedFiles;
@@ -42,7 +41,7 @@ public static void RegisterUnloadEvents()
4241
AppDomain.CurrentDomain.DomainUnload += new EventHandler(UnloadModule);
4342
}
4443

45-
public static void RecordHit(int hitLocationIndex)
44+
public static void RecordHitInCoreLibrary(int hitLocationIndex)
4645
{
4746
// Make sure to avoid recording if this is a call to RecordHit within the AppDomain setup code in an
4847
// instrumented build of System.Private.CoreLib.
@@ -52,6 +51,11 @@ public static void RecordHit(int hitLocationIndex)
5251
Interlocked.Increment(ref HitsArray[hitLocationIndex]);
5352
}
5453

54+
public static void RecordHit(int hitLocationIndex)
55+
{
56+
Interlocked.Increment(ref HitsArray[hitLocationIndex]);
57+
}
58+
5559
public static void UnloadModule(object sender, EventArgs e)
5660
{
5761
// Claim the current hits array and reset it to prevent double-counting scenarios.

0 commit comments

Comments
 (0)