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

Cache the getnode/gettoken classifiers retrieived from the extension manager. #73712

Merged
merged 6 commits into from
May 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification.Classifiers;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Extensions;
using Microsoft.CodeAnalysis.Host;
Expand All @@ -24,8 +25,17 @@ internal abstract class AbstractClassificationService(ISyntaxClassificationServi
{
private readonly ISyntaxClassificationService _syntaxClassificationService = syntaxClassificationService;

private static Func<SyntaxNode, ImmutableArray<Classifiers.ISyntaxClassifier>>? s_getNodeClassifiers;
private static Func<SyntaxToken, ImmutableArray<Classifiers.ISyntaxClassifier>>? s_getTokenClassifiers;
private static ExtensionClassifierInfo? s_cachedExtensionClassifierInfo;
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively. each classification service is lang specific. so don't cache these statically. cache as instance members.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh, I like that better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer, RemoteSemanticClassificationService accesses this method statically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a common enough code path, that it should also hit the optimization, and I don't want to cache these both statically and on the instances, so I think I'd prefer to keep it as is unless you see a nice way around this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can have RemoteSemanticClassificationService fetch the real service, not access it statically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the general idea works (I don't think RemoteSemanticClassificationService can access AbstractClassificationService, but I can just have the static method do the cast check.


private class ExtensionClassifierInfo(
ISyntaxClassificationService classificationService,
Func<SyntaxNode, ImmutableArray<ISyntaxClassifier>> getNodeClassifiers,
Func<SyntaxToken, ImmutableArray<ISyntaxClassifier>> getTokenClassifiers)
{
public readonly ISyntaxClassificationService ClassificationService = classificationService;
public readonly Func<SyntaxNode, ImmutableArray<ISyntaxClassifier>> GetNodeClassifiers = getNodeClassifiers;
public readonly Func<SyntaxToken, ImmutableArray<ISyntaxClassifier>> GetTokenClassifiers = getTokenClassifiers;
}

public abstract void AddLexicalClassifications(SourceText text, TextSpan textSpan, SegmentedList<ClassifiedSpan> result, CancellationToken cancellationToken);
public abstract ClassifiedSpan AdjustStaleClassification(SourceText text, ClassifiedSpan classifiedSpan);
Expand Down Expand Up @@ -145,17 +155,10 @@ public static async Task AddClassificationsInCurrentProcessAsync(
{
var classificationService = document.GetRequiredLanguageService<ISyntaxClassificationService>();

if (s_getNodeClassifiers == null || s_getTokenClassifiers == null)
{
var extensionManager = document.Project.Solution.Services.GetRequiredService<IExtensionManager>();
var classifiers = classificationService.GetDefaultSyntaxClassifiers();

s_getNodeClassifiers = extensionManager.CreateNodeExtensionGetter(classifiers, c => c.SyntaxNodeTypes);
s_getTokenClassifiers = extensionManager.CreateTokenExtensionGetter(classifiers, c => c.SyntaxTokenKinds);
}
GetExtensionClassifiers(document, classificationService, out var getNodeClassifiers, out var getTokenClassifiers);

await classificationService.AddSemanticClassificationsAsync(
document, textSpans, options, s_getNodeClassifiers, s_getTokenClassifiers, result, cancellationToken).ConfigureAwait(false);
document, textSpans, options, getNodeClassifiers, getTokenClassifiers, result, cancellationToken).ConfigureAwait(false);

if (options.ClassifyReassignedVariables)
{
Expand Down Expand Up @@ -188,6 +191,30 @@ await embeddedLanguageService.AddEmbeddedLanguageClassificationsAsync(
}
}

private static void GetExtensionClassifiers(
Document document,
ISyntaxClassificationService classificationService,
out Func<SyntaxNode, ImmutableArray<ISyntaxClassifier>> getNodeClassifiers,
out Func<SyntaxToken, ImmutableArray<ISyntaxClassifier>> getTokenClassifiers)
{
var cachedExtensionClassifierInfo = s_cachedExtensionClassifierInfo;

if (cachedExtensionClassifierInfo == null || cachedExtensionClassifierInfo.ClassificationService != classificationService)
{
var extensionManager = document.Project.Solution.Services.GetRequiredService<IExtensionManager>();
var classifiers = classificationService.GetDefaultSyntaxClassifiers();

cachedExtensionClassifierInfo = new ExtensionClassifierInfo(
classificationService,
extensionManager.CreateNodeExtensionGetter(classifiers, c => c.SyntaxNodeTypes),
extensionManager.CreateTokenExtensionGetter(classifiers, c => c.SyntaxTokenKinds));
s_cachedExtensionClassifierInfo = cachedExtensionClassifierInfo;
}

getNodeClassifiers = cachedExtensionClassifierInfo.GetNodeClassifiers;
getTokenClassifiers = cachedExtensionClassifierInfo.GetTokenClassifiers;
}

public async Task AddSyntacticClassificationsAsync(Document document, ImmutableArray<TextSpan> textSpans, SegmentedList<ClassifiedSpan> result, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
Expand Down
Loading