Reduce allocations again in BlockStructure collection #74627
Merged
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.
There are basically two places where array allocations have been improved (particularly when the array exceed the default array builder pool size).
AbstractBlockStructureProvider.ProvideBlockStructure now calls BlockSpanCollector.CollectBlockSpans with the context.Spans, not with a new TemporaryArray. This required a change to the Sort call to pass in the start index, and to the filtering code at the end of the method to only filter out from the range that the current provider added to context.Spans
BlockStructureContext has it's own pool for the ArrayBuilder. As this array was very commonly exceeding the maximum reusable size allowed in the default array pool, we were previously no placing these arrays back in a pool.
Note that BlockStructureServiceWithProviders.CreateBlockStructure no longer uses FixedSizeArrayBuilder, instead modifying the entries in place. There isn't really an allocation change in this method, as both old and new allocate a single array in this method.
After the GetMultiLineRegions change earlier today, these allocations are only about 0.8% in the scrolling speedometer profile I'm looking at. Of that, I'd expect this to only remove about half of that, so only somewhat impactful.
*** relevant allocations from scrolling speedometer profile ***