dotnet-arangodb icon indicating copy to clipboard operation
dotnet-arangodb copied to clipboard

Memory usage ExecuteStreamAsync

Open JejoSwaks opened this issue 2 years ago • 0 comments

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;
           }
       }

JejoSwaks avatar Oct 07 '22 13:10 JejoSwaks