Skip to content

Commit

Permalink
Reduce LOH allocations for SemanticToken classification in LSP (#69496)
Browse files Browse the repository at this point in the history
* Reduce LOH allocations for SemanticToken classification in LSP

LSP semantic classification classifies the whole document per call. This ends up with a large number of classifiedspans per call, enough so that the standard ArrayBuilder cache ends up throwing away it's values upon Free. Instead, use the Classifier's pooled list, as it doesn't have the size limit for it's cache.

This accounts for about 0.5% of LOH allocations in the devenv process in the customer profile that I'm looking at.

* Remove accidental remnants of earlier attempt I made locally and didn't fullly revert.
  • Loading branch information
ToddGrun authored Aug 15, 2023
1 parent 20b9455 commit 6547e21
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand Down Expand Up @@ -59,7 +59,7 @@ private static async Task<ClassifiedSpan[]> GetClassifiedSpansForDocumentAsync(
CancellationToken cancellationToken)
{
var classificationService = document.GetRequiredLanguageService<IClassificationService>();
using var _ = ArrayBuilder<ClassifiedSpan>.GetInstance(out var classifiedSpans);
using var _ = Classifier.GetPooledList(out var classifiedSpans);

// We always return both syntactic and semantic classifications. If there is a syntactic classifier running on the client
// then the semantic token classifications will override them.
Expand All @@ -82,7 +82,7 @@ private static async Task<ClassifiedSpan[]> GetClassifiedSpansForDocumentAsync(

public static ClassifiedSpan[] ConvertMultiLineToSingleLineSpans(SourceText text, ClassifiedSpan[] classifiedSpans)
{
using var _ = ArrayBuilder<ClassifiedSpan>.GetInstance(out var updatedClassifiedSpans);
using var _ = Classifier.GetPooledList(out var updatedClassifiedSpans);

for (var spanIndex = 0; spanIndex < classifiedSpans.Length; spanIndex++)
{
Expand All @@ -109,7 +109,7 @@ public static ClassifiedSpan[] ConvertMultiLineToSingleLineSpans(SourceText text
static void ConvertToSingleLineSpan(
SourceText text,
ClassifiedSpan[] originalClassifiedSpans,
ArrayBuilder<ClassifiedSpan> updatedClassifiedSpans,
SegmentedList<ClassifiedSpan> updatedClassifiedSpans,
ref int spanIndex,
string classificationType,
int startLine,
Expand Down

0 comments on commit 6547e21

Please sign in to comment.