dotnet-arangodb
dotnet-arangodb copied to clipboard
Memory usage ExecuteStreamAsync
I noticed the ExecuteStreamAsync
method retains the firstResult
for the entire duration of the enumerator. This results in a potentially large chunk of memory being held unnecessarily.
I have changed the code - for my purposes - to this:
public async IAsyncEnumerable<T> ExecuteStreamAsync<T>(ArangoHandle database, ArangoCursor cursor,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
QueryResponse<T> response = null;
string responseId = null;
while (response == null || response.HasMore)
{
if (response == null)
{
response = await SendAsync<QueryResponse<T>>(database, HttpMethod.Post, ApiPath(database, "cursor"), cursor,
cancellationToken: cancellationToken).ConfigureAwait(false);
responseId = response.Id;
Context.Configuration.QueryProfile?.Invoke(cursor.Query, cursor.BindVars, response.Extra.Statistic);
}
else
{
response = await SendAsync<QueryResponse<T>>(database, HttpMethod.Post, ApiPath(database, $"/cursor/{responseId}"),
cancellationToken: cancellationToken).ConfigureAwait(false);
}
if (response.Result?.Any() == true)
foreach (var result in response.Result)
yield return result;
// Force GC?
response.Result = null;
}
}