Skip to content

Commit

Permalink
Added pooling to locking to reduce memory allocations; ConfigureAwait…
Browse files Browse the repository at this point in the history
…(false) (#542) (#581)

Co-authored-by: Mark Cilia Vincenti <[email protected]>
  • Loading branch information
guythetechie and MarkCiliaVincenti authored Jul 5, 2024
1 parent f1ce89c commit 3a9cc89
Show file tree
Hide file tree
Showing 22 changed files with 57 additions and 42 deletions.
4 changes: 2 additions & 2 deletions tools/code/publisher/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where Common.SpecificationFileNames.Contains(file.Name)
/// </summary>
file sealed class ApiSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<ApiName> locker = new();
private readonly AsyncKeyedLocker<ApiName> locker = new(LockOptions.Default);
private ImmutableHashSet<ApiName> processedNames = [];

/// <summary>
Expand All @@ -89,7 +89,7 @@ where Common.SpecificationFileNames.Contains(file.Name)
public async ValueTask Run(ApiName name, Func<ApiName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ApiOperationPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from policyFile in ApiOperationPolicyFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class ApiOperationPolicySemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(ApiOperationPolicyName, ApiOperationName, ApiName)> locker = new();
private readonly AsyncKeyedLocker<(ApiOperationPolicyName, ApiOperationName, ApiName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(ApiOperationPolicyName, ApiOperationName, ApiName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from policyFile in ApiOperationPolicyFile.TryParse(file, serviceDirectory)
public async ValueTask Run(ApiOperationPolicyName name, ApiOperationName apiOperationName, ApiName apiName, Func<ApiOperationPolicyName, ApiOperationName, ApiName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, apiOperationName, apiName), cancellationToken);
using var _ = await locker.LockAsync((name, apiOperationName, apiName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, apiOperationName, apiName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ApiPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from policyFile in ApiPolicyFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class ApiPolicySemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(ApiPolicyName, ApiName)> locker = new();
private readonly AsyncKeyedLocker<(ApiPolicyName, ApiName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(ApiPolicyName, ApiName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from policyFile in ApiPolicyFile.TryParse(file, serviceDirectory)
public async ValueTask Run(ApiPolicyName name, ApiName apiName, Func<ApiPolicyName, ApiName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, apiName), cancellationToken);
using var _ = await locker.LockAsync((name, apiName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, apiName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ApiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from informationFile in ApiTagInformationFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class ApiTagSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(TagName, ApiName)> locker = new();
private readonly AsyncKeyedLocker<(TagName, ApiName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(TagName, ApiName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from informationFile in ApiTagInformationFile.TryParse(file, serviceDirectory)
public async ValueTask Run(TagName name, ApiName apiName, Func<TagName, ApiName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, apiName), cancellationToken);
using var _ = await locker.LockAsync((name, apiName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, apiName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/Backend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ from informationFile in BackendInformationFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class BackendSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<BackendName> locker = new();
private readonly AsyncKeyedLocker<BackendName> locker = new(LockOptions.Default);
private ImmutableHashSet<BackendName> processedNames = [];

/// <summary>
Expand All @@ -91,7 +91,7 @@ from informationFile in BackendInformationFile.TryParse(file, serviceDirectory)
public async ValueTask Run(BackendName name, Func<BackendName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/Diagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ from informationFile in DiagnosticInformationFile.TryParse(file, serviceDirector
/// </summary>
file sealed class DiagnosticSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<DiagnosticName> locker = new();
private readonly AsyncKeyedLocker<DiagnosticName> locker = new(LockOptions.Default);
private ImmutableHashSet<DiagnosticName> processedNames = [];

/// <summary>
Expand All @@ -70,7 +70,7 @@ from informationFile in DiagnosticInformationFile.TryParse(file, serviceDirector
public async ValueTask Run(DiagnosticName name, Func<DiagnosticName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ from informationFile in GatewayInformationFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class GatewaySemaphore : IDisposable
{
private readonly AsyncKeyedLocker<GatewayName> locker = new();
private readonly AsyncKeyedLocker<GatewayName> locker = new(LockOptions.Default);
private ImmutableHashSet<GatewayName> processedNames = [];

/// <summary>
Expand All @@ -68,7 +68,7 @@ from informationFile in GatewayInformationFile.TryParse(file, serviceDirectory)
public async ValueTask Run(GatewayName name, Func<GatewayName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/GatewayApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from informationFile in GatewayApiInformationFile.TryParse(file, serviceDirector
/// </summary>
file sealed class GatewayApiSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(ApiName, GatewayName)> locker = new();
private readonly AsyncKeyedLocker<(ApiName, GatewayName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(ApiName, GatewayName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from informationFile in GatewayApiInformationFile.TryParse(file, serviceDirector
public async ValueTask Run(ApiName name, GatewayName gatewayName, Func<ApiName, GatewayName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, gatewayName), cancellationToken);
using var _ = await locker.LockAsync((name, gatewayName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, gatewayName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ from informationFile in GroupInformationFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class GroupSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<GroupName> locker = new();
private readonly AsyncKeyedLocker<GroupName> locker = new(LockOptions.Default);
private ImmutableHashSet<GroupName> processedNames = [];

/// <summary>
Expand All @@ -68,7 +68,7 @@ from informationFile in GroupInformationFile.TryParse(file, serviceDirectory)
public async ValueTask Run(GroupName name, Func<GroupName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
15 changes: 15 additions & 0 deletions tools/code/publisher/LockOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AsyncKeyedLock;

namespace publisher
{
internal static class LockOptions
{
private readonly static AsyncKeyedLockOptions defaultOptions = new()
{
PoolSize = 20,
PoolInitialFill = 1
};

internal static AsyncKeyedLockOptions Default => defaultOptions;
}
}
4 changes: 2 additions & 2 deletions tools/code/publisher/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ from informationFile in LoggerInformationFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class LoggerSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<LoggerName> locker = new();
private readonly AsyncKeyedLocker<LoggerName> locker = new(LockOptions.Default);
private ImmutableHashSet<LoggerName> processedNames = [];

/// <summary>
Expand All @@ -68,7 +68,7 @@ from informationFile in LoggerInformationFile.TryParse(file, serviceDirectory)
public async ValueTask Run(LoggerName name, Func<LoggerName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/NamedValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private async ValueTask Put(NamedValueName name, CancellationToken cancellationT
/// </summary>
file sealed class NamedValueSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<NamedValueName> locker = new();
private readonly AsyncKeyedLocker<NamedValueName> locker = new(LockOptions.Default);
private ImmutableHashSet<NamedValueName> processedNames = [];

/// <summary>
Expand All @@ -113,7 +113,7 @@ private async ValueTask Put(NamedValueName name, CancellationToken cancellationT
public async ValueTask Run(NamedValueName name, Func<NamedValueName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/PolicyFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ from policyFile in PolicyFragmentPolicyFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class PolicyFragmentSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<PolicyFragmentName> locker = new();
private readonly AsyncKeyedLocker<PolicyFragmentName> locker = new(LockOptions.Default);
private ImmutableHashSet<PolicyFragmentName> processedNames = [];

/// <summary>
Expand All @@ -101,7 +101,7 @@ from policyFile in PolicyFragmentPolicyFile.TryParse(file, serviceDirectory)
public async ValueTask Run(PolicyFragmentName name, Func<PolicyFragmentName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ from informationFile in ProductInformationFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class ProductSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<ProductName> locker = new();
private readonly AsyncKeyedLocker<ProductName> locker = new(LockOptions.Default);
private ImmutableHashSet<ProductName> processedNames = [];

/// <summary>
Expand All @@ -69,7 +69,7 @@ from informationFile in ProductInformationFile.TryParse(file, serviceDirectory)
public async ValueTask Run(ProductName name, Func<ProductName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ProductApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from informationFile in ProductApiInformationFile.TryParse(file, serviceDirector
/// </summary>
file sealed class ProductApiSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(ApiName, ProductName)> locker = new();
private readonly AsyncKeyedLocker<(ApiName, ProductName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(ApiName, ProductName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from informationFile in ProductApiInformationFile.TryParse(file, serviceDirector
public async ValueTask Run(ApiName name, ProductName productName, Func<ApiName, ProductName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, productName), cancellationToken);
using var _ = await locker.LockAsync((name, productName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, productName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ProductGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from informationFile in ProductGroupInformationFile.TryParse(file, serviceDirect
/// </summary>
file sealed class ProductGroupSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(GroupName, ProductName)> locker = new();
private readonly AsyncKeyedLocker<(GroupName, ProductName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(GroupName, ProductName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from informationFile in ProductGroupInformationFile.TryParse(file, serviceDirect
public async ValueTask Run(GroupName name, ProductName productName, Func<GroupName, ProductName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, productName), cancellationToken);
using var _ = await locker.LockAsync((name, productName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, productName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ProductPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from policyFile in ProductPolicyFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class ProductPolicySemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(ProductPolicyName, ProductName)> locker = new();
private readonly AsyncKeyedLocker<(ProductPolicyName, ProductName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(ProductPolicyName, ProductName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from policyFile in ProductPolicyFile.TryParse(file, serviceDirectory)
public async ValueTask Run(ProductPolicyName name, ProductName productName, Func<ProductPolicyName, ProductName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, productName), cancellationToken);
using var _ = await locker.LockAsync((name, productName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, productName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ProductTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from informationFile in ProductTagInformationFile.TryParse(file, serviceDirector
/// </summary>
file sealed class ProductTagSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<(TagName, ProductName)> locker = new();
private readonly AsyncKeyedLocker<(TagName, ProductName)> locker = new(LockOptions.Default);
private ImmutableHashSet<(TagName, ProductName)> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from informationFile in ProductTagInformationFile.TryParse(file, serviceDirector
public async ValueTask Run(TagName name, ProductName productName, Func<TagName, ProductName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync((name, productName), cancellationToken);
using var _ = await locker.LockAsync((name, productName), cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains((name, productName)))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/ServicePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ from policyFile in ServicePolicyFile.TryParse(file, serviceDirectory)
/// </summary>
file sealed class ServicePolicySemaphore : IDisposable
{
private readonly AsyncKeyedLocker<ServicePolicyName> locker = new();
private readonly AsyncKeyedLocker<ServicePolicyName> locker = new(LockOptions.Default);
private ImmutableHashSet<ServicePolicyName> processedNames = [];

/// <summary>
Expand All @@ -65,7 +65,7 @@ from policyFile in ServicePolicyFile.TryParse(file, serviceDirectory)
public async ValueTask Run(ServicePolicyName name, Func<ServicePolicyName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
4 changes: 2 additions & 2 deletions tools/code/publisher/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ from informationFile in SubscriptionInformationFile.TryParse(file, serviceDirect
/// </summary>
file sealed class SubscriptionSemaphore : IDisposable
{
private readonly AsyncKeyedLocker<SubscriptionName> locker = new();
private readonly AsyncKeyedLocker<SubscriptionName> locker = new(LockOptions.Default);
private ImmutableHashSet<SubscriptionName> processedNames = [];

/// <summary>
Expand All @@ -70,7 +70,7 @@ from informationFile in SubscriptionInformationFile.TryParse(file, serviceDirect
public async ValueTask Run(SubscriptionName name, Func<SubscriptionName, CancellationToken, ValueTask> action, CancellationToken cancellationToken)
{
// Do not process the same name simultaneously
using var _ = await locker.LockAsync(name, cancellationToken);
using var _ = await locker.LockAsync(name, cancellationToken).ConfigureAwait(false);

// Only process each name once
if (processedNames.Contains(name))
Expand Down
Loading

0 comments on commit 3a9cc89

Please sign in to comment.