Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Oct 11, 2023
1 parent b19ce07 commit 5efd32b
Show file tree
Hide file tree
Showing 38 changed files with 459 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

namespace NetFabric.Hyperlinq.Analyzer.Benchmarks;

public class HLQ010_UseForLoop
public class HLQ010_UseForLoop_ValueTypeEnumerator
{
List<int>? list;
ArraySegment<int> source;

[Params(100, 10_000)]
public int Count { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
list = System.Linq.Enumerable.Range(0, Count).ToList();
source = new ArraySegment<int>(Enumerable.Range(0, Count).ToArray());
}

[Benchmark(Baseline = true)]
public int Foreach()
{
var sum = 0;
foreach (var item in list!)
foreach (var item in source!)
sum += item;
return sum;
}
Expand All @@ -28,9 +28,9 @@ public int Foreach()
public int For()
{
var sum = 0;
for (var index = 0; index < list!.Count; index++)
for (var index = 0; index < source!.Count; index++)
{
var item = list![index];
var item = source![index];
sum += item;
}
return sum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.6" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.9" />
</ItemGroup>
</Project>
30 changes: 8 additions & 22 deletions NetFabric.Hyperlinq.Analyzer.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,15 @@
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;

namespace NetFabric.Hyperlinq.Analyzer.Benchmarks;

class Program
{
static void Main(string[] args)
{
var config = DefaultConfig.Instance
var config = DefaultConfig.Instance
.WithSummaryStyle(SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend))
.AddDiagnoser(MemoryDiagnoser.Default)
.AddDiagnoser(new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig(
printSource: true,
exportGithubMarkdown: true)))
//.AddDiagnoser(new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig(
// printSource: true,
// exportGithubMarkdown: true)))
.AddExporter(MarkdownExporter.GitHub)
.AddJob(Job.Default
.WithRuntime(CoreRuntime.Core60)
.WithId(".NET 6"))
.AddJob(Job.Default
.WithRuntime(CoreRuntime.Core70)
.WithId(".NET 7"))
.AddJob(Job.Default
.WithRuntime(CoreRuntime.Core80)
.WithId(".NET 8"));
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core60))
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core70))
.AddJob(Job.Default.WithRuntime(CoreRuntime.Core80));

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class UseForLoopAnalyzerTests : DiagnosticVerifier
[Theory]
[InlineData("TestData/HLQ010/NoDiagnostic/Array.cs")]
[InlineData("TestData/HLQ010/NoDiagnostic/Span.cs")]
[InlineData("TestData/HLQ010/NoDiagnostic/List.cs")]
[InlineData("TestData/HLQ010/NoDiagnostic/ReadOnlySpan.cs")]
[InlineData("TestData/HLQ010/NoDiagnostic/Dictionary.cs")]
[InlineData("TestData/HLQ010/NoDiagnostic/ImmutableArray.cs")]
public void Verify_NoDiagnostics(string path)
{
var paths = new[]
Expand All @@ -30,7 +32,7 @@ public void Verify_NoDiagnostics(string path)
}

[Theory]
[InlineData("TestData/HLQ010/Diagnostic/List.cs", 11, 34)]
[InlineData("TestData/HLQ010/Diagnostic/ArraySegment.cs", 10, 34)]
public void Verify_Diagnostic(string path, int line, int column)
{
var paths = new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public class UseForEachLoopAnalyzerTests : DiagnosticVerifier

[Theory]
[InlineData("TestData/HLQ013/NoDiagnostic/List.cs")]
[InlineData("TestData/HLQ013/NoDiagnostic/Array.cs")]
[InlineData("TestData/HLQ013/NoDiagnostic/ImmutableArray.cs")]
[InlineData("TestData/HLQ013/NoDiagnostic/IndexNotUsed.cs")]
[InlineData("TestData/HLQ013/NoDiagnostic/MathOnIndex.cs")]
[InlineData("TestData/HLQ013/NoDiagnostic/MultipleIndexing.cs")]
[InlineData("TestData/HLQ013/NoDiagnostic/CompoundAssignmentNotOne.cs")]
public void Verify_NoDiagnostics(string path)
{
var paths = new[]
Expand All @@ -31,6 +35,10 @@ public void Verify_NoDiagnostics(string path)
[InlineData("TestData/HLQ013/Diagnostic/Array.cs", 10, 13, "int[]")]
[InlineData("TestData/HLQ013/Diagnostic/Span.cs", 10, 13, "System.Span<int>")]
[InlineData("TestData/HLQ013/Diagnostic/ReadOnlySpan.cs", 10, 13, "System.ReadOnlySpan<int>")]
[InlineData("TestData/HLQ013/Diagnostic/PrefixIncrement.cs", 10, 13, "int[]")]
[InlineData("TestData/HLQ013/Diagnostic/CompoundAssignmentOne.cs", 10, 13, "int[]")]
[InlineData("TestData/HLQ013/Diagnostic/PrefixAssignPlusOne.cs", 10, 13, "int[]")]
[InlineData("TestData/HLQ013/Diagnostic/PostfixAssignPlusOne.cs", 10, 13, "int[]")]
public void Verify_Diagnostic(string path, int line, int column, string collectionType)
{
var paths = new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ namespace TestHelper
/// </summary>
public abstract partial class DiagnosticVerifier
{
private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
private static readonly MetadataReference SystemPrivateCorLibReference = MetadataReference.CreateFromFile(typeof(Object).Assembly.Location);
private static readonly MetadataReference SystemRuntimeReference = MetadataReference.CreateFromFile(typeof(ValueType).Assembly.Location);
private static readonly MetadataReference SystemLinqReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
private static readonly MetadataReference MicrosoftCodeAnalysisCSharpReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
private static readonly MetadataReference MicrosoftCodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
private static readonly MetadataReference SystemCollectionsImmutableReference = MetadataReference.CreateFromFile(typeof(ImmutableArray).Assembly.Location);
private static readonly MetadataReference SystemRuntimeCompilerServicesUnsafeReference = MetadataReference.CreateFromFile(typeof(System.Runtime.CompilerServices.Unsafe).Assembly.Location);

internal static string DefaultFilePathPrefix = "Test";
internal static string CSharpDefaultFileExt = "cs";
Expand Down Expand Up @@ -138,27 +141,56 @@ protected static Document CreateDocument(string source, string language = Langua
private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
{
string fileNamePrefix = DefaultFilePathPrefix;
string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference);

string fileExt = language == LanguageNames.CSharp
? CSharpDefaultFileExt
: VisualBasicDefaultExt;

// Create the workspace
var workspace = new AdhocWorkspace();

// Create the project
var projectId = ProjectId.CreateNewId();
var versionStamp = VersionStamp.Create();
var projectName = TestProjectName;
var assemblyName = TestProjectName;
var projectInfo = ProjectInfo.Create(
projectId,
versionStamp,
projectName,
assemblyName,
language);

// Set the target framework by adding metadata references
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
projectInfo = projectInfo.WithCompilationOptions(compilationOptions);

var metadataReferences = new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Runtime.CompilerServices.Unsafe).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ImmutableArray).Assembly.Location),
// TODO: Add other framework assemblies as needed
};
projectInfo = projectInfo.WithMetadataReferences(metadataReferences);

// Add the project to the workspace
var project = workspace.AddProject(projectInfo);

// Add documents to the project
int count = 0;
foreach (var source in sources)
foreach (var sourceCode in sources)
{
var newFileName = fileNamePrefix + count + "." + fileExt;
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
// Add the document to the project
var newFileName = $"{fileNamePrefix}{count}.{fileExt}";
var sourceText = SourceText.From(sourceCode);
project = project.AddDocument(newFileName, sourceText).Project;

count++;
}
return solution.GetProject(projectId);
return project;
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="System.Collections.Immutable" Version="7.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace HLQ010.NoDiagnostic.ArraySegment
{
partial class C
{
void Method()
{
var source = new ArraySegment<int>();
foreach (var item in source)
Console.WriteLine(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace HLQ010.NoDiagnostic.ImmutableArray
{
partial class C
{
void Method()
{
var source = System.Collections.Immutable.ImmutableArray.Create(System.Array.Empty<int>());
foreach (var item in source)
Console.WriteLine(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace HLQ010.NoDiagnostic.List
namespace HLQ010.Diagnostic.List
{
partial class C
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;

namespace HLQ013.Diagnostic.Array
namespace HLQ013.Diagnostic.ArrayTest
{
partial class C
{
void Method()
{
var source = new[] { 1, 2, 3 };
for (var index = 0; index < source.Length; index++)
for (var index = 0; index < source.Length; index++)
{
var item = source[index];
Console.WriteLine(item);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HLQ013.Diagnostic.CompoundAssignmentOneTest
{
partial class C
{
void Method()
{
var source = new[] { 1, 2, 3 };
for (var index = 0; index < source.Length; index += 1)
{
var item = source[index];
Console.WriteLine(item);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HLQ013.Diagnostic.PostfixAssignPlusOneTest
{
partial class C
{
void Method()
{
var source = new[] { 1, 2, 3 };
for (var index = 0; index < source.Length; index = 1 + index)
{
var item = source[index];
Console.WriteLine(item);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HLQ013.Diagnostic.PrefixAssignPlusOneTest
{
partial class C
{
void Method()
{
var source = new[] { 1, 2, 3 };
for (var index = 0; index < source.Length; index = index + 1)
{
var item = source[index];
Console.WriteLine(item);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace HLQ013.Diagnostic.PrefixIncrementTest
{
partial class C
{
void Method()
{
var source = new[] { 1, 2, 3 };
for (var index = 0; index < source.Length; ++index)
{
var item = source[index];
Console.WriteLine(item);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace HLQ013.Diagnostic.ReadOnlySpan
namespace HLQ013.Diagnostic.ReadOnlySpanTest
{
partial class C
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace HLQ013.Diagnostic.Span
namespace HLQ013.Diagnostic.SpanTest
{
partial class C
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace HLQ013.NoDiagnostic.CompoundAssignmentNotOneTest
{
partial class C
{
void Method()
{
var source = new[] { 1, 2, 3 };

for (var index = 0; index < source.Length; index += 2) // not incrementing by 1
{
var item = source[index];
Console.WriteLine(item);
}
}
}
}
Loading

0 comments on commit 5efd32b

Please sign in to comment.