Skip to content

Commit

Permalink
Reduce allocations in AbstractStructureTaggerProvider.GetMultiLineReg…
Browse files Browse the repository at this point in the history
…ions (#74619)

* Reduce allocations in AbstractStructureTaggerProvider.GetMultiLineRegions

This is a private method, and the caller simply enumerates the results, so there is no need to allocate an intermediary array.

This shows up as 0.6% of allocations in the scrolling speedomenter profile that I'm looking at (in the typing section of a particularly poorly performing run)
  • Loading branch information
ToddGrun authored Jul 31, 2024
1 parent f92d3ac commit d41f808
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,13 @@ private void ProcessSpans(
ImmutableArray<BlockSpan> spans)
{
var snapshot = snapshotSpan.Snapshot;
spans = GetMultiLineRegions(outliningService, spans, snapshot);

foreach (var span in spans)
// Use the returned enumerable directly instead of allocating into an array. The returned
// enumeration can contain a fairly large number of items for large files, so even
// using an ArrayBuilder could result in allocation issues without using a custom pool.
var multiLineSpans = GetMultiLineRegions(outliningService, spans, snapshot);

foreach (var span in multiLineSpans)
{
var tag = new StructureTag(this, span, snapshot);
context.AddTag(new TagSpan<IContainerStructureTag>(span.TextSpan.ToSnapshotSpan(snapshot), tag));
Expand All @@ -226,12 +230,11 @@ protected override bool TagEquals(IContainerStructureTag tag1, IContainerStructu

private static bool s_exceptionReported = false;

private static ImmutableArray<BlockSpan> GetMultiLineRegions(
private static IEnumerable<BlockSpan> GetMultiLineRegions(
BlockStructureService service,
ImmutableArray<BlockSpan> regions, ITextSnapshot snapshot)
{
// Remove any spans that aren't multiline.
var multiLineRegions = ArrayBuilder<BlockSpan>.GetInstance();
foreach (var region in regions)
{
if (region.TextSpan.Length > 0)
Expand Down Expand Up @@ -262,12 +265,10 @@ private static ImmutableArray<BlockSpan> GetMultiLineRegions(
var endLine = snapshot.GetLineNumberFromPosition(region.TextSpan.End);
if (startLine != endLine)
{
multiLineRegions.Add(region);
yield return region;
}
}
}

return multiLineRegions.ToImmutableAndFree();
}

#region Creating Preview Buffers
Expand Down

0 comments on commit d41f808

Please sign in to comment.