Skip to content

Commit

Permalink
Fix regression
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Jun 28, 2024
1 parent 215b32b commit 41b6e2f
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/HyperSharp.Responders/ResponderCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public ResponderDelegate<TContext, TOutput> CompileResponders<TContext, TOutput>
private ResponderDelegate<TContext, TOutput> CompileDependency<TContext, TOutput>(IServiceProvider serviceProvider, ResponderBuilder builder)
{
// ActivatorUtilities throws an exception if the type has no constructors (structs)
IResponder<TContext, TOutput> responder = (IResponder<TContext, TOutput>)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, builder.Type);
IResponder<TContext, TOutput> responder = (IResponder<TContext, TOutput>)GetOrCreateInstance(serviceProvider, builder.Type);
ResponderDelegate<TContext, TOutput> responderDelegate = responder.Respond;
if (builder.Dependencies.Count == 0)
{
Expand Down Expand Up @@ -285,12 +285,12 @@ private ValueTaskResponderDelegate<TContext, TOutput> CompileAsyncDependency<TCo
ValueTaskResponderDelegate<TContext, TOutput> responderDelegate;
if (typeof(ITaskResponder).IsAssignableFrom(builder.Type))
{
ITaskResponder<TContext, TOutput> taskResponder = (ITaskResponder<TContext, TOutput>)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, builder.Type);
ITaskResponder<TContext, TOutput> taskResponder = (ITaskResponder<TContext, TOutput>)GetOrCreateInstance(serviceProvider, builder.Type);
responderDelegate = async ValueTask<Result<TOutput>> (context, cancellationToken) => await taskResponder.RespondAsync(context, cancellationToken);
}
else
{
IValueTaskResponder<TContext, TOutput> responder = (IValueTaskResponder<TContext, TOutput>)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, builder.Type);
IValueTaskResponder<TContext, TOutput> responder = (IValueTaskResponder<TContext, TOutput>)GetOrCreateInstance(serviceProvider, builder.Type);
responderDelegate = responder.RespondAsync;
}

Expand Down Expand Up @@ -337,5 +337,19 @@ private ValueTaskResponderDelegate<TContext, TOutput> CompileAsyncDependency<TCo
}
};
}

private static object GetOrCreateInstance(IServiceProvider serviceProvider, Type type)
{
object? instance = serviceProvider.GetService(type);
if (instance is not null)
{
return instance;
}

ConstructorInfo[] constructors = type.GetConstructors();
return constructors.Length == 0 || constructors.Any(constructor => constructor.GetParameters().Length == 0)
? Activator.CreateInstance(type)!
: ActivatorUtilities.CreateInstance(serviceProvider, type);
}
}
}

1 comment on commit 41b6e2f

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Machine Information:

BenchmarkDotNet v0.13.12, Ubuntu 22.04.4 LTS (Jammy Jellyfish)

  • AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
  • Hardware Intrinsics: AVX2, AES, BMI1, BMI2, FMA, LZCNT, PCLMUL, POPCNT VectorSize=256
  • .NET 8.0.7 (8.0.724.27014), x64, RyuJIT
  • Total Execution Time: 50.102s

HyperBenchmarks

Execution Time: 48.644s

FullHttpRequest:

Mean: 379.27μs
Error: 360ns
StdDev: 1.39μs
Max per second: 2,636.62 (1,000,000,000ns / 379,273.51ns)

HyperContextRespondAsync:

Mean: 13.46μs
Error: 54ns
StdDev: 209ns
Max per second: 74,307.43 (1,000,000,000ns / 13,457.61ns)

ParseHeadersTestAsync:

Mean: 6.11μs
Error: 11ns
StdDev: 42ns
Max per second: 163,675.39 (1,000,000,000ns / 6,109.65ns)

HttpBenchmarks

Execution Time: 1.458s

HyperSharpTestAsync, Baseline, Failed:

No results.

EmbedIoTestAsync, Failed:

No results.

GenHttpTestAsync, Failed:

No results.

HttpCoreTestAsync, Failed:

No results.

HttpListenerTestAsync, Failed:

No results.

Please sign in to comment.