Skip to content

Commit 3a9cc89

Browse files
Added pooling to locking to reduce memory allocations; ConfigureAwait(false) (#542) (#581)
Co-authored-by: Mark Cilia Vincenti <markciliavincenti@gmail.com>
1 parent f1ce89c commit 3a9cc89

22 files changed

+57
-42
lines changed

tools/code/publisher/Api.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where Common.SpecificationFileNames.Contains(file.Name)
8080
/// </summary>
8181
file sealed class ApiSemaphore : IDisposable
8282
{
83-
private readonly AsyncKeyedLocker<ApiName> locker = new();
83+
private readonly AsyncKeyedLocker<ApiName> locker = new(LockOptions.Default);
8484
private ImmutableHashSet<ApiName> processedNames = [];
8585

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

9494
// Only process each name once
9595
if (processedNames.Contains(name))

tools/code/publisher/ApiOperationPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ from policyFile in ApiOperationPolicyFile.TryParse(file, serviceDirectory)
5656
/// </summary>
5757
file sealed class ApiOperationPolicySemaphore : IDisposable
5858
{
59-
private readonly AsyncKeyedLocker<(ApiOperationPolicyName, ApiOperationName, ApiName)> locker = new();
59+
private readonly AsyncKeyedLocker<(ApiOperationPolicyName, ApiOperationName, ApiName)> locker = new(LockOptions.Default);
6060
private ImmutableHashSet<(ApiOperationPolicyName, ApiOperationName, ApiName)> processedNames = [];
6161

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

7070
// Only process each name once
7171
if (processedNames.Contains((name, apiOperationName, apiName)))

tools/code/publisher/ApiPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ from policyFile in ApiPolicyFile.TryParse(file, serviceDirectory)
5656
/// </summary>
5757
file sealed class ApiPolicySemaphore : IDisposable
5858
{
59-
private readonly AsyncKeyedLocker<(ApiPolicyName, ApiName)> locker = new();
59+
private readonly AsyncKeyedLocker<(ApiPolicyName, ApiName)> locker = new(LockOptions.Default);
6060
private ImmutableHashSet<(ApiPolicyName, ApiName)> processedNames = [];
6161

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

7070
// Only process each name once
7171
if (processedNames.Contains((name, apiName)))

tools/code/publisher/ApiTag.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ from informationFile in ApiTagInformationFile.TryParse(file, serviceDirectory)
5656
/// </summary>
5757
file sealed class ApiTagSemaphore : IDisposable
5858
{
59-
private readonly AsyncKeyedLocker<(TagName, ApiName)> locker = new();
59+
private readonly AsyncKeyedLocker<(TagName, ApiName)> locker = new(LockOptions.Default);
6060
private ImmutableHashSet<(TagName, ApiName)> processedNames = [];
6161

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

7070
// Only process each name once
7171
if (processedNames.Contains((name, apiName)))

tools/code/publisher/Backend.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ from informationFile in BackendInformationFile.TryParse(file, serviceDirectory)
8282
/// </summary>
8383
file sealed class BackendSemaphore : IDisposable
8484
{
85-
private readonly AsyncKeyedLocker<BackendName> locker = new();
85+
private readonly AsyncKeyedLocker<BackendName> locker = new(LockOptions.Default);
8686
private ImmutableHashSet<BackendName> processedNames = [];
8787

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

9696
// Only process each name once
9797
if (processedNames.Contains(name))

tools/code/publisher/Diagnostic.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ from informationFile in DiagnosticInformationFile.TryParse(file, serviceDirector
6161
/// </summary>
6262
file sealed class DiagnosticSemaphore : IDisposable
6363
{
64-
private readonly AsyncKeyedLocker<DiagnosticName> locker = new();
64+
private readonly AsyncKeyedLocker<DiagnosticName> locker = new(LockOptions.Default);
6565
private ImmutableHashSet<DiagnosticName> processedNames = [];
6666

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

7575
// Only process each name once
7676
if (processedNames.Contains(name))

tools/code/publisher/Gateway.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ from informationFile in GatewayInformationFile.TryParse(file, serviceDirectory)
5959
/// </summary>
6060
file sealed class GatewaySemaphore : IDisposable
6161
{
62-
private readonly AsyncKeyedLocker<GatewayName> locker = new();
62+
private readonly AsyncKeyedLocker<GatewayName> locker = new(LockOptions.Default);
6363
private ImmutableHashSet<GatewayName> processedNames = [];
6464

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

7373
// Only process each name once
7474
if (processedNames.Contains(name))

tools/code/publisher/GatewayApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ from informationFile in GatewayApiInformationFile.TryParse(file, serviceDirector
5656
/// </summary>
5757
file sealed class GatewayApiSemaphore : IDisposable
5858
{
59-
private readonly AsyncKeyedLocker<(ApiName, GatewayName)> locker = new();
59+
private readonly AsyncKeyedLocker<(ApiName, GatewayName)> locker = new(LockOptions.Default);
6060
private ImmutableHashSet<(ApiName, GatewayName)> processedNames = [];
6161

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

7070
// Only process each name once
7171
if (processedNames.Contains((name, gatewayName)))

tools/code/publisher/Group.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ from informationFile in GroupInformationFile.TryParse(file, serviceDirectory)
5959
/// </summary>
6060
file sealed class GroupSemaphore : IDisposable
6161
{
62-
private readonly AsyncKeyedLocker<GroupName> locker = new();
62+
private readonly AsyncKeyedLocker<GroupName> locker = new(LockOptions.Default);
6363
private ImmutableHashSet<GroupName> processedNames = [];
6464

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

7373
// Only process each name once
7474
if (processedNames.Contains(name))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AsyncKeyedLock;
2+
3+
namespace publisher
4+
{
5+
internal static class LockOptions
6+
{
7+
private readonly static AsyncKeyedLockOptions defaultOptions = new()
8+
{
9+
PoolSize = 20,
10+
PoolInitialFill = 1
11+
};
12+
13+
internal static AsyncKeyedLockOptions Default => defaultOptions;
14+
}
15+
}

0 commit comments

Comments
 (0)