From 555887b02acb559b58be3a98f545e37706d14f99 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Thu, 16 May 2024 09:51:25 +0300 Subject: [PATCH] Improved usage of MethodImpl attribute --- build/ReadmeTarget.cs | 6 ++---- src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs | 8 ++++---- src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs | 2 +- src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs | 2 +- src/Pure.DI.Core/Core/Code/FactoryRewriter.cs | 8 ++++---- src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs | 4 ++-- src/Pure.DI.Core/Core/Names.cs | 9 +++++++-- .../any/Pure.DI/MS/ServiceProviderFactory.g.cs | 8 ++++---- 8 files changed, 25 insertions(+), 22 deletions(-) diff --git a/build/ReadmeTarget.cs b/build/ReadmeTarget.cs index 7f2991ad9..591f9703b 100644 --- a/build/ReadmeTarget.cs +++ b/build/ReadmeTarget.cs @@ -362,10 +362,8 @@ private static async Task AddExample(string logsDirectory, string exampleSearchP .Replace("System.Collections.Generic.", "") .Replace("System.", "") .Replace("Pure.DI.", "") - .Replace(" Benchmarks.Model.", "") - .Replace(salt, "") - .Replace("(MethodImplOptions)256", "MethodImplOptions.AggressiveInlining") - .Replace("(MethodImplOptions)8", "MethodImplOptions.NoInlining"))); + .Replace("Benchmarks.Model.", "") + .Replace(salt, ""))); await examplesWriter.WriteLineAsync(generatedCode); await examplesWriter.WriteLineAsync("```"); } diff --git a/src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs b/src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs index 5c8624b73..86991a5f7 100644 --- a/src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs @@ -26,7 +26,7 @@ public CompositionCode Build(CompositionCode composition) } buildTools.AddPureHeader(apiCode); - apiCode.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + apiCode.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); apiCode.AppendLine($"{hints.ResolveMethodModifiers} T {hints.ResolveMethodName}()"); apiCode.AppendLine("{"); using (apiCode.Indent()) @@ -49,7 +49,7 @@ public CompositionCode Build(CompositionCode composition) } buildTools.AddPureHeader(apiCode); - apiCode.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + apiCode.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); apiCode.AppendLine($"{hints.ResolveByTagMethodModifiers} T {hints.ResolveByTagMethodName}(object{nullable} tag)"); apiCode.AppendLine("{"); using (apiCode.Indent()) @@ -174,7 +174,7 @@ private void CreateObjectResolverMethod( LinesBuilder code) { buildTools.AddPureHeader(code); - code.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + code.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); code.AppendLine($"{methodModifiers} object {methodName}({methodArgs})"); code.AppendLine("{"); using (code.Indent()) @@ -202,7 +202,7 @@ private static void CreateObjectConflictsResolverMethod( bool byTag, LinesBuilder code) { - code.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})8)]"); + code.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplNoInlining})]"); code.AppendLine($"private object Resolve{Names.Salt}({methodArgs}, int index)"); code.AppendLine("{"); using (code.Indent()) diff --git a/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs b/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs index 955da310e..d19ddd899 100644 --- a/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs @@ -127,7 +127,7 @@ variable.Node.Lifetime is Lifetime.Singleton or Lifetime.Scoped var localMethodCode = ctx.LocalFunctionsCode; if (variable.Node.Binding.SemanticModel.Compilation.GetLanguageVersion() >= LanguageVersion.CSharp9) { - localMethodCode.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + localMethodCode.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); } localMethodCode.AppendLine($"void {localMethodName}()"); diff --git a/src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs b/src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs index 88908b53c..59046a51a 100644 --- a/src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs @@ -53,7 +53,7 @@ private void BuildEnumerable(BuildContext ctx, in DpConstruct enumerable, string var localMethodName = $"{Names.EnumerateMethodNamePrefix}_{variable.VariableDeclarationName}".Replace("__", "_"); if (enumerable.Source.SemanticModel.Compilation.GetLanguageVersion() >= LanguageVersion.CSharp9) { - code.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + code.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); } code.AppendLine($"{methodPrefix}{typeResolver.Resolve(variable.InstanceType)} {localMethodName}()"); diff --git a/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs b/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs index 54d8ed5a7..f1683262d 100644 --- a/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs +++ b/src/Pure.DI.Core/Core/Code/FactoryRewriter.cs @@ -2,6 +2,8 @@ // ReSharper disable InvertIf namespace Pure.DI.Core.Code; +using System.Runtime.CompilerServices; + internal sealed class FactoryRewriter( IArguments arguments, DpFactory factory, @@ -12,12 +14,10 @@ internal sealed class FactoryRewriter( { private static readonly AttributeListSyntax MethodImplAttribute = SyntaxFactory.AttributeList().AddAttributes( SyntaxFactory.Attribute( - SyntaxFactory.IdentifierName(Names.MethodImplAttribute), + SyntaxFactory.IdentifierName(Names.MethodImplAttributeName), SyntaxFactory.AttributeArgumentList().AddArguments( SyntaxFactory.AttributeArgument( - SyntaxFactory.CastExpression( - SyntaxFactory.ParseTypeName(Names.MethodImplOptions), - SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(256))))))) + SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ParseTypeName(Names.MethodImplOptionsName), SyntaxFactory.IdentifierName(Names.MethodImplAggressiveInliningOptionsName)))))) .WithTrailingTrivia(SyntaxTriviaList.Create(SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, " "))); private static readonly IdentifierNameSyntax InjectionMarkerExpression = SyntaxFactory.IdentifierName(Names.InjectionMarker); diff --git a/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs b/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs index 2b88f95e1..e503b6edf 100644 --- a/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs +++ b/src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs @@ -86,7 +86,7 @@ private void BuildRoot(CompositionCode composition, Root root) name.Append(rootArgsStr); if (root.IsMethod) { - code.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + code.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); } code.AppendLine(name.ToString()); @@ -145,7 +145,7 @@ private void BuildRoot(CompositionCode composition, Root root) if (!root.IsMethod) { buildTools.AddPureHeader(code); - code.AppendLine($"[{Names.MethodImplAttribute}(({Names.MethodImplOptions})256)]"); + code.AppendLine($"[{Names.MethodImplAttributeName}({Names.MethodImplAggressiveInlining})]"); code.AppendLine("get"); code.AppendLine("{"); indentToken = code.Indent(); diff --git a/src/Pure.DI.Core/Core/Names.cs b/src/Pure.DI.Core/Core/Names.cs index 39c31f836..c366cdf3e 100644 --- a/src/Pure.DI.Core/Core/Names.cs +++ b/src/Pure.DI.Core/Core/Names.cs @@ -1,6 +1,8 @@ // ReSharper disable InconsistentNaming namespace Pure.DI.Core; +using System.Runtime.CompilerServices; + internal static class Names { public static readonly string Salt = $"M{DateTime.Now.Month:00}D{DateTime.Now.Day:00}di"; @@ -12,8 +14,11 @@ internal static class Names public const string SystemNamespace = $"global::{nameof(System)}."; // Attributes - public const string MethodImplAttribute = $"{SystemNamespace}Runtime.CompilerServices.MethodImpl"; - public const string MethodImplOptions = $"{SystemNamespace}Runtime.CompilerServices.MethodImplOptions"; + public const string MethodImplAttributeName = $"{SystemNamespace}Runtime.CompilerServices.MethodImpl"; + public const string MethodImplOptionsName = $"{SystemNamespace}Runtime.CompilerServices.{nameof(MethodImplOptions)}"; + public const string MethodImplAggressiveInliningOptionsName = nameof(MethodImplOptions.AggressiveInlining); + public const string MethodImplAggressiveInlining = $"{MethodImplOptionsName}.{MethodImplAggressiveInliningOptionsName}"; + public const string MethodImplNoInlining = $"{MethodImplOptionsName}.{nameof(MethodImplOptions.NoInlining)}"; // Messages public const string CannotResolveMessage = "Cannot resolve composition root"; diff --git a/src/Pure.DI.MS/any/Pure.DI/MS/ServiceProviderFactory.g.cs b/src/Pure.DI.MS/any/Pure.DI/MS/ServiceProviderFactory.g.cs index c50f242e7..a0a7196ad 100644 --- a/src/Pure.DI.MS/any/Pure.DI/MS/ServiceProviderFactory.g.cs +++ b/src/Pure.DI.MS/any/Pure.DI/MS/ServiceProviderFactory.g.cs @@ -75,14 +75,14 @@ private static void HintsSetup() => #if NETSTANDARD2_0_OR_GREATER || NETCOREAPP || NET40_OR_GREATER || NET [global::System.Diagnostics.Contracts.Pure] #endif - [global::System.Runtime.CompilerServices.MethodImpl((global::System.Runtime.CompilerServices.MethodImplOptions)256)] + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] protected static IServiceCollection CreateServiceCollection(TComposition composition) { return ServiceCollectionFactory.CreateServiceCollection(composition); } /// - [global::System.Runtime.CompilerServices.MethodImpl((global::System.Runtime.CompilerServices.MethodImplOptions)256)] + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] public IServiceCollection CreateBuilder(IServiceCollection services) { // Registers composition roots as services in the service collection. @@ -110,7 +110,7 @@ public IServiceProvider CreateServiceProvider(IServiceCollection services) /// Dependency resolution lifetime. /// Dependency resolution type. /// Resolved dependency instance. - [global::System.Runtime.CompilerServices.MethodImpl((global::System.Runtime.CompilerServices.MethodImplOptions)256)] + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] protected T OnCannotResolve(object? tag, Lifetime lifetime) { return (T)(_serviceProvider ?? throw new InvalidOperationException("Not ready yet."))(typeof(T), tag) @@ -126,7 +126,7 @@ protected T OnCannotResolve(object? tag, Lifetime lifetime) /// The lifetime of the composition root. /// The contract type of the composition root. /// The implementation type of the composition root. - [global::System.Runtime.CompilerServices.MethodImpl((global::System.Runtime.CompilerServices.MethodImplOptions)256)] + [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] protected static void OnNewRoot( IResolver resolver, string name, object tag, Lifetime lifetime)