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

Use ReadOnlyCollection.Empty #10013

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ThomasGoulet73
Copy link
Contributor

@ThomasGoulet73 ThomasGoulet73 commented Oct 29, 2024

Description

Replaces ReadOnlyCollection allocations with a shared singleton. I also replaced EmptyList with ReadOnlyCollection.Empty because it does the same thing, is a BCL API and allows to potentially allocate less ReadOnlyCollection instances.

Customer Impact

Better perf and memory usage.

Regression

None.

Testing

Local testing.

Risk

Low.

@ThomasGoulet73 ThomasGoulet73 requested review from a team as code owners October 29, 2024 02:08
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Oct 29, 2024
@@ -1868,7 +1868,7 @@ protected virtual IEnumerator<IInputElement> HostedElementsCore
if (_complexContent == null || !(_complexContent.TextContainer is TextContainer))
{
// Return empty collection
return new HostedElements(new ReadOnlyCollection<TextSegment>(new List<TextSegment>(0)));
return new HostedElements(ReadOnlyCollection<TextSegment>.Empty);
Copy link
Contributor

Choose a reason for hiding this comment

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

@ThomasGoulet73 Could you revert the changes in Textblock? #9993

Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I missed your comment when answering below. Since it's the same changes it shouldn't conflict, I think it's fine for both PRs to stay as-is.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have asked because I personally try to not do overlapping changes (even if they would be the same) with other contributors open PRs when I'm aware of them existing (usually that includes search in PRs since I've got burnt on your PR many months ago, and this one would probably would go with "ReadOnlyCollection" search.). The other time you've asked me, now I'm politely asking you.

Not trying to be difficult, you may resolve the conversation as you see fit, it was just an ask since my changes were the driving subject of that PR and now it feels a bit wasted if it's already contained here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't notice that it was one of the main points of your PR. I thought the main point of your PR was using Span and the ReadOnlyCollection change was just a "While you're there" change. It wouldn't conflict because it's the same change but I'll revert it because I don't want to "erase" your work if my PR is merged first.

@@ -224,7 +224,7 @@ internal IEnumerable<IManipulator> GetManipulatorsReadOnly()
}
else
{
return new ReadOnlyCollection<IManipulator>(new List<IManipulator>(2));
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't checked the whole code but unlike all of the other changes, this original line is allocating a list with a size of 2 and yours is empty. Can this become a problem?

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 don't believe this is a problem, the 2 here is for the initial capacity of the list which doesn't affect the size and thus doesn't affect the ReadOnlyCollection. Here's a small test:
https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7AHwAEAmABgFgAoUgRmqLIAIjaUBuB51gOgGEIAG0EwwGAJYQ8AZx4B5YACtRGALIQAJjEGcqDWgE4AFHhgB3JgCUYAQw1y8ggJ4DhKyXgA8EJSoB8JuZMADLi0hjevmIBJACUsfwQ+BixuqzG1nYOzq4iYh6RytE8AKIAtgAOGE6JyalAA

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this ain't an issue unless you're doing dark magic with the wrapped collection. It was just a wasted memory to begin with.

…yCollection.Empty

# Conflicts:
#	src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/MemberReflector.cs
#	src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlMember.cs
@ThomasGoulet73
Copy link
Contributor Author

I pulled main to fix the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions Included in test pass PR metadata: Label to tag PRs, to facilitate with triage Status:Completed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants