-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69673 from sharwell/trim-builder
Avoid static overhead for switch-over-string optimization
- Loading branch information
Showing
10 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Compilers/Core/Portable/Collections/ImmutableHashSetExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
|
||
namespace Microsoft.CodeAnalysis; | ||
|
||
internal static class ImmutableHashSetExtensions | ||
{ | ||
/// <summary> | ||
/// Performs a <see cref="ImmutableHashSet{T}.SetEquals"/> comparison without allocating an intermediate | ||
/// <see cref="HashSet{T}"/>. | ||
/// </summary> | ||
/// <seealso href="https://github.com/dotnet/runtime/issues/90986"/> | ||
public static bool SetEqualsWithoutIntermediateHashSet<T>(this ImmutableHashSet<T> set, ImmutableHashSet<T> other) | ||
{ | ||
if (set is null) | ||
throw new ArgumentNullException(nameof(set)); | ||
if (other is null) | ||
throw new ArgumentNullException(nameof(other)); | ||
|
||
if (ReferenceEquals(set, other)) | ||
return true; | ||
|
||
var otherSet = other.WithComparer(set.KeyComparer); | ||
if (set.Count != otherSet.Count) | ||
return false; | ||
|
||
foreach (var item in other) | ||
{ | ||
if (!set.Contains(item)) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Linq; | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Engines; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
|
||
namespace IdeCoreBenchmarks; | ||
|
||
[MemoryDiagnoser] | ||
[SimpleJob(RunStrategy.Throughput, invocationCount: 1)] | ||
public class SwitchStatementBenchmarks | ||
{ | ||
[Params(100, 1000, 5000, 10000)] | ||
public int SwitchCount | ||
{ | ||
get; | ||
set; | ||
} | ||
|
||
private static string CreateSourceFile(int switchCount) | ||
{ | ||
var builder = new StringBuilder(); | ||
builder.AppendLine( | ||
""" | ||
class TestClass | ||
{ | ||
int TestMethod(string arg) | ||
{ | ||
switch (arg) | ||
{ | ||
"""); | ||
|
||
for (var i = 0; i < switchCount; i++) | ||
{ | ||
builder.AppendLine( | ||
$""" | ||
case "Text{i}": return {i}; | ||
"""); | ||
} | ||
|
||
builder.AppendLine( | ||
""" | ||
default: return 0; | ||
} | ||
} | ||
} | ||
"""); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
[Benchmark] | ||
public object BindFile() | ||
{ | ||
var projectId = ProjectId.CreateNewId(); | ||
var documentId = DocumentId.CreateNewId(projectId); | ||
|
||
using var workspace = new AdhocWorkspace(); | ||
|
||
var solution = workspace.CurrentSolution | ||
.AddProject(projectId, "ProjectName", "AssemblyName", LanguageNames.CSharp) | ||
.AddDocument(documentId, "DocumentName", CreateSourceFile(SwitchCount)); | ||
|
||
solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(object).Assembly.Location)); | ||
solution = solution.WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
||
var project = solution.Projects.First(); | ||
var compilation = project.GetCompilationAsync().Result; | ||
return compilation.EmitToStream(); | ||
} | ||
} |