Skip to content

Commit

Permalink
feat: add [DynamicDependency] to a number of places to simplify trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyakunin committed Nov 20, 2023
1 parent 12e586d commit cc7b7b0
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/Stl.CommandR/CommanderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Stl.CommandR.Interception;
using Stl.CommandR.Internal;
using Stl.CommandR.Rpc;
using Stl.Interception;
using Stl.Rpc;

namespace Stl.CommandR;
Expand All @@ -16,6 +17,14 @@ private sealed class AddedTag;
public IServiceCollection Services { get; }
public HashSet<CommandHandler> Handlers { get; }

#if NET5_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Proxies))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CommandHandlerMethodDef))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MethodCommandHandler<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(InterfaceCommandHandler<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CommandServiceInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CommandContext<>))]
#endif
[RequiresUnreferencedCode(UnreferencedCode.Commander)]
internal CommanderBuilder(
IServiceCollection services,
Expand Down
13 changes: 13 additions & 0 deletions src/Stl.Fusion/FusionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ public readonly struct FusionBuilder
public RpcBuilder Rpc { get; }
public RpcServiceMode ServiceMode { get; }

#if NET5_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CommanderBuilder))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcBuilder))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ComputeServiceInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ComputeMethodFunction<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcComputeSystemCalls))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcInboundComputeCall<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcOutboundComputeCall<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ClientComputeServiceInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ClientComputeMethodFunction<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(FuncComputedState<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(AnonymousComputedSource<>))]
#endif
[RequiresUnreferencedCode(UnreferencedCode.Fusion)]
internal FusionBuilder(
IServiceCollection services,
Expand Down
14 changes: 4 additions & 10 deletions src/Stl.Fusion/Interception/ComputeFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ public interface IComputeFunction
ComputedOptions ComputedOptions { get; }
}

public abstract class ComputeFunctionBase<T> : FunctionBase<T>, IComputeFunction
public abstract class ComputeFunctionBase<T>(ComputeMethodDef methodDef, IServiceProvider services)
: FunctionBase<T>(services), IComputeFunction
{
public ComputeMethodDef MethodDef { get; }
public ComputedOptions ComputedOptions { get; }

protected ComputeFunctionBase(ComputeMethodDef methodDef, IServiceProvider services)
: base(services)
{
MethodDef = methodDef;
ComputedOptions = methodDef.ComputedOptions;
}
public ComputeMethodDef MethodDef { get; } = methodDef;
public ComputedOptions ComputedOptions { get; } = methodDef.ComputedOptions;

public override string ToString()
=> MethodDef.FullName;
Expand Down
14 changes: 13 additions & 1 deletion src/Stl.Interception/ArgumentList-Generated.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable ArrangeConstructorOrDestructorBody
using Cysharp.Text;
Expand All @@ -14,6 +14,18 @@ namespace Stl.Interception;

public abstract partial record ArgumentList
{
#if NET5_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , , , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , , , , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , , , , , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , , , , , , >))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<, , , , , , , , , >))]
#endif
public static readonly ImmutableArray<Type> Types = ImmutableArray.Create(new [] {
typeof(ArgumentList0),
typeof(ArgumentList<>),
Expand Down
6 changes: 6 additions & 0 deletions src/Stl.Interception/ArgumentList-Generated.tt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ namespace Stl.Interception;

public abstract partial record ArgumentList
{
#if NET5_0_OR_GREATER
<# for (var itemCount = 1; itemCount <= maxItemCount; itemCount++) {
#>
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList<<#=ItemList("", itemCount)#>>))]
<# } #>
#endif
public static readonly ImmutableArray<Type> Types = ImmutableArray.Create(new [] {
typeof(ArgumentList0),
<# for (var itemCount = 1; itemCount <= maxItemCount; itemCount++) {
Expand Down
15 changes: 15 additions & 0 deletions src/Stl.Interception/Proxies.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using Cysharp.Text;
using Stl.Interception.Interceptors;
using Stl.Interception.Internal;

namespace Stl.Interception;
Expand Down Expand Up @@ -107,6 +108,20 @@ public static Type GetProxyType<TType>()
public static Type GetProxyType(Type type)
=> TryGetProxyType(type) ?? throw Errors.NoProxyType(type);

#if NET5_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(InterfaceProxy))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ProxyHelper))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Interceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(InterceptorBase))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(TypeViewInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(TypedFactoryInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(InterceptorExt))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MethodDef))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Invocation))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ArgumentList))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Result<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ResultBox<>))]
#endif
public static Type? TryGetProxyType(Type type)
=> Cache.GetOrAdd(type, static type1 => {
if (type1.IsConstructedGenericType) {
Expand Down
35 changes: 35 additions & 0 deletions src/Stl.Rpc/RpcBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.WebSockets;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Stl.Interception;
using Stl.Rpc.Caching;
using Stl.Rpc.Clients;
using Stl.Rpc.Diagnostics;
using Stl.Rpc.Infrastructure;
using Stl.Rpc.Internal;
using Stl.Rpc.Testing;
using Errors = Stl.Rpc.Internal.Errors;

namespace Stl.Rpc;
Expand All @@ -13,6 +17,37 @@ public readonly struct RpcBuilder
public IServiceCollection Services { get; }
public RpcConfiguration Configuration { get; }

#if NET5_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Proxies))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcDefaultDelegates))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcMethodDef))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcServiceDef))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcServiceRegistry))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcConfiguration))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcByteArgumentSerializer))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcMethodTracer))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcMethodActivityCounters))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcClientInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcRoutingInterceptor))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcInboundContext))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcInboundContextFactory))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcOutboundContext))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcInboundCall<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcInbound404Call<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcOutboundCall<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcMiddlewares<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcInboundMiddleware))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcOutboundMiddleware))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcServerPeer))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcClientPeer))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcHub))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcRemoteObjectTracker))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcSharedObjectTracker))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcSharedStream))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcCacheInfoCapture))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcCacheEntry))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(RpcSystemCalls))]
#endif
[RequiresUnreferencedCode(UnreferencedCode.Rpc)]
internal RpcBuilder(
IServiceCollection services,
Expand Down
2 changes: 0 additions & 2 deletions src/Stl.Rpc/Testing/RpcTestClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Stl.Channels;
using Stl.Internal;
using Stl.Rpc.Infrastructure;
using Stl.Rpc.WebSockets;

Expand Down
6 changes: 0 additions & 6 deletions src/Stl/Reflection/TypeExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ public static Func<Type, Type> NonProxyTypeResolver {
}
}

public static void MarkUsed<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>()
{ }

public static void MarkUsed([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type)
{ }

public static Type NonProxyType(this Type type)
=> NonProxyTypeCache.GetOrAdd(type, NonProxyTypeResolver);

Expand Down

0 comments on commit cc7b7b0

Please sign in to comment.