Skip to content

Commit

Permalink
Avoid creating a separate Task for lazily initialization of CloudTable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kzu committed May 31, 2021
1 parent 4f7c81a commit db525bd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 106 deletions.
7 changes: 1 addition & 6 deletions .netconfig
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,4 @@
url = https://github.com/devlooped/.github/blob/main/Gemfile
sha = f2dc1370469bec1b2d32d82faf659b050cdc7e2a
etag = d45832acd078778ffebf260000f6d25172a131f51684744d7e982da2a47170ce
weak
[file "src/TableStorage/System/Threading/Tasks/AsyncLazy.cs"]
url = https://github.com/devlooped/catbag/blob/main/System/Threading/Tasks/AsyncLazy.cs
sha = f2a68a668dcbab4ba87c792ed5fd2ca5ca8229de
etag = b6b9f5e7c57e294ba3d59382409171dfa20f220008dbeb725d7b9e2bb4c7aa91
weak
weak
6 changes: 3 additions & 3 deletions src/TableStorage/AttributedTableRepository`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ partial class AttributedTableRepository<T> : TableRepository<T> where T : class
/// </summary>
/// <param name="storageAccount">Storage account to connect to.</param>
public AttributedTableRepository(CloudStorageAccount storageAccount)
: base(storageAccount,
TableRepository.GetDefaultTableName<T>(),
PartitionKeyAttribute.CreateAccessor<T>(),
: base(storageAccount,
TableRepository.GetDefaultTableName<T>(),
PartitionKeyAttribute.CreateAccessor<T>(),
RowKeyAttribute.CreateAccessor<T>())
{
}
Expand Down
83 changes: 0 additions & 83 deletions src/TableStorage/System/Threading/Tasks/AsyncLazy.cs

This file was deleted.

14 changes: 7 additions & 7 deletions src/TableStorage/TableEntityRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Devlooped
partial class TableEntityRepository : ITableRepository<TableEntity>
{
readonly CloudStorageAccount storageAccount;
readonly AsyncLazy<CloudTable> table;
readonly Task<CloudTable> table;

/// <summary>
/// Initializes the table repository.
Expand All @@ -23,7 +23,7 @@ protected internal TableEntityRepository(CloudStorageAccount storageAccount, str
{
this.storageAccount = storageAccount;
TableName = tableName;
table = new AsyncLazy<CloudTable>(() => GetTableAsync(TableName));
table = GetTableAsync(TableName);
}

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

await table.ExecuteAsync(TableOperation.Delete(
new TableEntity(partitionKey, rowKey) { ETag = "*" }), cancellation)
Expand All @@ -42,7 +42,7 @@ await table.ExecuteAsync(TableOperation.Delete(
/// <inheritdoc />
public async Task DeleteAsync(TableEntity entity, CancellationToken cancellation = default)
{
var table = await this.table.Value.ConfigureAwait(false);
var table = await this.table.ConfigureAwait(false);
entity.ETag = "*";
await table.ExecuteAsync(TableOperation.Delete(entity), cancellation)
.ConfigureAwait(false);
Expand All @@ -51,7 +51,7 @@ await table.ExecuteAsync(TableOperation.Delete(entity), cancellation)
/// <inheritdoc />
public async IAsyncEnumerable<TableEntity> EnumerateAsync(string partitionKey, [EnumeratorCancellation] CancellationToken cancellation = default)
{
var table = await this.table.Value;
var table = await this.table;
var query = new TableQuery<TableEntity>()
.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));

Expand All @@ -71,7 +71,7 @@ public async IAsyncEnumerable<TableEntity> EnumerateAsync(string partitionKey, [
/// <inheritdoc />
public async Task<TableEntity?> GetAsync(string partitionKey, string rowKey, CancellationToken cancellation = default)
{
var table = await this.table.Value.ConfigureAwait(false);
var table = await this.table.ConfigureAwait(false);
var result = await table.ExecuteAsync(TableOperation.Retrieve(
partitionKey, rowKey,
(partitionKey, rowKey, timestamp, properties, etag) => new TableEntity(partitionKey, rowKey) { Timestamp = timestamp, ETag = etag }),
Expand All @@ -84,7 +84,7 @@ public async IAsyncEnumerable<TableEntity> EnumerateAsync(string partitionKey, [
/// <inheritdoc />
public async Task<TableEntity> PutAsync(TableEntity entity, CancellationToken cancellation = default)
{
var table = await this.table.Value.ConfigureAwait(false);
var table = await this.table.ConfigureAwait(false);
entity.ETag = "*";
var result = await table.ExecuteAsync(TableOperation.InsertOrReplace(entity), cancellation)
.ConfigureAwait(false);
Expand Down
14 changes: 7 additions & 7 deletions src/TableStorage/TableRepository`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ partial class TableRepository<T> : ITableRepository<T> where T : class
readonly CloudStorageAccount storageAccount;
readonly Func<T, string> partitionKey;
readonly Func<T, string> rowKey;
readonly AsyncLazy<CloudTable> table;
readonly Task<CloudTable> table;

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

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

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

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

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

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

var table = await this.table.Value.ConfigureAwait(false);
var table = await this.table.ConfigureAwait(false);
var values = properties
.ToDictionary(prop => prop.Name, prop => EntityProperty.CreateEntityPropertyFromObject(prop.GetValue(entity)));

Expand Down

0 comments on commit db525bd

Please sign in to comment.