Skip to content

Commit 10379ef

Browse files
committed
Preparation for Name hashing performance update and Host Precompilation performance update.
1 parent e90084e commit 10379ef

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

EnumSourceGenerator/EnumGenerator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3030
if (iContentInterface?.TypeArguments.FirstOrDefault() is not INamedTypeSymbol typeArgument) continue;
3131

3232
// Extract enum member names from the All property of the class
33+
var structName = typeArgument.Name;
3334
var enumMembers = ExtractEnumMembers(ctx, classSymbol).ToImmutableArray();
3435

3536
var source = GenerateEnumSource(classSymbol.Name, enumMembers);
3637
ctx.AddSource($"{classSymbol.Name}TypeEnum.g.cs", SourceText.From(source, Encoding.UTF8));
3738

38-
var helper = GeneratePartialHelper(classSymbol.Name,
39+
var helper = GeneratePartialHelper(classSymbol.Name, structName,
3940
classSymbol.ContainingNamespace.ToDisplayString(),
4041
typeArgument.ToDisplayString(), enumMembers);
4142
ctx.AddSource($"{classSymbol.Name}Helper.g.cs", SourceText.From(helper, Encoding.UTF8));
@@ -196,14 +197,19 @@ public enum {className}Type
196197
}}";
197198
}
198199

199-
private static string GeneratePartialHelper(string className, string fullNamespace, string typeArgument, ImmutableArray<string> enumMembers)
200+
private static string GeneratePartialHelper(string className, string structName, string fullNamespace, string typeArgument, ImmutableArray<string> enumMembers)
200201
{
201202
var membersSource = string.Join(";\n", enumMembers.Select((m, i) => $" public {typeArgument} {m} => All[{i}]"));
202203
return $@"// Auto-generated code
203204
using ContentEnums;
204205
205206
namespace {fullNamespace}
206207
{{
208+
public partial record struct {structName}
209+
{{
210+
public bool Equals({structName} other) => string.Equals(Name, other.Name);
211+
public override int GetHashCode() => Name?.GetHashCode() ?? 0;
212+
}}
207213
public partial class {className}
208214
{{
209215
public {typeArgument} Get({className}Type type) => All[(int)type];
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//using Microsoft.CodeAnalysis;
2+
//using Microsoft.CodeAnalysis.CSharp;
3+
//using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
//using Microsoft.CodeAnalysis.Text;
5+
//using System.Linq;
6+
//using System.Text;
7+
8+
//namespace EnumSourceGenerator;
9+
10+
//[Generator]
11+
//public class HostGenerator : IIncrementalGenerator
12+
//{
13+
// public void Initialize(IncrementalGeneratorInitializationContext context)
14+
// {
15+
// var classDeclarations = context.SyntaxProvider
16+
// .CreateSyntaxProvider(
17+
// predicate: static (s, _) => s is ClassDeclarationSyntax,
18+
// transform: static (ctx, _) =>
19+
// {
20+
// var classSyntax = (ClassDeclarationSyntax)ctx.Node;
21+
// var symbol = ctx.SemanticModel.GetDeclaredSymbol(classSyntax);
22+
23+
// return symbol?.GetAttributes().Any(a =>
24+
// a.AttributeClass?.Name == "SingletonAttribute") == true
25+
// ? symbol
26+
// : null;
27+
// })
28+
// .Where(static symbol => symbol is not null);
29+
30+
// var compilationAndClasses = context.CompilationProvider.Combine(classDeclarations.Collect());
31+
32+
// context.RegisterSourceOutput(compilationAndClasses, (spc, source) =>
33+
// {
34+
// var (compilation, classes) = source;
35+
// var sb = new StringBuilder();
36+
// sb.AppendLine("namespace SimpleInjection;");
37+
// sb.AppendLine("public static partial class Host");
38+
// sb.AppendLine("{");
39+
// sb.AppendLine(" public static Host Configure()");
40+
// sb.AppendLine(" {");
41+
// sb.AppendLine(" var host = new Host();");
42+
43+
// foreach (var impl in classes.Distinct())
44+
// {
45+
// var attr = impl.GetAttributes().FirstOrDefault(a =>
46+
// a.AttributeClass?.Name == "SingletonAttribute");
47+
48+
// var iface = attr?.ConstructorArguments[0].Value as INamedTypeSymbol;
49+
50+
// if (iface != null)
51+
// {
52+
// sb.AppendLine($" host._services.AddSingleton<{iface.ToDisplayString()}, {impl.ToDisplayString()}>();");
53+
// }
54+
// }
55+
56+
// sb.AppendLine(" return host;");
57+
// sb.AppendLine(" }");
58+
// sb.AppendLine("}");
59+
60+
// spc.AddSource("Host.Configure.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8));
61+
// });
62+
// }
63+
//}

SimpleInjection.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.14.36203.30 d17.14
4+
VisualStudioVersion = 17.14.36203.30
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleInjection", "SimpleInjection\SimpleInjection.csproj", "{EBCC1F56-6AF3-475D-8DD9-12ADEDD53E9C}"
77
EndProject

SimpleInjection/Injection/Host.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// initialization and dependency resolution. It automatically discovers attributed classes and registers them
1010
/// with their appropriate lifetimes.
1111
/// </remarks>
12-
public sealed class Host
12+
public sealed partial class Host
1313
{
1414
private readonly List<ServiceDescriptor> _serviceDescriptors = [];
1515
private readonly Dictionary<Type, object> _singletonInstances = [];
@@ -28,6 +28,7 @@ private Host() { }
2828
/// with service lifetime attributes (<see cref="SingletonAttribute"/>, <see cref="ScopedAttribute"/>,
2929
/// <see cref="TransientAttribute"/>). It then creates a new host and registers these types with appropriate lifetimes.
3030
/// </remarks>
31+
//[Obsolete("Use Host.Configure() instead. This method will be removed in future versions.")]
3132
public static Host Initialize()
3233
{
3334
var host = new Host();

0 commit comments

Comments
 (0)