Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory usage ExecuteStreamAsync #58

Open
JejoSwaks opened this issue Oct 7, 2022 · 0 comments
Open

Memory usage ExecuteStreamAsync #58

JejoSwaks opened this issue Oct 7, 2022 · 0 comments

Comments

@JejoSwaks
Copy link

JejoSwaks commented Oct 7, 2022

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;
           }
       }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant