diff --git a/WebApiClientCore.Benchmarks/Requests/Benchmark.cs b/WebApiClientCore.Benchmarks/Requests/Benchmark.cs index 8df2d182..775769d1 100644 --- a/WebApiClientCore.Benchmarks/Requests/Benchmark.cs +++ b/WebApiClientCore.Benchmarks/Requests/Benchmark.cs @@ -34,12 +34,13 @@ public void Setup() services .AddRefitClient(new RefitSettings { - Buffered = true, }) .AddHttpMessageHandler(() => new MockResponseHandler()) .ConfigureHttpClient(c => c.BaseAddress = new Uri("http://webapiclient.com/")); this.ServiceProvider = services.BuildServiceProvider(); + this.ServiceProvider.GetService(); + this.ServiceProvider.GetService(); } } } diff --git a/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.GetBenchmark-report-github.md b/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.GetBenchmark-report-github.md index 21b1f507..d7ff935d 100644 --- a/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.GetBenchmark-report-github.md +++ b/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.GetBenchmark-report-github.md @@ -1,7 +1,7 @@ ``` -BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.4412/22H2/2022Update) -Intel Core i3-4150 CPU 3.50GHz (Haswell), 1 CPU, 4 logical and 2 physical cores +BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3593/23H2/2023Update/SunValley3) +Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores [Host] : .NET 8.0.4, X64 NativeAOT AVX2 Job=InProcess Toolchain=InProcessEmitToolchain @@ -9,6 +9,6 @@ Job=InProcess Toolchain=InProcessEmitToolchain ``` | Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | |-------------------------- |----------:|----------:|----------:|------:|--------:|-------:|----------:|------------:| -| HttpClient_GetAsync | 2.203 μs | 0.0430 μs | 0.0574 μs | 0.42 | 0.01 | 1.3237 | 2.03 KB | 0.51 | -| WebApiClientCore_GetAsync | 5.245 μs | 0.1027 μs | 0.1142 μs | 1.00 | 0.00 | 2.6169 | 4.02 KB | 1.00 | -| Refit_GetAsync | 12.336 μs | 0.2447 μs | 0.6615 μs | 2.37 | 0.13 | 3.4790 | 5.34 KB | 1.33 | +| HttpClient_GetAsync | 3.059 μs | 0.1315 μs | 0.3816 μs | 0.49 | 0.09 | 0.4959 | 2.03 KB | 0.51 | +| WebApiClientCore_GetAsync | 6.277 μs | 0.2695 μs | 0.7903 μs | 1.00 | 0.00 | 0.9766 | 4.02 KB | 1.00 | +| Refit_GetAsync | 14.295 μs | 0.4401 μs | 1.2626 μs | 2.30 | 0.31 | 1.2817 | 5.34 KB | 1.33 | diff --git a/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PostJsonBenchmark-report-github.md b/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PostJsonBenchmark-report-github.md index fee92064..668c5b3f 100644 --- a/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PostJsonBenchmark-report-github.md +++ b/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PostJsonBenchmark-report-github.md @@ -1,7 +1,7 @@ ``` -BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.4412/22H2/2022Update) -Intel Core i3-4150 CPU 3.50GHz (Haswell), 1 CPU, 4 logical and 2 physical cores +BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3593/23H2/2023Update/SunValley3) +Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores [Host] : .NET 8.0.4, X64 NativeAOT AVX2 Job=InProcess Toolchain=InProcessEmitToolchain @@ -9,6 +9,6 @@ Job=InProcess Toolchain=InProcessEmitToolchain ``` | Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | |------------------------------- |----------:|----------:|----------:|------:|--------:|-------:|----------:|------------:| -| HttpClient_PostJsonAsync | 2.760 μs | 0.0294 μs | 0.0246 μs | 0.48 | 0.01 | 1.5068 | 2.31 KB | 0.55 | -| WebApiClientCore_PostJsonAsync | 5.712 μs | 0.0614 μs | 0.0512 μs | 1.00 | 0.00 | 2.7237 | 4.17 KB | 1.00 | -| Refit_PostJsonAsync | 13.246 μs | 0.0457 μs | 0.0382 μs | 2.32 | 0.02 | 3.9215 | 6.02 KB | 1.44 | +| HttpClient_PostJsonAsync | 3.804 μs | 0.1678 μs | 0.4813 μs | 0.60 | 0.11 | 0.5646 | 2.31 KB | 0.55 | +| WebApiClientCore_PostJsonAsync | 6.528 μs | 0.2695 μs | 0.7945 μs | 1.00 | 0.00 | 1.0147 | 4.17 KB | 1.00 | +| Refit_PostJsonAsync | 13.823 μs | 0.5368 μs | 1.5658 μs | 2.16 | 0.38 | 1.4038 | 5.83 KB | 1.40 | diff --git a/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PutFormBenchmark-report-github.md b/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PutFormBenchmark-report-github.md index 1082bc83..b9f4ce95 100644 --- a/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PutFormBenchmark-report-github.md +++ b/WebApiClientCore.Benchmarks/results/WebApiClientCore.Benchmarks.Requests.PutFormBenchmark-report-github.md @@ -1,13 +1,13 @@ ``` -BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.4412/22H2/2022Update) -Intel Core i3-4150 CPU 3.50GHz (Haswell), 1 CPU, 4 logical and 2 physical cores +BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3593/23H2/2023Update/SunValley3) +Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores [Host] : .NET 8.0.4, X64 NativeAOT AVX2 Job=InProcess Toolchain=InProcessEmitToolchain ``` -| Method | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | -|------------------------------ |----------:|----------:|----------:|----------:|------:|--------:|-------:|----------:|------------:| -| WebApiClientCore_PutFormAsync | 8.689 μs | 0.1715 μs | 0.3136 μs | 8.733 μs | 1.00 | 0.00 | 3.2501 | 5 KB | 1.00 | -| Refit_PutFormAsync | 20.598 μs | 0.4215 μs | 1.2429 μs | 21.112 μs | 2.37 | 0.17 | 4.5776 | 7.05 KB | 1.41 | +| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen0 | Allocated | Alloc Ratio | +|------------------------------ |---------:|---------:|---------:|------:|--------:|-------:|----------:|------------:| +| WebApiClientCore_PutFormAsync | 10.45 μs | 0.415 μs | 1.204 μs | 1.00 | 0.00 | 1.2207 | 5 KB | 1.00 | +| Refit_PutFormAsync | 22.70 μs | 1.261 μs | 3.717 μs | 2.19 | 0.33 | 1.6785 | 6.89 KB | 1.38 | diff --git a/WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs b/WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs index fd923610..fe75bf2d 100644 --- a/WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs +++ b/WebApiClientCore/Implementations/SourceGeneratorHttpApiActivator.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using WebApiClientCore.Exceptions; using WebApiClientCore.Internals; @@ -21,7 +22,7 @@ public class SourceGeneratorHttpApiActivator< { private readonly ApiActionInvoker[] actionInvokers; private readonly Func activator; - private static readonly Type? _proxyClassType = SourceGeneratorProxyClassType.Find(typeof(THttpApi)); + private static readonly Type? _proxyClassType = SourceGeneratorProxyClassFinder.Find(typeof(THttpApi)); /// /// 获取是否支持 @@ -51,7 +52,9 @@ public SourceGeneratorHttpApiActivator(IApiActionDescriptorProvider apiActionDes .Select(actionInvokerProvider.CreateActionInvoker) .ToArray(); - this.activator = LambdaUtil.CreateCtorFunc(proxyClassType); + this.activator = RuntimeFeature.IsDynamicCodeSupported + ? LambdaUtil.CreateCtorFunc(proxyClassType) + : (interceptor, invokers) => proxyClassType.CreateInstance(interceptor, invokers); } /// diff --git a/WebApiClientCore/Implementations/SourceGeneratorProxyClassType.cs b/WebApiClientCore/Implementations/SourceGeneratorProxyClassFinder.cs similarity index 97% rename from WebApiClientCore/Implementations/SourceGeneratorProxyClassType.cs rename to WebApiClientCore/Implementations/SourceGeneratorProxyClassFinder.cs index 44768dee..f14fb4e4 100644 --- a/WebApiClientCore/Implementations/SourceGeneratorProxyClassType.cs +++ b/WebApiClientCore/Implementations/SourceGeneratorProxyClassFinder.cs @@ -7,7 +7,7 @@ namespace WebApiClientCore.Implementations /// /// 提供获取SourceGenerator生成的代理类型 /// - static class SourceGeneratorProxyClassType + static class SourceGeneratorProxyClassFinder { private static readonly object syncRoot = new(); private static readonly HashSet assemblies = [];