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

Move to collection exprs #73009

Merged
merged 13 commits into from
Apr 19, 2024
Merged

Conversation

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 13, 2024
@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review April 13, 2024 01:40
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner April 13, 2024 01:40
@CyrusNajmabadi
Copy link
Member Author

@ToddGrun ptal

@@ -17,7 +17,7 @@ internal static void UpdateText(SourceText newText, ITextBuffer buffer, EditOpti
var oldSnapshot = buffer.CurrentSnapshot;
var oldText = oldSnapshot.AsText();
var changes = newText.GetTextChanges(oldText);
UpdateText(changes.ToImmutableArray(), buffer, oldSnapshot, oldText, options);
UpdateText([.. changes], buffer, oldSnapshot, oldText, options);
Copy link
Contributor

Choose a reason for hiding this comment

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

[.. changes]

Is this smart enough to not allocate?

Copy link
Member Author

Choose a reason for hiding this comment

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

it rewrites it to:

		int num = 0;
		TextChange[] array = new TextChange[readOnlyList.Count];
		foreach (TextChange item in readOnlyList)
		{
			array[num] = item;
			num++;
		}
		UpdateText(ImmutableCollectionsMarshal.AsImmutableArray(array), buffer, oldSnapshot, oldText, options);

which is basically ideal here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't the old code not allocate since changes is actually an ImmutableArray?

    public static ImmutableArray<TSource> ToImmutableArray<TSource>(this IEnumerable<TSource> items)
    {
        if (items is ImmutableArray<TSource>)
        {
            return (ImmutableArray<TSource>)items;
        }

        return CreateRange(items);
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I see. This benefitted from a runtime check. That's a tougher call. Will have to think about that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Talked with Cyrus about this, and while there is a new allocation here, he's ran perf tests and no regressions appear at that level. Additionally, he's indicated that this particular regression is something that is being investigated to see if it can be addressed at the collection expression level. Not going to block anymore based on this.

@@ -45,7 +45,7 @@ public override ITypeSymbol VisitNamedType(INamedTypeSymbol symbol)
return symbol;
}

return symbol.ConstructedFrom.Construct(arguments.ToArray());
return symbol.ConstructedFrom.Construct([.. arguments]);
Copy link
Contributor

Choose a reason for hiding this comment

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

([.. arguments]);

Could we just have do ToImmutableArray on the "var arguments = " line above and not needed this?

@@ -47,7 +47,7 @@ public override ITypeSymbol VisitNamedType(INamedTypeSymbol symbol)
if (arguments.SequenceEqual(symbol.TypeArguments))
return symbol;

return symbol.ConstructedFrom.Construct(arguments.ToArray());
return symbol.ConstructedFrom.Construct([.. arguments]);
Copy link
Contributor

Choose a reason for hiding this comment

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

([.. arguments])

Same comment about maybe doing ToImmutableArray above instead

@@ -49,7 +49,7 @@ public override ITypeSymbol VisitNamedType(INamedTypeSymbol symbol)
return symbol;
}

return symbol.ConstructedFrom.Construct(arguments.ToArray());
return symbol.ConstructedFrom.Construct([.. arguments]);
Copy link
Contributor

Choose a reason for hiding this comment

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

([.. arguments]);

here too.

@@ -151,7 +151,7 @@ public async Task WaitAllAsync(Workspace? workspace, string[]? featureNames = nu
do
{
// wait for all current tasks to be done for the time given
if (Task.WaitAll(tasks.ToArray(), smallTimeout))
if (Task.WaitAll([.. tasks], smallTimeout))
Copy link
Contributor

Choose a reason for hiding this comment

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

[.. tasks]

nit: not sure if this code is high enough frequency to warrant, but with minimal hoop jumps, it looks like this could use your FixedSizeArrayBuilder and cut out one of the array allocations here.

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@CyrusNajmabadi
Copy link
Member Author

Will do in follow ups!

@CyrusNajmabadi CyrusNajmabadi merged commit e5a525d into dotnet:main Apr 19, 2024
25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the useCollection branch April 19, 2024 23:32
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 19, 2024
@dibarbet dibarbet modified the milestones: Next, 17.11 P1 Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants