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

Drop roslyn impact on scrolling perf by 30% #73648

Merged
merged 8 commits into from
May 22, 2024
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
16 changes: 16 additions & 0 deletions src/Dependencies/Collections/SegmentedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ public static void Sort<T>(SegmentedArray<T> array, int index, int length, IComp
}
}

public static void Sort<T>(SegmentedArray<T> array, int index, int length, Comparison<T> comparison)
Copy link
Member Author

Choose a reason for hiding this comment

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

copy of method above, just taking and passing through a Comparison instead of an IComparer. See https://github.com/dotnet/roslyn/pull/73648/files#r1610644205 for more details.

Copy link
Member

@sharwell sharwell May 28, 2024

Choose a reason for hiding this comment

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

To better match the reference implementation (from dotnet/runtime), let's remove this method and inline these two lines to the caller:

var segment = new SegmentedArraySegment<T>(array, index, length);
SegmentedArraySortHelper<T>.Sort(segment, comparison);

{
if (index < 0)
ThrowHelper.ThrowIndexArgumentOutOfRange_NeedNonNegNumException();
if (length < 0)
ThrowHelper.ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum();
if (array.Length - index < length)
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);

if (length > 1)
{
var segment = new SegmentedArraySegment<T>(array, index, length);
SegmentedArraySortHelper<T>.Sort(segment, comparison);
}
}

public static void Sort<T>(SegmentedArray<T> array, Comparison<T> comparison)
{
if (comparison is null)
Expand Down
2 changes: 1 addition & 1 deletion src/Dependencies/Collections/SegmentedList`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ public void Sort(Comparison<T> comparison)

if (_size > 1)
{
SegmentedArray.Sort<T>(_items, 0, _size, Comparer<T>.Create(comparison));
SegmentedArray.Sort(_items, 0, _size, comparison);
Copy link
Member Author

Choose a reason for hiding this comment

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

existing codepath forced an allocation from comparison to comparer. That comparer was later converted back to a comparison by grabbing off the .Compare method from it. So this double allocated going through this path.

THe path that takes an IComparer single-allocs (tearing off the .Compare delegate).

Now, there is at least no alloc if passing in a comparison delegate. we pass it through all the way to the final place that uses it.

Copy link
Member Author

@CyrusNajmabadi CyrusNajmabadi May 22, 2024

Choose a reason for hiding this comment

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

Allocations caused by this contributed to nearly half of the excess costs here (as well as a ton of garbage):

image

}
_version++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public IEnumerable<ITagSpan<IClassificationTag>> GetTags(NormalizedSnapshotSpanC
}

private static IEnumerable<ITagSpan<IClassificationTag>> GetIntersectingTags(NormalizedSnapshotSpanCollection spans, TagSpanIntervalTree<IClassificationTag> cachedTags)
=> SegmentedListPool<ITagSpan<IClassificationTag>>.ComputeList(
=> SegmentedListPool<TagSpan<IClassificationTag>>.ComputeList(
Copy link
Member Author

Choose a reason for hiding this comment

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

switched everything to be TagSpan based. Avoids lots and lots and lots and lots of virtual dispatch. Everything is non-virtual on the impl type, allowing for major inlining.

static (args, tags) => args.cachedTags.AddIntersectingTagSpans(args.spans, tags),
(cachedTags, spans));

Expand Down Expand Up @@ -138,7 +138,7 @@ private IEnumerable<ITagSpan<IClassificationTag>> ComputeAndCacheAllTags(
var options = _globalOptions.GetClassificationOptions(document.Project.Language);

// Final list of tags to produce, containing syntax/semantic/embedded classification tags.
using var _ = SegmentedListPool.GetPooledList<ITagSpan<IClassificationTag>>(out var mergedTags);
using var _ = SegmentedListPool.GetPooledList<TagSpan<IClassificationTag>>(out var mergedTags);

_owner._threadingContext.JoinableTaskFactory.Run(async () =>
{
Expand Down Expand Up @@ -166,7 +166,7 @@ await TotalClassificationAggregateTagger.AddTagsAsync(

return GetIntersectingTags(spans, cachedTags);

Func<NormalizedSnapshotSpanCollection, SegmentedList<ITagSpan<IClassificationTag>>, VoidResult, Task> GetTaggingFunction(
Func<NormalizedSnapshotSpanCollection, SegmentedList<TagSpan<IClassificationTag>>, VoidResult, Task> GetTaggingFunction(
bool requireSingleSpan, Func<TextSpan, SegmentedList<ClassifiedSpan>, Task> addTagsAsync)
{
Contract.ThrowIfTrue(requireSingleSpan && spans.Count != 1, "We should only be asking for a single span");
Expand All @@ -175,7 +175,7 @@ Func<NormalizedSnapshotSpanCollection, SegmentedList<ITagSpan<IClassificationTag

async Task AddSpansAsync(
NormalizedSnapshotSpanCollection spans,
SegmentedList<ITagSpan<IClassificationTag>> result,
SegmentedList<TagSpan<IClassificationTag>> result,
Func<TextSpan, SegmentedList<ClassifiedSpan>, Task> addAsync)
{
// temp buffer we can use across all our classification calls. Should be cleared between each call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,15 @@ async ValueTask<SnapshotSpan> ComputeChangedSpanAsync()
return _lastProcessedData;
}

public void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<ITagSpan<IClassificationTag>> tags)
public void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<TagSpan<IClassificationTag>> tags)
{
_taggerProvider._threadingContext.ThrowIfNotOnUIThread();

using (Logger.LogBlock(FunctionId.Tagger_SyntacticClassification_TagComputer_GetTags, CancellationToken.None))
AddTagsWorker(spans, tags);
}

private void AddTagsWorker(NormalizedSnapshotSpanCollection spans, SegmentedList<ITagSpan<IClassificationTag>> tags)
private void AddTagsWorker(NormalizedSnapshotSpanCollection spans, SegmentedList<TagSpan<IClassificationTag>> tags)
{
_taggerProvider._threadingContext.ThrowIfNotOnUIThread();
if (spans.Count == 0 || _workspace == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Tagger(TagComputer tagComputer)

public override void AddTags(
NormalizedSnapshotSpanCollection spans,
SegmentedList<ITagSpan<IClassificationTag>> tags)
SegmentedList<TagSpan<IClassificationTag>> tags)
{
var tagComputer = _tagComputer ?? throw new ObjectDisposedException(GetType().FullName);
tagComputer.AddTags(spans, tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -77,9 +78,9 @@ internal sealed class TotalClassificationAggregateTagger(
EfficientTagger<IClassificationTag> embeddedTagger)
: AbstractAggregateTagger<IClassificationTag>([syntacticTagger, semanticTagger, embeddedTagger])
{
private static readonly Comparison<ITagSpan<IClassificationTag>> s_spanComparison = static (s1, s2) => s1.Span.Start - s2.Span.Start;
private static readonly Comparison<TagSpan<IClassificationTag>> s_spanComparison = static (s1, s2) => s1.Span.Start.Position - s2.Span.Start.Position;

public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<ITagSpan<IClassificationTag>> totalTags)
public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<TagSpan<IClassificationTag>> totalTags)
{
// Everything we pass in is synchronous, so we should immediately get a completed task back out.
AddTagsAsync(
Expand All @@ -105,20 +106,20 @@ public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedLi

public static async Task AddTagsAsync<TArg>(
NormalizedSnapshotSpanCollection spans,
SegmentedList<ITagSpan<IClassificationTag>> totalTags,
Func<NormalizedSnapshotSpanCollection, SegmentedList<ITagSpan<IClassificationTag>>, TArg, Task> addSyntacticSpansAsync,
Func<NormalizedSnapshotSpanCollection, SegmentedList<ITagSpan<IClassificationTag>>, TArg, Task> addSemanticSpansAsync,
Func<NormalizedSnapshotSpanCollection, SegmentedList<ITagSpan<IClassificationTag>>, TArg, Task> addEmbeddedSpansAsync,
SegmentedList<TagSpan<IClassificationTag>> totalTags,
Func<NormalizedSnapshotSpanCollection, SegmentedList<TagSpan<IClassificationTag>>, TArg, Task> addSyntacticSpansAsync,
Func<NormalizedSnapshotSpanCollection, SegmentedList<TagSpan<IClassificationTag>>, TArg, Task> addSemanticSpansAsync,
Func<NormalizedSnapshotSpanCollection, SegmentedList<TagSpan<IClassificationTag>>, TArg, Task> addEmbeddedSpansAsync,
TArg arg)
{
// First, get all the syntactic tags. While they are generally overridden by semantic tags (since semantics
// allows us to understand better what things like identifiers mean), they do take precedence for certain
// tags like 'Comments' and 'Excluded Code'. In those cases we want the classification to 'snap' instantly to
// the syntactic state, and we do not want things like semantic classifications showing up over that.

using var _1 = SegmentedListPool.GetPooledList<ITagSpan<IClassificationTag>>(out var stringLiterals);
using var _2 = SegmentedListPool.GetPooledList<ITagSpan<IClassificationTag>>(out var syntacticSpans);
using var _3 = SegmentedListPool.GetPooledList<ITagSpan<IClassificationTag>>(out var semanticSpans);
using var _1 = SegmentedListPool.GetPooledList<TagSpan<IClassificationTag>>(out var stringLiterals);
using var _2 = SegmentedListPool.GetPooledList<TagSpan<IClassificationTag>>(out var syntacticSpans);
using var _3 = SegmentedListPool.GetPooledList<TagSpan<IClassificationTag>>(out var semanticSpans);

await addSyntacticSpansAsync(spans, syntacticSpans, arg).ConfigureAwait(false);
await addSemanticSpansAsync(spans, semanticSpans, arg).ConfigureAwait(false);
Expand Down Expand Up @@ -231,7 +232,7 @@ async Task AddEmbeddedClassificationsAsync()
return;

// Only need to ask for the spans that overlapped the string literals.
using var _1 = SegmentedListPool.GetPooledList<ITagSpan<IClassificationTag>>(out var embeddedClassifications);
using var _1 = SegmentedListPool.GetPooledList<TagSpan<IClassificationTag>>(out var embeddedClassifications);

var stringLiteralSpansFull = new NormalizedSnapshotSpanCollection(stringLiterals.Select(s => s.Span));

Expand All @@ -257,27 +258,27 @@ async Task AddEmbeddedClassificationsAsync()
// The helper will add all the embedded classifications first, then add string literal classifications
// in the the space between the embedded classifications that were originally classified as a string
// literal.
ClassifierHelper.MergeParts<ITagSpan<IClassificationTag>, ClassificationTagSpanIntervalIntrospector>(
ClassifierHelper.MergeParts<TagSpan<IClassificationTag>, ClassificationTagSpanIntervalIntrospector>(
stringLiterals,
embeddedClassifications,
totalTags,
static tag => tag.Span.Span.ToTextSpan(),
static (original, final) => new TagSpan<IClassificationTag>(new SnapshotSpan(original.Span.Snapshot, final.ToSpan()), original.Tag));
}

ITagSpan<IClassificationTag>? GetNextSyntacticSpan()
TagSpan<IClassificationTag>? GetNextSyntacticSpan()
=> syntacticEnumerator.MoveNext() ? syntacticEnumerator.Current : null;

ITagSpan<IClassificationTag>? GetNextSemanticSpan()
TagSpan<IClassificationTag>? GetNextSemanticSpan()
=> semanticEnumerator.MoveNext() ? semanticEnumerator.Current : null;
}

private readonly struct ClassificationTagSpanIntervalIntrospector : IIntervalIntrospector<ITagSpan<IClassificationTag>>
private readonly struct ClassificationTagSpanIntervalIntrospector : IIntervalIntrospector<TagSpan<IClassificationTag>>
{
public int GetStart(ITagSpan<IClassificationTag> value)
public int GetStart(TagSpan<IClassificationTag> value)
=> value.Span.Start;

public int GetLength(ITagSpan<IClassificationTag> value)
public int GetLength(TagSpan<IClassificationTag> value)
=> value.Span.Length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public bool HasSpanThatContains(SnapshotPoint point)
return _tree.HasIntervalThatContains(point.Position, length: 0, new IntervalIntrospector(snapshot));
}

public IList<ITagSpan<TTag>> GetIntersectingSpans(SnapshotSpan snapshotSpan)
=> SegmentedListPool<ITagSpan<TTag>>.ComputeList(
public IList<TagSpan<TTag>> GetIntersectingSpans(SnapshotSpan snapshotSpan)
=> SegmentedListPool<TagSpan<TTag>>.ComputeList(
static (args, tags) => [email protected](args.snapshotSpan, tags),
(@this: this, snapshotSpan));

Expand All @@ -61,7 +61,7 @@ public IList<ITagSpan<TTag>> GetIntersectingSpans(SnapshotSpan snapshotSpan)
/// <paramref name="result"/>. Note the sorted chunk of items are appended to <paramref name="result"/>. This
/// means that <paramref name="result"/> may not be sorted if there were already items in them.
/// </summary>
private void AppendIntersectingSpansInSortedOrder(SnapshotSpan snapshotSpan, SegmentedList<ITagSpan<TTag>> result)
private void AppendIntersectingSpansInSortedOrder(SnapshotSpan snapshotSpan, SegmentedList<TagSpan<TTag>> result)
{
var snapshot = snapshotSpan.Snapshot;
Debug.Assert(snapshot.TextBuffer == _textBuffer);
Expand All @@ -80,14 +80,14 @@ public IEnumerable<ITagSpan<TTag>> GetSpans(ITextSnapshot snapshot)
public bool IsEmpty()
=> _tree.IsEmpty();

public void AddIntersectingTagSpans(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<ITagSpan<TTag>> tags)
public void AddIntersectingTagSpans(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<TagSpan<TTag>> tags)
{
AddIntersectingTagSpansWorker(requestedSpans, tags);
DebugVerifyTags(requestedSpans, tags);
}

[Conditional("DEBUG")]
private static void DebugVerifyTags(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<ITagSpan<TTag>> tags)
private static void DebugVerifyTags(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<TagSpan<TTag>> tags)
{
if (tags == null)
{
Expand All @@ -107,7 +107,7 @@ private static void DebugVerifyTags(NormalizedSnapshotSpanCollection requestedSp

private void AddIntersectingTagSpansWorker(
NormalizedSnapshotSpanCollection requestedSpans,
SegmentedList<ITagSpan<TTag>> tags)
SegmentedList<TagSpan<TTag>> tags)
{
const int MaxNumberOfRequestedSpans = 100;

Expand All @@ -123,20 +123,20 @@ private void AddIntersectingTagSpansWorker(

private void AddTagsForSmallNumberOfSpans(
NormalizedSnapshotSpanCollection requestedSpans,
SegmentedList<ITagSpan<TTag>> tags)
SegmentedList<TagSpan<TTag>> tags)
{
foreach (var span in requestedSpans)
AppendIntersectingSpansInSortedOrder(span, tags);
}

private void AddTagsForLargeNumberOfSpans(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<ITagSpan<TTag>> tags)
private void AddTagsForLargeNumberOfSpans(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<TagSpan<TTag>> tags)
{
// we are asked with bunch of spans. rather than asking same question again and again, ask once with big span
// which will return superset of what we want. and then filter them out in O(m+n) cost.
// m == number of requested spans, n = number of returned spans
var mergedSpan = new SnapshotSpan(requestedSpans[0].Start, requestedSpans[^1].End);

using var _1 = SegmentedListPool.GetPooledList<ITagSpan<TTag>>(out var tempList);
using var _1 = SegmentedListPool.GetPooledList<TagSpan<TTag>>(out var tempList);

AppendIntersectingSpansInSortedOrder(mergedSpan, tempList);
if (tempList.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ private DiffResult ComputeDifference(
return tags;
}

public void AddTags(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<ITagSpan<TTag>> tags)
public void AddTags(NormalizedSnapshotSpanCollection requestedSpans, SegmentedList<TagSpan<TTag>> tags)
{
_dataSource.ThreadingContext.ThrowIfNotOnUIThread();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override event EventHandler<SnapshotSpanEventArgs>? TagsChanged
public override void Dispose()
=> _tagSource.OnTaggerDisposed(this);

public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<ITagSpan<TTag>> tags)
public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<TagSpan<TTag>> tags)
=> _tagSource.AddTags(spans, tags);
}
}
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core/Tagging/AggregateTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal sealed class SimpleAggregateTagger<TTag>(ImmutableArray<EfficientTagger
: AbstractAggregateTagger<TTag>(taggers)
where TTag : ITag
{
public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<ITagSpan<TTag>> tags)
public override void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<TagSpan<TTag>> tags)
{
foreach (var tagger in this.Taggers)
tagger.AddTags(spans, tags);
Expand Down
4 changes: 2 additions & 2 deletions src/EditorFeatures/Core/Tagging/EfficientTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ internal abstract class EfficientTagger<TTag> : ITagger<TTag>, IDisposable where
/// <summary>
/// Produce the set of tags with the requested <paramref name="spans"/>, adding those tags to <paramref name="tags"/>.
/// </summary>
public abstract void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<ITagSpan<TTag>> tags);
public abstract void AddTags(NormalizedSnapshotSpanCollection spans, SegmentedList<TagSpan<TTag>> tags);

public abstract void Dispose();

/// <summary>
/// Default impl of the core <see cref="ITagger{T}"/> interface. Forces an allocation.
/// </summary>
public IEnumerable<ITagSpan<TTag>> GetTags(NormalizedSnapshotSpanCollection spans)
=> SegmentedListPool<ITagSpan<TTag>>.ComputeList(
=> SegmentedListPool<TagSpan<TTag>>.ComputeList(
static (args, tags) => [email protected](args.spans, tags),
(@this: this, spans));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
namespace Microsoft.CodeAnalysis.CSharp.Classification;

[ExportLanguageService(typeof(IClassificationService), LanguageNames.CSharp), Shared]
internal class CSharpEditorClassificationService : AbstractClassificationService
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpEditorClassificationService(
CSharpSyntaxClassificationService syntaxClassificationService) : AbstractClassificationService(syntaxClassificationService)
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpEditorClassificationService()
{
}

public override void AddLexicalClassifications(SourceText text, TextSpan textSpan, SegmentedList<ClassifiedSpan> result, CancellationToken cancellationToken)
=> ClassificationHelpers.AddLexicalClassifications(text, textSpan, result, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Microsoft.CodeAnalysis.CSharp.Classification;

[ExportLanguageService(typeof(ISyntaxClassificationService), LanguageNames.CSharp), Shared]
[ExportLanguageService(typeof(ISyntaxClassificationService), LanguageNames.CSharp), Export, Shared]
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class CSharpSyntaxClassificationService() : AbstractSyntaxClassificationService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

namespace Microsoft.CodeAnalysis.Classification;

internal abstract class AbstractClassificationService : IClassificationService
internal abstract class AbstractClassificationService(ISyntaxClassificationService syntaxClassificationService) : IClassificationService
{
private readonly ISyntaxClassificationService _syntaxClassificationService = syntaxClassificationService;

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 @@ -192,8 +194,7 @@ public void AddSyntacticClassifications(
if (root is null)
return;

var classificationService = services.GetLanguageServices(root.Language).GetRequiredService<ISyntaxClassificationService>();
Copy link
Member Author

Choose a reason for hiding this comment

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

service-retrieval continues to be surprising expensive when done in hot spots. here we can trivially lift this out such that thsi value is provided in the constructor.

classificationService.AddSyntacticClassifications(root, textSpans, result, cancellationToken);
_syntaxClassificationService.AddSyntacticClassifications(root, textSpans, result, cancellationToken);
}

/// <summary>
Expand Down
Loading
Loading