Skip to content

Commit

Permalink
Use value task instead of task for the allocation saving.
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairjevans committed Jul 22, 2020
1 parent 6158bdc commit b122d2c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/Autofac/Builder/IRegistrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public interface IRegistrationBuilder<out TLimit, out TActivatorData, out TRegis
/// </summary>
/// <param name="handler">An event handler; the resolve process will not continue until the returned task completes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPreparing(Func<PreparingEventArgs, Task> handler);
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPreparing(Func<PreparingEventArgs, ValueTask> handler);

/// <summary>
/// Add a handler for the Activating event.
Expand All @@ -278,7 +278,7 @@ public interface IRegistrationBuilder<out TLimit, out TActivatorData, out TRegis
/// </summary>
/// <param name="handler">An event handler; the resolve process will not continue until the returned task completes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivating(Func<IActivatingEventArgs<TLimit>, Task> handler);
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivating(Func<IActivatingEventArgs<TLimit>, ValueTask> handler);

/// <summary>
/// Add a handler for the Activated event.
Expand All @@ -292,7 +292,7 @@ public interface IRegistrationBuilder<out TLimit, out TActivatorData, out TRegis
/// </summary>
/// <param name="handler">An event handler; the resolve process will not continue until the returned task completes.</param>
/// <returns>A registration builder allowing further configuration of the component.</returns>
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivated(Func<IActivatedEventArgs<TLimit>, Task> handler);
IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivated(Func<IActivatedEventArgs<TLimit>, ValueTask> handler);

/// <summary>
/// Configure the component so that any properties whose types are registered in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,22 @@ public RegistrationBuilder(Service defaultService, TActivatorData activatorData,
}

/// <inheritdoc/>
public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPreparing(Func<PreparingEventArgs, Task> handler)
public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnPreparing(Func<PreparingEventArgs, ValueTask> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}

return OnPreparing(args => handler(args).ConfigureAwait(false).GetAwaiter().GetResult());
return OnPreparing(args =>
{
var vt = handler(args);
if (!vt.IsCompletedSuccessfully)
{
vt.ConfigureAwait(false).GetAwaiter().GetResult();
}
});
}

/// <summary>
Expand Down Expand Up @@ -457,14 +465,22 @@ public RegistrationBuilder(Service defaultService, TActivatorData activatorData,
}

/// <inheritdoc/>
public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivating(Func<IActivatingEventArgs<TLimit>, Task> handler)
public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivating(Func<IActivatingEventArgs<TLimit>, ValueTask> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}

return OnActivating(args => handler(args).ConfigureAwait(false).GetAwaiter().GetResult());
return OnActivating(args =>
{
var vt = handler(args);
if (!vt.IsCompletedSuccessfully)
{
vt.ConfigureAwait(false).GetAwaiter().GetResult();
}
});
}

/// <summary>
Expand Down Expand Up @@ -508,14 +524,22 @@ public RegistrationBuilder(Service defaultService, TActivatorData activatorData,
}

/// <inheritdoc/>
public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivated(Func<IActivatedEventArgs<TLimit>, Task> handler)
public IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> OnActivated(Func<IActivatedEventArgs<TLimit>, ValueTask> handler)
{
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}

return OnActivated(args => handler(args).ConfigureAwait(false).GetAwaiter().GetResult());
return OnActivated(args =>
{
var vt = handler(args);
if (!vt.IsCompletedSuccessfully)
{
vt.ConfigureAwait(false).GetAwaiter().GetResult();
}
});
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/RegistrationExtensions.EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static partial class RegistrationExtensions
public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle>
OnRelease<TLimit, TActivatorData, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registration,
Func<TLimit, Task> releaseAction)
Func<TLimit, ValueTask> releaseAction)
{
if (registration == null) throw new ArgumentNullException(nameof(registration));
if (releaseAction == null) throw new ArgumentNullException(nameof(releaseAction));
Expand Down
11 changes: 8 additions & 3 deletions src/Autofac/Util/AsyncReleaseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Autofac.Util
/// </summary>
internal class AsyncReleaseAction<TLimit> : Disposable
{
private readonly Func<TLimit, Task> _action;
private readonly Func<TLimit, ValueTask> _action;
private readonly Func<TLimit> _factory;

/// <summary>
Expand All @@ -24,7 +24,7 @@ internal class AsyncReleaseAction<TLimit> : Disposable
/// A factory that retrieves the value on which the <paramref name="action" />
/// should be executed.
/// </param>
public AsyncReleaseAction(Func<TLimit, Task> action, Func<TLimit> factory)
public AsyncReleaseAction(Func<TLimit, ValueTask> action, Func<TLimit> factory)
{
if (action == null) throw new ArgumentNullException(nameof(action));
if (factory == null) throw new ArgumentNullException(nameof(factory));
Expand Down Expand Up @@ -55,7 +55,12 @@ protected override void Dispose(bool disposing)
// during .OnActivating() will be accounted for.
if (disposing)
{
_action(_factory()).ConfigureAwait(false).GetAwaiter().GetResult();
var vt = _action(_factory());

if (!vt.IsCompletedSuccessfully)
{
vt.ConfigureAwait(false).GetAwaiter().GetResult();
}
}

base.Dispose(disposing);
Expand Down

0 comments on commit b122d2c

Please sign in to comment.