Skip to content

Commit

Permalink
Use ValueTask to go Faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryCordewener committed Nov 15, 2024
1 parent 089ecad commit c9f13c9
Show file tree
Hide file tree
Showing 43 changed files with 213 additions and 213 deletions.
4 changes: 2 additions & 2 deletions Core.Arango.Tests/FoxxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ await Arango.Foxx.InstallServiceAsync("test", "/sample/service", ArangoFoxxSourc
Assert.Equal("/sample/service", services.First().Mount);
}

private async Task<Stream> BuildService(string response)
private async ValueTask<Stream> BuildService(string response)
{
var ms = new MemoryStream();
using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, true, Encoding.UTF8))
Expand Down Expand Up @@ -110,7 +110,7 @@ await Arango.Foxx.InstallServiceAsync("test", "/sample/service",

await Arango.Foxx.ReplaceConfigurationAsync("test", "/sample/service", new
{
currency = "",
currency = "",
secretKey = "s3cr3t"
});

Expand Down
2 changes: 1 addition & 1 deletion Core.Arango.Tests/TransactionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task StreamTransactionQuery(string serializer)
Assert.Equal(3, (await Arango.Query.FindAsync<Entity>("test", "test", $"true")).Count);

var exception =
await Assert.ThrowsAsync<ArangoException>(() => Arango.Query.FindAsync<Entity>(t2, "test", $"true"));
await Assert.ThrowsAsync<ArangoException>(() => Arango.Query.FindAsync<Entity>(t2, "test", $"true").AsTask());

Assert.NotNull(exception.ErrorNumber);
Assert.NotNull(exception.Code);
Expand Down
4 changes: 2 additions & 2 deletions Core.Arango/ArangoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public ArangoContext(string cs, IArangoConfiguration settings = null)
public IArangoPregelModule Pregel { get; }

/// <inheritdoc />
public async Task<ArangoVersion> GetVersionAsync(CancellationToken cancellationToken = default)
public async ValueTask<ArangoVersion> GetVersionAsync(CancellationToken cancellationToken = default)
{
var res = await Configuration.Transport.SendAsync<ArangoVersion>(HttpMethod.Get,
"/_db/_system/_api/version",
Expand All @@ -123,7 +123,7 @@ public async Task<ArangoVersion> GetVersionAsync(CancellationToken cancellationT
}

/// <inheritdoc />
public async Task<IReadOnlyCollection<string>> GetEndpointsAsync(CancellationToken cancellationToken = default)
public async ValueTask<IReadOnlyCollection<string>> GetEndpointsAsync(CancellationToken cancellationToken = default)
{
var res = await Configuration.Transport.SendAsync<EndpointResponse>(HttpMethod.Get,
"/_api/cluster/endpoints", cancellationToken: cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions Core.Arango/IArangoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public interface IArangoContext
/// <summary>
/// Get Arango server version and license
/// </summary>
Task<ArangoVersion> GetVersionAsync(CancellationToken cancellationToken = default);
ValueTask<ArangoVersion> GetVersionAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Get Arango cluster endpoints
/// </summary>
Task<IReadOnlyCollection<string>> GetEndpointsAsync(CancellationToken cancellationToken = default);
ValueTask<IReadOnlyCollection<string>> GetEndpointsAsync(CancellationToken cancellationToken = default);
}
}
22 changes: 11 additions & 11 deletions Core.Arango/Linq/ArangoQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@ public static (string aql, IDictionary<string, object> bindVars) ToAql<T>(this I
return (data.Query.Trim(), data.BindVars);
}

public static Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource> FirstAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
{
return FirstOrDefaultAsync(source, false, null, cancellationToken);
}

public static Task<TSource> FirstAsync<TSource>(this IQueryable<TSource> source,
public static ValueTask<TSource> FirstAsync<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
{
return FirstOrDefaultAsync(source, false, predicate, cancellationToken);
}

public static Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
{
return FirstOrDefaultAsync(source, true, null, cancellationToken);
}

public static Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source,
public static ValueTask<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
{
return FirstOrDefaultAsync(source, true, predicate, cancellationToken);
}

private static Task<T> FirstOrDefaultAsync<T>(this IQueryable<T> source, bool returnDefaultWhenEmpty,
private static ValueTask<T> FirstOrDefaultAsync<T>(this IQueryable<T> source, bool returnDefaultWhenEmpty,
Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
{
if (predicate != null)
Expand All @@ -85,29 +85,29 @@ private static Task<T> FirstOrDefaultAsync<T>(this IQueryable<T> source, bool re
: source.AsArangoQueryable().FirstAsync(cancellationToken);
}

public static Task<TSource> SingleAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource> SingleAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
{
return SingleOrDefaultAsync(source, false, null, cancellationToken);
}

public static Task<TSource> SingleAsync<TSource>(this IQueryable<TSource> source,
public static ValueTask<TSource> SingleAsync<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
{
return SingleOrDefaultAsync(source, false, predicate, cancellationToken);
}

public static Task<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source, CancellationToken cancellationToken = default)
{
return SingleOrDefaultAsync(source, true, null, cancellationToken);
}

public static Task<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source,
public static ValueTask<TSource> SingleOrDefaultAsync<TSource>(this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate, CancellationToken cancellationToken = default)
{
return SingleOrDefaultAsync(source, true, predicate, cancellationToken);
}

private static Task<T> SingleOrDefaultAsync<T>(this IQueryable<T> source, bool returnDefaultWhenEmpty,
private static ValueTask<T> SingleOrDefaultAsync<T>(this IQueryable<T> source, bool returnDefaultWhenEmpty,
Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default)
{
if (predicate != null)
Expand All @@ -119,7 +119,7 @@ private static Task<T> SingleOrDefaultAsync<T>(this IQueryable<T> source, bool r
: source.AsArangoQueryable().SingleAsync(cancellationToken);
}

public static Task<List<T>> ToListAsync<T>(this IQueryable<T> source, CancellationToken cancellationToken = default)
public static ValueTask<List<T>> ToListAsync<T>(this IQueryable<T> source, CancellationToken cancellationToken = default)
{
return source.AsArangoQueryable().ToListAsync(cancellationToken);
}
Expand Down
2 changes: 1 addition & 1 deletion Core.Arango/Linq/Interface/IArangoLinq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ internal interface IArangoLinq
public string ResolvePropertyName(Type t, string s);
public string ResolveCollectionName(Type t);
public IAsyncEnumerable<T> StreamAsync<T>(string query, IDictionary<string, object> bindVars, CancellationToken cancellationToken = default);
public Task<ArangoList<T>> ExecuteAsync<T>(string query, IDictionary<string, object> bindVars, CancellationToken cancellationToken = default);
public ValueTask<ArangoList<T>> ExecuteAsync<T>(string query, IDictionary<string, object> bindVars, CancellationToken cancellationToken = default);
}
}
2 changes: 1 addition & 1 deletion Core.Arango/Linq/Internal/ArangoLinq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IAsyncEnumerable<T> StreamAsync<T>(string query, IDictionary<string, obje
return _context.Query.ExecuteStreamAsync<T>(_handle, query, bindVars, cancellationToken: cancellationToken);
}

public async Task<ArangoList<T>> ExecuteAsync<T>(string query, IDictionary<string, object> bindVars, CancellationToken cancellationToken = default)
public async ValueTask<ArangoList<T>> ExecuteAsync<T>(string query, IDictionary<string, object> bindVars, CancellationToken cancellationToken = default)
{
return await _context.Query.ExecuteAsync<T>(_handle, query, bindVars, cancellationToken: cancellationToken).ConfigureAwait(false);
}
Expand Down
10 changes: 5 additions & 5 deletions Core.Arango/Linq/Query/ArangoQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,34 @@ public ArangoQueryable(IQueryProvider provider, Expression expression, IArangoLi
this.db = db;
}

public async Task<List<T>> ToListAsync(CancellationToken cancellationToken = default)
public async ValueTask<List<T>> ToListAsync(CancellationToken cancellationToken = default)
{
var data = GetQueryData();
return await db.ExecuteAsync<T>(data.Query, data.BindVars, cancellationToken).ConfigureAwait(false);
}

public async Task<T> SingleOrDefaultAsync(CancellationToken cancellationToken = default)
public async ValueTask<T> SingleOrDefaultAsync(CancellationToken cancellationToken = default)
{
var data = GetQueryData();
var list = await db.ExecuteAsync<T>(data.Query, data.BindVars, cancellationToken).ConfigureAwait(false);
return list.SingleOrDefault();
}

public async Task<T> SingleAsync(CancellationToken cancellationToken = default)
public async ValueTask<T> SingleAsync(CancellationToken cancellationToken = default)
{
var data = GetQueryData();
var list = await db.ExecuteAsync<T>(data.Query, data.BindVars, cancellationToken).ConfigureAwait(false);
return list.Single();
}

public async Task<T> FirstOrDefaultAsync(CancellationToken cancellationToken = default)
public async ValueTask<T> FirstOrDefaultAsync(CancellationToken cancellationToken = default)
{
var data = GetQueryData();
var list = await db.ExecuteAsync<T>(data.Query, data.BindVars, cancellationToken).ConfigureAwait(false);
return list.FirstOrDefault();
}

public async Task<T> FirstAsync(CancellationToken cancellationToken = default)
public async ValueTask<T> FirstAsync(CancellationToken cancellationToken = default)
{
var data = GetQueryData();
var list = await db.ExecuteAsync<T>(data.Query, data.BindVars, cancellationToken).ConfigureAwait(false);
Expand Down
4 changes: 2 additions & 2 deletions Core.Arango/Modules/IArangoAnalyzerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Task DeleteAsync(ArangoHandle database, string analyzer, bool force = false,
/// <summary>
/// returns a listing of available Analyzer definitions
/// </summary>
Task<IReadOnlyCollection<ArangoAnalyzer>> ListAsync(ArangoHandle database,
ValueTask<IReadOnlyCollection<ArangoAnalyzer>> ListAsync(ArangoHandle database,
CancellationToken cancellationToken = default);

/// <summary>
/// returns an Analyzer definition
/// </summary>
Task<ArangoAnalyzer> GetDefinitionAsync(ArangoHandle database, string analyzer,
ValueTask<ArangoAnalyzer> GetDefinitionAsync(ArangoHandle database, string analyzer,
CancellationToken cancellationToken = default);
}
}
4 changes: 2 additions & 2 deletions Core.Arango/Modules/IArangoBackupModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IArangoBackupModule
/// <summary>
/// Creates a local backup.
/// </summary>
Task<ArangoBackup> CreateAsync(ArangoBackupRequest request, CancellationToken cancellationToken = default);
ValueTask<ArangoBackup> CreateAsync(ArangoBackupRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Restores from a local backup.
Expand All @@ -28,6 +28,6 @@ public interface IArangoBackupModule
/// <summary>
/// List all local backups.
/// </summary>
Task<List<ArangoBackup>> ListAsync(string id = null, CancellationToken cancellationToken = default);
ValueTask<List<ArangoBackup>> ListAsync(string id = null, CancellationToken cancellationToken = default);
}
}
6 changes: 3 additions & 3 deletions Core.Arango/Modules/IArangoCollectionModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Task CreateAsync(ArangoHandle database, string collection, ArangoCollectionType
/// <summary>
/// Returns all collections
/// </summary>
Task<IReadOnlyCollection<ArangoCollection>> ListAsync(ArangoHandle database,
ValueTask<IReadOnlyCollection<ArangoCollection>> ListAsync(ArangoHandle database,
CancellationToken cancellationToken = default);

/// <summary>
Expand Down Expand Up @@ -55,13 +55,13 @@ Task DropAsync(ArangoHandle database, string collection,
/// <summary>
/// Returns a collection
/// </summary>
Task<ArangoCollection> GetAsync(ArangoHandle database, string collection,
ValueTask<ArangoCollection> GetAsync(ArangoHandle database, string collection,
CancellationToken cancellationToken = default);

/// <summary>
/// Checks of collection exists (calls ListAsync internally)
/// </summary>
Task<bool> ExistAsync(ArangoHandle database, string collection,
ValueTask<bool> ExistAsync(ArangoHandle database, string collection,
CancellationToken cancellationToken = default);

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions Core.Arango/Modules/IArangoDatabaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public interface IArangoDatabaseModule
/// <summary>
/// Creates a new database
/// </summary>
Task<bool> CreateAsync(ArangoHandle name, CancellationToken cancellationToken = default);
ValueTask<bool> CreateAsync(ArangoHandle name, CancellationToken cancellationToken = default);

/// <summary>
/// Creates a new database with options
/// </summary>
Task<bool> CreateAsync(ArangoDatabase database, CancellationToken cancellationToken = default);
ValueTask<bool> CreateAsync(ArangoDatabase database, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves information about the current database
/// </summary>
Task<ArangoDatabaseInfo> GetAsync(ArangoHandle handle, CancellationToken cancellationToken = default);
ValueTask<ArangoDatabaseInfo> GetAsync(ArangoHandle handle, CancellationToken cancellationToken = default);

/// <summary>
/// Drop an existing database
Expand All @@ -33,11 +33,11 @@ public interface IArangoDatabaseModule
/// <summary>
/// Checks if database exists
/// </summary>
Task<bool> ExistAsync(ArangoHandle name, CancellationToken cancellationToken = default);
ValueTask<bool> ExistAsync(ArangoHandle name, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves a list of all existing databases
/// </summary>
Task<List<string>> ListAsync(CancellationToken cancellationToken = default);
ValueTask<List<string>> ListAsync(CancellationToken cancellationToken = default);
}
}
Loading

0 comments on commit c9f13c9

Please sign in to comment.