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

Remove cancellation token from tagging context object. #57167

Merged
merged 4 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
Expand Down Expand Up @@ -76,9 +77,9 @@ protected override ITaggerEventSource CreateEventSource(
TaggerEventSources.OnTextChanged(subjectBuffer));
}

protected override async Task ProduceTagsAsync(TaggerContext<LineSeparatorTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition)
protected override async Task ProduceTagsAsync(
TaggerContext<LineSeparatorTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition, CancellationToken cancellationToken)
{
var cancellationToken = context.CancellationToken;
var document = documentSnapshotSpan.Document;
if (document == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,28 @@ protected override ITaggerEventSource CreateEventSource(ITextView textView, ITex
TaggerEventSources.OnParseOptionChanged(subjectBuffer));
}

protected override Task ProduceTagsAsync(TaggerContext<BraceHighlightTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition)
protected override Task ProduceTagsAsync(
TaggerContext<BraceHighlightTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition, CancellationToken cancellationToken)
{
var document = documentSnapshotSpan.Document;
if (!caretPosition.HasValue || document == null)
{
return Task.CompletedTask;
}

return ProduceTagsAsync(context, document, documentSnapshotSpan.SnapshotSpan.Snapshot, caretPosition.Value);
return ProduceTagsAsync(
context, document, documentSnapshotSpan.SnapshotSpan.Snapshot, caretPosition.Value, cancellationToken);
}

internal async Task ProduceTagsAsync(TaggerContext<BraceHighlightTag> context, Document document, ITextSnapshot snapshot, int position)
internal async Task ProduceTagsAsync(
TaggerContext<BraceHighlightTag> context, Document document, ITextSnapshot snapshot, int position, CancellationToken cancellationToken)
{
using (Logger.LogBlock(FunctionId.Tagger_BraceHighlighting_TagProducer_ProduceTags, context.CancellationToken))
using (Logger.LogBlock(FunctionId.Tagger_BraceHighlighting_TagProducer_ProduceTags, cancellationToken))
{
if (position >= 0 && position <= snapshot.Length)
{
var (bracesLeftOfPosition, bracesRightOfPosition) = await GetAllMatchingBracesAsync(
_braceMatcherService, document, position, context.CancellationToken).ConfigureAwait(false);
_braceMatcherService, document, position, cancellationToken).ConfigureAwait(false);

AddBraces(context, snapshot, bracesLeftOfPosition);
AddBraces(context, snapshot, bracesRightOfPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ public IEnumerable<ITagSpan<IClassificationTag>> GetAllTags(NormalizedSnapshotSp
if (!canReuseCache)
{
// Our cache is not there, or is out of date. We need to compute the up to date results.
var context = new TaggerContext<IClassificationTag>(document, snapshot, cancellationToken: cancellationToken);
var context = new TaggerContext<IClassificationTag>(document, snapshot);
this.ThreadingContext.JoinableTaskFactory.Run(
() => ProduceTagsAsync(context, new DocumentSnapshotSpan(document, spanToTag), _owner._typeMap));
() => ProduceTagsAsync(context, new DocumentSnapshotSpan(document, spanToTag), _owner._typeMap, cancellationToken));
Copy link
Member

@sharwell sharwell Oct 15, 2021

Choose a reason for hiding this comment

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

⚠️ cancellationToken will be captured for this lambda even on code paths where canReuseCache is true. I didn't evaluate how common this is, but if this method normally doesn't use this code path and normally doesn't allocate (or limited allocations) and is called frequently, this could be observable in perf.

Copy link
Member Author

Choose a reason for hiding this comment

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

we capture everything else passed along here. i'm not sure why this woudl matter.

similary, this codepaths runs once per Copy operation. it doesn't seem like any change here would be relevant.


cachedTaggedSpan = spanToTag;
cachedTags = new TagSpanIntervalTree<IClassificationTag>(snapshot.TextBuffer, SpanTrackingMode.EdgeExclusive, context.tagSpans);
Expand All @@ -152,11 +152,12 @@ private void GetCachedInfo(out SnapshotSpan? cachedTaggedSpan, out TagSpanInterv
}
}

private static Task ProduceTagsAsync(TaggerContext<IClassificationTag> context, DocumentSnapshotSpan documentSpan, ClassificationTypeMap typeMap)
private static Task ProduceTagsAsync(
TaggerContext<IClassificationTag> context, DocumentSnapshotSpan documentSpan, ClassificationTypeMap typeMap, CancellationToken cancellationToken)
{
var classificationService = documentSpan.Document.GetLanguageService<IClassificationService>();
return classificationService != null
? SemanticClassificationUtilities.ProduceTagsAsync(context, documentSpan, classificationService, typeMap)
? SemanticClassificationUtilities.ProduceTagsAsync(context, documentSpan, classificationService, typeMap, cancellationToken)
: Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#nullable disable

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
Expand All @@ -18,7 +17,6 @@
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Storage;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Text;
Expand All @@ -33,7 +31,8 @@ public static async Task ProduceTagsAsync(
TaggerContext<IClassificationTag> context,
DocumentSnapshotSpan spanToTag,
IClassificationService classificationService,
ClassificationTypeMap typeMap)
ClassificationTypeMap typeMap,
CancellationToken cancellationToken)
{
var document = spanToTag.Document;
if (document == null)
Expand All @@ -49,11 +48,11 @@ public static async Task ProduceTagsAsync(
// parsing/loading. For cross language projects, this also produces semantic classifications more quickly
// as we do not have to wait on skeletons to be built.

document = document.WithFrozenPartialSemantics(context.CancellationToken);
document = document.WithFrozenPartialSemantics(cancellationToken);
spanToTag = new DocumentSnapshotSpan(document, spanToTag.SnapshotSpan);

var classified = await TryClassifyContainingMemberSpanAsync(
context, spanToTag, classificationService, typeMap).ConfigureAwait(false);
context, spanToTag, classificationService, typeMap, cancellationToken).ConfigureAwait(false);
if (classified)
{
return;
Expand All @@ -62,14 +61,15 @@ public static async Task ProduceTagsAsync(
// We weren't able to use our specialized codepaths for semantic classifying.
// Fall back to classifying the full span that was asked for.
await ClassifySpansAsync(
context, spanToTag, classificationService, typeMap).ConfigureAwait(false);
context, spanToTag, classificationService, typeMap, cancellationToken).ConfigureAwait(false);
}

private static async Task<bool> TryClassifyContainingMemberSpanAsync(
TaggerContext<IClassificationTag> context,
DocumentSnapshotSpan spanToTag,
IClassificationService classificationService,
ClassificationTypeMap typeMap)
ClassificationTypeMap typeMap,
CancellationToken cancellationToken)
{
var range = context.TextChangeRange;
if (range == null)
Expand All @@ -85,8 +85,6 @@ private static async Task<bool> TryClassifyContainingMemberSpanAsync(
return false;
}

var cancellationToken = context.CancellationToken;

var lastSemanticVersion = (VersionStamp?)context.State;
if (lastSemanticVersion != null)
{
Expand Down Expand Up @@ -128,23 +126,23 @@ private static async Task<bool> TryClassifyContainingMemberSpanAsync(

// re-classify only the member we're inside.
await ClassifySpansAsync(
context, subSpanToTag, classificationService, typeMap).ConfigureAwait(false);
context, subSpanToTag, classificationService, typeMap, cancellationToken).ConfigureAwait(false);
return true;
}

private static async Task ClassifySpansAsync(
TaggerContext<IClassificationTag> context,
DocumentSnapshotSpan spanToTag,
IClassificationService classificationService,
ClassificationTypeMap typeMap)
ClassificationTypeMap typeMap,
CancellationToken cancellationToken)
{
try
{
var document = spanToTag.Document;
var snapshotSpan = spanToTag.SnapshotSpan;
var snapshot = snapshotSpan.Snapshot;

var cancellationToken = context.CancellationToken;
using (Logger.LogBlock(FunctionId.Tagger_SemanticClassification_TagProducer_ProduceTags, cancellationToken))
{
using var _ = ArrayBuilder<ClassifiedSpan>.GetInstance(out var classifiedSpans);
Expand All @@ -162,7 +160,7 @@ await AddSemanticClassificationsAsync(
context.State = version;
}
}
catch (Exception e) when (FatalError.ReportAndPropagateUnlessCanceled(e))
catch (Exception e) when (FatalError.ReportAndPropagateUnlessCanceled(e, cancellationToken))
{
throw ExceptionUtilities.Unreachable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
Expand Down Expand Up @@ -90,7 +91,8 @@ protected override IEnumerable<SnapshotSpan> GetSpansToTag(ITextView textView, I
return SpecializedCollections.SingletonEnumerable(visibleSpanOpt.Value);
}

protected override Task ProduceTagsAsync(TaggerContext<IClassificationTag> context)
protected override Task ProduceTagsAsync(
TaggerContext<IClassificationTag> context, CancellationToken cancellationToken)
{
Debug.Assert(context.SpansToTag.IsSingle());

Expand All @@ -113,7 +115,8 @@ protected override Task ProduceTagsAsync(TaggerContext<IClassificationTag> conte
if (workspaceContextService?.IsInLspEditorContext() == true)
return Task.CompletedTask;

return SemanticClassificationUtilities.ProduceTagsAsync(context, spanToTag, classificationService, _typeMap);
return SemanticClassificationUtilities.ProduceTagsAsync(
context, spanToTag, classificationService, _typeMap, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ protected override ITaggerEventSource CreateEventSource(ITextView textViewOpt, I
protected internal virtual ImmutableArray<DiagnosticDataLocation> GetLocationsToTag(DiagnosticData diagnosticData)
=> diagnosticData.DataLocation is object ? ImmutableArray.Create(diagnosticData.DataLocation) : ImmutableArray<DiagnosticDataLocation>.Empty;

protected override Task ProduceTagsAsync(TaggerContext<TTag> context, DocumentSnapshotSpan spanToTag, int? caretPosition)
protected override Task ProduceTagsAsync(
TaggerContext<TTag> context, DocumentSnapshotSpan spanToTag, int? caretPosition, CancellationToken cancellationToken)
{
return ProduceTagsAsync(context, spanToTag);
return ProduceTagsAsync(context, spanToTag, cancellationToken);
}

private async Task ProduceTagsAsync(TaggerContext<TTag> context, DocumentSnapshotSpan spanToTag)
private async Task ProduceTagsAsync(
TaggerContext<TTag> context, DocumentSnapshotSpan spanToTag, CancellationToken cancellationToken)
{
if (!this.IsEnabled)
{
Expand All @@ -138,7 +140,6 @@ private async Task ProduceTagsAsync(TaggerContext<TTag> context, DocumentSnapsho

var editorSnapshot = spanToTag.SnapshotSpan.Snapshot;

var cancellationToken = context.CancellationToken;
var workspace = document.Project.Solution.Workspace;

// See if we've marked any spans as those we want to suppress diagnostics for.
Expand All @@ -149,7 +150,7 @@ private async Task ProduceTagsAsync(TaggerContext<TTag> context, DocumentSnapsho
buffer?.Properties.TryGetProperty(PredefinedPreviewTaggerKeys.SuppressDiagnosticsSpansKey, out suppressedDiagnosticsSpans);

var buckets = _diagnosticService.GetPushDiagnosticBuckets(
workspace, document.Project.Id, document.Id, InternalDiagnosticsOptions.NormalDiagnosticMode, context.CancellationToken);
workspace, document.Project.Id, document.Id, InternalDiagnosticsOptions.NormalDiagnosticMode, cancellationToken);

foreach (var bucket in buckets)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor;
using Microsoft.CodeAnalysis.Editor.Shared.Tagging;
Expand Down Expand Up @@ -55,7 +56,8 @@ protected override ITaggerEventSource CreateEventSource(ITextView textView, ITex
TaggerEventSources.OnDocumentActiveContextChanged(subjectBuffer));
}

protected override async Task ProduceTagsAsync(TaggerContext<ITextMarkerTag> context)
protected override async Task ProduceTagsAsync(
TaggerContext<ITextMarkerTag> context, CancellationToken cancellationToken)
{
Debug.Assert(context.SpansToTag.IsSingle());

Expand All @@ -75,7 +77,7 @@ protected override async Task ProduceTagsAsync(TaggerContext<ITextMarkerTag> con

var snapshot = spanToTag.SnapshotSpan.Snapshot;

var activeStatementSpans = await activeStatementTrackingService.GetAdjustedTrackingSpansAsync(document, snapshot, context.CancellationToken).ConfigureAwait(false);
var activeStatementSpans = await activeStatementTrackingService.GetAdjustedTrackingSpansAsync(document, snapshot, cancellationToken).ConfigureAwait(false);
foreach (var activeStatementSpan in activeStatementSpans)
{
if (activeStatementSpan.IsLeaf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
using Microsoft.CodeAnalysis.Editor.Shared.Tagging;
Expand Down Expand Up @@ -62,9 +63,9 @@ protected override ITaggerEventSource CreateEventSource(ITextView textView, ITex
TaggerEventSources.OnParseOptionChanged(subjectBuffer));
}

protected override async Task ProduceTagsAsync(TaggerContext<KeywordHighlightTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition)
protected override async Task ProduceTagsAsync(
TaggerContext<KeywordHighlightTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition, CancellationToken cancellationToken)
{
var cancellationToken = context.CancellationToken;
var document = documentSnapshotSpan.Document;

// https://devdiv.visualstudio.com/DevDiv/_workitems/edit/763988
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Options;
Expand Down Expand Up @@ -125,7 +126,7 @@ protected sealed override ITaggerEventSource CreateEventSource(ITextView textVie
}

protected sealed override async Task ProduceTagsAsync(
TaggerContext<IStructureTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition)
TaggerContext<IStructureTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition, CancellationToken cancellationToken)
{
try
{
Expand All @@ -142,13 +143,13 @@ protected sealed override async Task ProduceTagsAsync(
return;

var blockStructure = await outliningService.GetBlockStructureAsync(
documentSnapshotSpan.Document, context.CancellationToken).ConfigureAwait(false);
documentSnapshotSpan.Document, cancellationToken).ConfigureAwait(false);

ProcessSpans(
context, documentSnapshotSpan.SnapshotSpan, outliningService,
blockStructure.Spans);
}
catch (Exception e) when (FatalError.ReportAndPropagateUnlessCanceled(e))
catch (Exception e) when (FatalError.ReportAndPropagateUnlessCanceled(e, cancellationToken))
{
throw ExceptionUtilities.Unreachable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Tagging;
Expand Down Expand Up @@ -93,9 +94,9 @@ protected override IEnumerable<SnapshotSpan> GetSpansToTag(ITextView textView, I
return SpecializedCollections.SingletonEnumerable(visibleSpanOpt.Value);
}

protected override async Task ProduceTagsAsync(TaggerContext<InlineHintDataTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition)
protected override async Task ProduceTagsAsync(
TaggerContext<InlineHintDataTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition, CancellationToken cancellationToken)
{
var cancellationToken = context.CancellationToken;
var document = documentSnapshotSpan.Document;
if (document == null)
return;
Expand Down
Loading