Skip to content

Commit db525bd

Browse files
committed
Avoid creating a separate Task for lazily initialization of CloudTable
By using a direct Task<CloudTable> and awaiting that instead, we don't incur the cost of running a separate Task on the thread pool. Fixes #23
1 parent 4f7c81a commit db525bd

File tree

5 files changed

+18
-106
lines changed

5 files changed

+18
-106
lines changed

.netconfig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,4 @@
151151
url = https://github.com/devlooped/.github/blob/main/Gemfile
152152
sha = f2dc1370469bec1b2d32d82faf659b050cdc7e2a
153153
etag = d45832acd078778ffebf260000f6d25172a131f51684744d7e982da2a47170ce
154-
weak
155-
[file "src/TableStorage/System/Threading/Tasks/AsyncLazy.cs"]
156-
url = https://github.com/devlooped/catbag/blob/main/System/Threading/Tasks/AsyncLazy.cs
157-
sha = f2a68a668dcbab4ba87c792ed5fd2ca5ca8229de
158-
etag = b6b9f5e7c57e294ba3d59382409171dfa20f220008dbeb725d7b9e2bb4c7aa91
159-
weak
154+
weak

src/TableStorage/AttributedTableRepository`1.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ partial class AttributedTableRepository<T> : TableRepository<T> where T : class
2121
/// </summary>
2222
/// <param name="storageAccount">Storage account to connect to.</param>
2323
public AttributedTableRepository(CloudStorageAccount storageAccount)
24-
: base(storageAccount,
25-
TableRepository.GetDefaultTableName<T>(),
26-
PartitionKeyAttribute.CreateAccessor<T>(),
24+
: base(storageAccount,
25+
TableRepository.GetDefaultTableName<T>(),
26+
PartitionKeyAttribute.CreateAccessor<T>(),
2727
RowKeyAttribute.CreateAccessor<T>())
2828
{
2929
}

src/TableStorage/System/Threading/Tasks/AsyncLazy.cs

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/TableStorage/TableEntityRepository.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Devlooped
1212
partial class TableEntityRepository : ITableRepository<TableEntity>
1313
{
1414
readonly CloudStorageAccount storageAccount;
15-
readonly AsyncLazy<CloudTable> table;
15+
readonly Task<CloudTable> table;
1616

1717
/// <summary>
1818
/// Initializes the table repository.
@@ -23,7 +23,7 @@ protected internal TableEntityRepository(CloudStorageAccount storageAccount, str
2323
{
2424
this.storageAccount = storageAccount;
2525
TableName = tableName;
26-
table = new AsyncLazy<CloudTable>(() => GetTableAsync(TableName));
26+
table = GetTableAsync(TableName);
2727
}
2828

2929
/// <inheritdoc />
@@ -32,7 +32,7 @@ protected internal TableEntityRepository(CloudStorageAccount storageAccount, str
3232
/// <inheritdoc />
3333
public async Task DeleteAsync(string partitionKey, string rowKey, CancellationToken cancellation = default)
3434
{
35-
var table = await this.table.Value.ConfigureAwait(false);
35+
var table = await this.table.ConfigureAwait(false);
3636

3737
await table.ExecuteAsync(TableOperation.Delete(
3838
new TableEntity(partitionKey, rowKey) { ETag = "*" }), cancellation)
@@ -42,7 +42,7 @@ await table.ExecuteAsync(TableOperation.Delete(
4242
/// <inheritdoc />
4343
public async Task DeleteAsync(TableEntity entity, CancellationToken cancellation = default)
4444
{
45-
var table = await this.table.Value.ConfigureAwait(false);
45+
var table = await this.table.ConfigureAwait(false);
4646
entity.ETag = "*";
4747
await table.ExecuteAsync(TableOperation.Delete(entity), cancellation)
4848
.ConfigureAwait(false);
@@ -51,7 +51,7 @@ await table.ExecuteAsync(TableOperation.Delete(entity), cancellation)
5151
/// <inheritdoc />
5252
public async IAsyncEnumerable<TableEntity> EnumerateAsync(string partitionKey, [EnumeratorCancellation] CancellationToken cancellation = default)
5353
{
54-
var table = await this.table.Value;
54+
var table = await this.table;
5555
var query = new TableQuery<TableEntity>()
5656
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
5757

@@ -71,7 +71,7 @@ public async IAsyncEnumerable<TableEntity> EnumerateAsync(string partitionKey, [
7171
/// <inheritdoc />
7272
public async Task<TableEntity?> GetAsync(string partitionKey, string rowKey, CancellationToken cancellation = default)
7373
{
74-
var table = await this.table.Value.ConfigureAwait(false);
74+
var table = await this.table.ConfigureAwait(false);
7575
var result = await table.ExecuteAsync(TableOperation.Retrieve(
7676
partitionKey, rowKey,
7777
(partitionKey, rowKey, timestamp, properties, etag) => new TableEntity(partitionKey, rowKey) { Timestamp = timestamp, ETag = etag }),
@@ -84,7 +84,7 @@ public async IAsyncEnumerable<TableEntity> EnumerateAsync(string partitionKey, [
8484
/// <inheritdoc />
8585
public async Task<TableEntity> PutAsync(TableEntity entity, CancellationToken cancellation = default)
8686
{
87-
var table = await this.table.Value.ConfigureAwait(false);
87+
var table = await this.table.ConfigureAwait(false);
8888
entity.ETag = "*";
8989
var result = await table.ExecuteAsync(TableOperation.InsertOrReplace(entity), cancellation)
9090
.ConfigureAwait(false);

src/TableStorage/TableRepository`1.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ partial class TableRepository<T> : ITableRepository<T> where T : class
2525
readonly CloudStorageAccount storageAccount;
2626
readonly Func<T, string> partitionKey;
2727
readonly Func<T, string> rowKey;
28-
readonly AsyncLazy<CloudTable> table;
28+
readonly Task<CloudTable> table;
2929

3030
/// <summary>
3131
/// Initializes the table repository.
@@ -40,7 +40,7 @@ protected internal TableRepository(CloudStorageAccount storageAccount, string ta
4040
TableName = tableName ?? TableRepository.GetDefaultTableName<T>();
4141
this.partitionKey = partitionKey ?? PartitionKeyAttribute.CreateAccessor<T>();
4242
this.rowKey = rowKey ?? RowKeyAttribute.CreateAccessor<T>();
43-
table = new AsyncLazy<CloudTable>(() => GetTableAsync(TableName));
43+
table = GetTableAsync(TableName);
4444
}
4545

4646
/// <inheritdoc />
@@ -49,7 +49,7 @@ protected internal TableRepository(CloudStorageAccount storageAccount, string ta
4949
/// <inheritdoc />
5050
public async Task DeleteAsync(string partitionKey, string rowKey, CancellationToken cancellation = default)
5151
{
52-
var table = await this.table.Value.ConfigureAwait(false);
52+
var table = await this.table.ConfigureAwait(false);
5353

5454
await table.ExecuteAsync(TableOperation.Delete(
5555
new TableEntity(partitionKey, rowKey) { ETag = "*" }), cancellation)
@@ -61,7 +61,7 @@ public async Task DeleteAsync(T entity, CancellationToken cancellation = default
6161
{
6262
var partitionKey = this.partitionKey.Invoke(entity);
6363
var rowKey = this.rowKey.Invoke(entity);
64-
var table = await this.table.Value.ConfigureAwait(false);
64+
var table = await this.table.ConfigureAwait(false);
6565

6666
await table.ExecuteAsync(TableOperation.Delete(
6767
new TableEntity(partitionKey, rowKey) { ETag = "*" }), cancellation)
@@ -71,7 +71,7 @@ await table.ExecuteAsync(TableOperation.Delete(
7171
/// <inheritdoc />
7272
public async IAsyncEnumerable<T> EnumerateAsync(string partitionKey, [EnumeratorCancellation] CancellationToken cancellation = default)
7373
{
74-
var table = await this.table.Value;
74+
var table = await this.table;
7575
var query = new TableQuery<DynamicTableEntity>()
7676
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
7777

@@ -91,7 +91,7 @@ public async IAsyncEnumerable<T> EnumerateAsync(string partitionKey, [Enumerator
9191
/// <inheritdoc />
9292
public async Task<T?> GetAsync(string partitionKey, string rowKey, CancellationToken cancellation = default)
9393
{
94-
var table = await this.table.Value.ConfigureAwait(false);
94+
var table = await this.table.ConfigureAwait(false);
9595
var result = await table.ExecuteAsync(TableOperation.Retrieve(partitionKey, rowKey), cancellation)
9696
.ConfigureAwait(false);
9797

@@ -111,7 +111,7 @@ public async Task<T> PutAsync(T entity, CancellationToken cancellation = default
111111
.Where(prop => prop.GetCustomAttribute<BrowsableAttribute>()?.Browsable != false)
112112
.ToArray());
113113

114-
var table = await this.table.Value.ConfigureAwait(false);
114+
var table = await this.table.ConfigureAwait(false);
115115
var values = properties
116116
.ToDictionary(prop => prop.Name, prop => EntityProperty.CreateEntityPropertyFromObject(prop.GetValue(entity)));
117117

0 commit comments

Comments
 (0)