Avoid some List allocations and resizes #857
Open
+57
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe your change
Profiling my application,
SplitIntoBatchesAsync
showed up and I spotted room for improvement.SplitIntoBatchesAsync
Before, a new List was allocated and data copied, even though batch size is below max.
If we can get the size of the batch and it is below max batch size, no new allocations & copying happens and the data is used directly.
If the batch size is over max batch size, resizing the records list from 4->8->16->32->64->128->256->512->1024 is avoided.
Resizing the list involves allocating new arrays for the mentioned capacities and copying the data over.
Instead one array of max batch size is allocated.
If
batch size % max batch size != 0
an array is allocated for the last batch.BatchRequest
If we can get the size of the data, the Operations List is initiated with the size, so resizing it from 4->8->16->32->64->128->256->512->1024 for max batch size is avoided.
What problem is this fixing?
This will avoid allocations & resizing of records for batches smaller than max batch size for all collections.
Resizing lists will be avoided for
SplitIntoBatchesAsync
&BatchRequest
for all collections of batches & data.This will reduce resources spent on allocating, resizing, memory pressure & garbage collection for these operations.
Thereby increasing the perceived performance of algolia.
And lowering the environmental footprint of users of this SDK