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

Reduce allocations during typing #75351

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
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 @@ -6,17 +6,16 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;

namespace Roslyn.Utilities
{
internal static class TextChangeRangeExtensions
{
public static TextChangeRange? Accumulate(this TextChangeRange? accumulatedTextChangeSoFar, IEnumerable<TextChangeRange> changesInNextVersion)
public static TextChangeRange? Accumulate(this TextChangeRange? accumulatedTextChangeSoFar, IReadOnlyList<TextChangeRange> changesInNextVersion)
{
if (!changesInNextVersion.Any())
if (changesInNextVersion.Count == 0)
{
return accumulatedTextChangeSoFar;
}
Expand All @@ -25,7 +24,9 @@ internal static class TextChangeRangeExtensions
// we could apply each one individually like we do in SyntaxDiff::ComputeSpansInNew by calculating delta
// between each change in changesInNextVersion which is already sorted in its textual position ascending order.
// but end result will be same as just applying it once with encompassed text change range.
var newChange = TextChangeRange.Collapse(changesInNextVersion);
var newChange = changesInNextVersion.Count == 1
? changesInNextVersion[0]
: TextChangeRange.Collapse(changesInNextVersion);
Comment on lines +27 to +29
Copy link
Member

Choose a reason for hiding this comment

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

Should this optimization be moved inside TextChangeRange.Collapse itself?

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 considered that, but that method has several usages in which the caller only has an IEnumerable. I instead chose to limit the changes to the caller that was showing up in the performance profile I was looking at.


// no previous accumulated change, return the new value.
if (accumulatedTextChangeSoFar == null)
Expand Down
38 changes: 29 additions & 9 deletions src/EditorFeatures/Text/ITextImageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.VisualStudio.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -51,35 +52,54 @@ public static IReadOnlyList<TextChangeRange> GetChangeRanges(ITextImageVersion o
}

if (changes == null)
{
return [];
}
else

var builder = ArrayBuilder<TextChangeRange>.GetInstance(changes.Count);
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < changes.Count; i++)
{
return ImmutableArray.CreateRange(changes.Select(forward ? s_forwardTextChangeRange : s_backwardTextChangeRange));
var change = changes[i];
builder.Add(forward ? s_forwardTextChangeRange(change) : s_backwardTextChangeRange(change));
}

return builder.ToImmutableAndFree();
}

private static TextChangeRange GetChangeRanges(ITextImageVersion oldVersion, ITextImageVersion newVersion, bool forward)
{
TextChangeRange? range = null;
var iterator = GetMultipleVersionTextChanges(oldVersion, newVersion, forward);
foreach (var changes in forward ? iterator : iterator.Reverse())
using var _ = ArrayBuilder<ArrayBuilder<TextChangeRange>>.GetInstance(out var builder);

GetMultipleVersionTextChanges(oldVersion, newVersion, forward, builder);
foreach (var changes in builder)
{
range = range.Accumulate(changes);
changes.Free();
ToddGrun marked this conversation as resolved.
Show resolved Hide resolved
}

RoslynDebug.Assert(range.HasValue);
return range.Value;
}

private static IEnumerable<IEnumerable<TextChangeRange>> GetMultipleVersionTextChanges(
ITextImageVersion oldVersion, ITextImageVersion newVersion, bool forward)
private static void GetMultipleVersionTextChanges(
ITextImageVersion oldVersion,
ITextImageVersion newVersion,
bool forward,
ArrayBuilder<ArrayBuilder<TextChangeRange>> builder)
{
for (var version = oldVersion; version != newVersion; version = version.Next)
{
yield return version.Changes.Select(forward ? s_forwardTextChangeRange : s_backwardTextChangeRange);
var changes = ArrayBuilder<TextChangeRange>.GetInstance(version.Changes.Count);
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < version.Changes.Count; i++)
{
var change = version.Changes[i];
changes.Add(forward ? s_forwardTextChangeRange(change) : s_backwardTextChangeRange(change));
}

builder.Add(changes);
}

if (!forward)
builder.ReverseContents();
}

private static TextChangeRange CreateTextChangeRange(ITextChange change, bool forward)
Expand Down