Skip to content

Locking perf improvements #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions TildeSql/Internal/Caching/AsyncDuplicateLock.cs

This file was deleted.

42 changes: 0 additions & 42 deletions TildeSql/Internal/Common/AsyncLock.cs

This file was deleted.

40 changes: 18 additions & 22 deletions TildeSql/Internal/QueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Threading;
using System.Threading.Tasks;

using AsyncKeyedLock;

using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;

Expand All @@ -17,7 +19,7 @@
using TildeSql.UnitOfWork;

class QueryEngine : IAsyncDisposable, IDisposable {
private readonly AsyncLock mutex = new();
private readonly AsyncNonKeyedLocker mutex = new();

private readonly ISchema schema;

Expand All @@ -26,7 +28,7 @@ class QueryEngine : IAsyncDisposable, IDisposable {
private readonly UnitOfWork unitOfWork;

private readonly IPersistenceQueryExecutor persistenceQueryExecutor;

private readonly CacheExecutor cacheExecutor;

private readonly CacheSetter cacheSetter;
Expand Down Expand Up @@ -58,12 +60,12 @@ public QueryEngine(
IDistributedCache distributedCache,
ICacheSerializer cacheSerializer,
CacheOptions cacheOptions) {
this.schema = schema;
this.identityMap = identityMap;
this.unitOfWork = unitOfWork;
this.schema = schema;
this.identityMap = identityMap;
this.unitOfWork = unitOfWork;
this.persistenceQueryExecutor = persistenceQueryExecutor;
this.serializer = serializer;
this.identityMapExecutor = new IdentityMapExecutor(this.identityMap, unitOfWork);
this.serializer = serializer;
this.identityMapExecutor = new IdentityMapExecutor(this.identityMap, unitOfWork);
if (memoryCache != null || distributedCache != null) {
this.cacheExecutor = new CacheExecutor(memoryCache, distributedCache, cacheSerializer, cacheOptions);
this.cacheSetter = new CacheSetter(memoryCache, distributedCache, cacheSerializer, cacheOptions);
Expand All @@ -81,20 +83,14 @@ public async IAsyncEnumerable<T> GetResult<T>(IQuery query)
&& !this.identityMapQueries.Contains(query)
&& !(this.cacheExecutorQueries?.Contains(query) ?? false)
&& !this.persistenceQueryExecutorQueries.Contains(query)) {
var @lock = await this.mutex.LockAsync();

try {
// query has not been executed, so let's flush existing queries and then add
await this.FlushPersistenceAsync();
if (!this.queriesToExecute.Contains(query)) {
this.Add(query);
}

await this.ExecuteAsync();
}
finally {
@lock?.Dispose();
using var _ = await this.mutex.LockAsync();
// query has not been executed, so let's flush existing queries and then add
await this.FlushPersistenceAsync();
if (!this.queriesToExecute.Contains(query)) {
this.Add(query);
}

await this.ExecuteAsync();
}

if (this.queryForwardMap.TryGetValue(query, out var queries)) {
Expand Down Expand Up @@ -165,7 +161,7 @@ T HydrateDocument(object[] row) {
}

public async ValueTask EnsureCleanAsync(CancellationToken cancellationToken = default) {
using var @lock = await this.mutex.LockAsync(cancellationToken);
using var _ = await this.mutex.LockAsync(cancellationToken);
await this.FlushPersistenceAsync(); // clear out any non-read queries
await this.ExecuteAsync(cancellationToken); // execute any non-executed queries
await this.FlushPersistenceAsync(); // ensure that they're also read
Expand Down Expand Up @@ -231,7 +227,7 @@ async IAsyncEnumerable<IQuery> ExecuteAgainstCacheAsync() {
// stampede protection, we take a lock on all cacheable queries
var cacheableQueries = queriesStillToExecute.Where(q => q.CacheKey != null).OrderBy(q => q.CacheKey).ToArray();
var locks = new Dictionary<string, IDisposable>();
var locker = new AsyncDuplicateLock();
var locker = new AsyncKeyedLocker<string>(o => o.PoolSize = 0);
try {
foreach (var cacheableQuery in cacheableQueries) {
if (!locks.ContainsKey(cacheableQuery.CacheKey)) {
Expand Down
1 change: 1 addition & 0 deletions TildeSql/TildeSql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AsyncKeyedLock" Version="7.1.6" />
<PackageReference Include="fasterflect" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
Expand Down