Skip to content

Commit

Permalink
Improved usage of MethodImpl attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed May 16, 2024
1 parent f07ba41 commit 555887b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
6 changes: 2 additions & 4 deletions build/ReadmeTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("```");
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/Code/ApiMembersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}<T>()");
apiCode.AppendLine("{");
using (apiCode.Indent())
Expand All @@ -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}<T>(object{nullable} tag)");
apiCode.AppendLine("{");
using (apiCode.Indent())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/BlockCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}()");
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Code/ConstructCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}()");
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.Core/Core/Code/FactoryRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// ReSharper disable InvertIf
namespace Pure.DI.Core.Code;

using System.Runtime.CompilerServices;

internal sealed class FactoryRewriter(
IArguments arguments,
DpFactory factory,
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/Code/RootMethodsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions src/Pure.DI.Core/Core/Names.cs
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand Down
8 changes: 4 additions & 4 deletions src/Pure.DI.MS/any/Pure.DI/MS/ServiceProviderFactory.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <inheritdoc />
[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.
Expand Down Expand Up @@ -110,7 +110,7 @@ public IServiceProvider CreateServiceProvider(IServiceCollection services)
/// <param name="lifetime">Dependency resolution lifetime.</param>
/// <typeparam name="T">Dependency resolution type.</typeparam>
/// <returns>Resolved dependency instance.</returns>
[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<T>(object? tag, Lifetime lifetime)
{
return (T)(_serviceProvider ?? throw new InvalidOperationException("Not ready yet."))(typeof(T), tag)
Expand All @@ -126,7 +126,7 @@ protected T OnCannotResolve<T>(object? tag, Lifetime lifetime)
/// <param name="lifetime">The lifetime of the composition root.</param>
/// <typeparam name="TContract">The contract type of the composition root.</typeparam>
/// <typeparam name="T">The implementation type of the composition root.</typeparam>
[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<TContract, T>(
IResolver<TComposition, TContract> resolver,
string name, object tag, Lifetime lifetime)
Expand Down

0 comments on commit 555887b

Please sign in to comment.