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
Open
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 @@ -215,7 +215,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.

return ReadOnlyCollection<IManipulator>.Empty;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ internal ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out boo

if (subtrackDetails.cParas == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Get list of paragraphs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ private ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out bool

if (trackDetails.cParas == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Get list of paragraphs
Expand All @@ -597,7 +597,7 @@ private ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out bool
// cBasicColumns == 0, means that subpage content is empty
if (subpageDetails.u.complex.cBasicColumns == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Retrieve description for each column.
Expand All @@ -611,7 +611,7 @@ private ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out bool

if (trackDetails.cParas == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Get list of paragraphs
Expand Down Expand Up @@ -738,7 +738,7 @@ internal Geometry GetTightBoundingGeometryFromTextPositions(ReadOnlyCollection<C
// Figure always has one column, so we can skip getting a column from the text position range
Invariant.Assert(columns != null && columns.Count <= 1, "Columns collection is null.");
Invariant.Assert(floatingElements != null, "Floating element collection is null.");
ReadOnlyCollection<ParagraphResult> paragraphs = (columns.Count > 0) ? columns[0].Paragraphs : new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
ReadOnlyCollection<ParagraphResult> paragraphs = (columns.Count > 0) ? columns[0].Paragraphs : ReadOnlyCollection<ParagraphResult>.Empty;

if (paragraphs.Count > 0 || floatingElements.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ private ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out bool

if (trackDetails.cParas == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Get list of paragraphs
Expand All @@ -606,7 +606,7 @@ private ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out bool
// cBasicColumns == 0, means that subpage content is empty
if (subpageDetails.u.complex.cBasicColumns == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Retrieve description for each column.
Expand All @@ -620,7 +620,7 @@ private ReadOnlyCollection<ParagraphResult> GetChildrenParagraphResults(out bool

if (trackDetails.cParas == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

// Get list of paragraphs
Expand Down Expand Up @@ -747,7 +747,7 @@ internal Geometry GetTightBoundingGeometryFromTextPositions(ReadOnlyCollection<C
// Floater always has one column, so we can skip getting a column from the text position range
Invariant.Assert(columns != null && columns.Count <= 1, "Columns collection is null.");
Invariant.Assert(floatingElements != null, "Floating element collection is null.");
ReadOnlyCollection<ParagraphResult> paragraphs = (columns.Count > 0) ? columns[0].Paragraphs : new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
ReadOnlyCollection<ParagraphResult> paragraphs = (columns.Count > 0) ? columns[0].Paragraphs : ReadOnlyCollection<ParagraphResult>.Empty;

if (paragraphs.Count > 0 || floatingElements.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ internal IEnumerator<IInputElement> HostedElementsCore
else
{
// Return empty collection
return new HostedElements(new ReadOnlyCollection<TextSegment>(new List<TextSegment>(0)));
return new HostedElements(ReadOnlyCollection<TextSegment>.Empty);
}
}
}
Expand Down Expand Up @@ -572,7 +572,7 @@ internal ReadOnlyCollection<ParagraphResult> GetParagraphResultsFromColumn(IntPt

if (trackDetails.cParas == 0)
{
return new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
return ReadOnlyCollection<ParagraphResult>.Empty;
}

PTS.FSPARADESCRIPTION[] arrayParaDesc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ ReadOnlyCollection<Rect> IContentHost.GetRectangles(ContentElement child)
{
return host.GetRectangles(child);
}
return new ReadOnlyCollection<Rect>(new List<Rect>(0));
return ReadOnlyCollection<Rect>.Empty;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ internal ReadOnlyCollection<LineResult> GetLineResults()
#if TEXTPANELLAYOUTDEBUG
TextPanelDebug.IncrementCounter("TextPara.GetLines", TextPanelDebug.Category.TextView);
#endif
ReadOnlyCollection<LineResult> lines = new ReadOnlyCollection<LineResult>(new List<LineResult>(0));
ReadOnlyCollection<LineResult> lines = ReadOnlyCollection<LineResult>.Empty;

// Query paragraph details
PTS.FSTEXTDETAILS textDetails;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ internal override ReadOnlyCollection<GlyphRun> GetGlyphRuns(ITextPointer start,
}
if (IsPageMissing)
{
return new ReadOnlyCollection<GlyphRun>(new List<GlyphRun>());
return ReadOnlyCollection<GlyphRun>.Empty;
}
return _pageTextView.GetGlyphRuns(start, end);
}
Expand Down Expand Up @@ -528,7 +528,7 @@ internal override ReadOnlyCollection<TextSegment> TextSegments
{
if (!IsValid || IsPageMissing)
{
return new ReadOnlyCollection<TextSegment>(new List<TextSegment>());
return ReadOnlyCollection<TextSegment>.Empty;
}
return _pageTextView.TextSegments;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ internal override ReadOnlyCollection<TextSegment> TextSegments
// Verify that layout information is valid. Cannot continue if not valid.
if (!IsValid)
{
return new ReadOnlyCollection<TextSegment>(new List<TextSegment>());
return ReadOnlyCollection<TextSegment>.Empty;
}
return this.TextSegmentsCore;
}
Expand Down Expand Up @@ -3559,7 +3559,7 @@ private Rect GetRectangleFromContentEdge(ParagraphResult paragraphResult, ITextP
/// <summary>
/// Cached collection of ColumnResults.
/// </summary>
private static ReadOnlyCollection<ParagraphResult> _emptyParagraphCollection = new ReadOnlyCollection<ParagraphResult>(new List<ParagraphResult>(0));
private static ReadOnlyCollection<ParagraphResult> _emptyParagraphCollection = ReadOnlyCollection<ParagraphResult>.Empty;

/// <summary>
/// Cached collection of TextSegments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ internal virtual ReadOnlyCollection<GlyphRun> GetGlyphRuns(ITextPointer start, I
{
throw new InvalidOperationException(SR.TextViewInvalidLayout);
}
return new ReadOnlyCollection<GlyphRun>(new List<GlyphRun>());
return ReadOnlyCollection<GlyphRun>.Empty;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ IInputElement IContentHost.InputHitTest(Point point)
/// </summary>
ReadOnlyCollection<Rect> IContentHost.GetRectangles(ContentElement child)
{
return new ReadOnlyCollection<Rect>(new List<Rect>());
return ReadOnlyCollection<Rect>.Empty;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public IEnumerator<IInputElement> HostedElements
}
else
{
return new HostedElements(new ReadOnlyCollection<TextSegment>(new List<TextSegment>(0)));
return new HostedElements(ReadOnlyCollection<TextSegment>.Empty);
}
}
}
Expand All @@ -463,7 +463,7 @@ public ReadOnlyCollection<Rect> GetRectangles(ContentElement child)
}
else
{
return new ReadOnlyCollection<Rect>(new List<Rect>(0));
return ReadOnlyCollection<Rect>.Empty;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ protected override ReadOnlyCollection<DocumentPageView> GetPageViewsCollection(o
else
{
//Return an empty collection (null is not valid).
pageViews = new ReadOnlyCollection<DocumentPageView>(new List<DocumentPageView>(0));
pageViews = ReadOnlyCollection<DocumentPageView>.Empty;
}

return pageViews;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static DocumentViewerBase()
protected DocumentViewerBase()
: base()
{
_pageViews = new ReadOnlyCollection<DocumentPageView>(new List<DocumentPageView>());
_pageViews = ReadOnlyCollection<DocumentPageView>.Empty;
// By default text selection is enabled.
SetFlags(true, Flags.IsSelectionEnabled);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public TextChangedEventArgs(RoutedEvent id, UndoAction action, ICollection<TextC
/// <param name="id">event id</param>
/// <param name="action">UndoAction</param>
public TextChangedEventArgs(RoutedEvent id, UndoAction action)
: this(id, action, new ReadOnlyCollection<TextChange>(new List<TextChange>()))
: this(id, action, ReadOnlyCollection<TextChange>.Empty)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ protected virtual ReadOnlyCollection<Rect> GetRectanglesCore(ContentElement chil
throw new ArgumentNullException("child");
}

ReadOnlyCollection<Rect> rectangles = new ReadOnlyCollection<Rect>(new List<Rect>(0));
ReadOnlyCollection<Rect> rectangles = ReadOnlyCollection<Rect>.Empty;
if (IsLayoutDataValid)
{
// Here we must call the internal function on document page that tells it whether or not
Expand Down Expand Up @@ -1019,7 +1019,7 @@ protected virtual IEnumerator<IInputElement> HostedElementsCore
else
{
// Return empty collection
hostedElements = new HostedElements(new ReadOnlyCollection<TextSegment>(new List<TextSegment>(0)));
hostedElements = new HostedElements(ReadOnlyCollection<TextSegment>.Empty);
}
return hostedElements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#nullable disable

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Markup;
Expand Down Expand Up @@ -91,7 +92,7 @@ internal static MemberReflector UnknownReflector
s_UnknownReflector._typeConverter.Value = null;
s_UnknownReflector._valueSerializer.Value = null;

s_UnknownReflector.DependsOn = XamlType.EmptyList<XamlMember>.Value;
s_UnknownReflector.DependsOn = ReadOnlyCollection<XamlMember>.Empty;
s_UnknownReflector.Invoker = XamlMemberInvoker.UnknownInvoker;
s_UnknownReflector.Type = XamlLanguage.Object;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#nullable disable

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
Expand Down Expand Up @@ -346,7 +347,7 @@ public IList<XamlMember> DependsOn
EnsureReflector();
if (_reflector.DependsOn is null)
{
_reflector.DependsOn = LookupDependsOn() ?? XamlType.EmptyList<XamlMember>.Value;
_reflector.DependsOn = LookupDependsOn() ?? ReadOnlyCollection<XamlMember>.Empty;
}
return _reflector.DependsOn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public IList<XamlType> AllowedContentTypes
if (_reflector.AllowedContentTypes is null)
{
_reflector.AllowedContentTypes = LookupAllowedContentTypes() ??
EmptyList<XamlType>.Value;
ReadOnlyCollection<XamlType>.Empty;
}
return _reflector.AllowedContentTypes;
}
Expand All @@ -251,7 +251,7 @@ public IList<XamlType> ContentWrappers
if (_reflector.ContentWrappers is null)
{
_reflector.ContentWrappers = LookupContentWrappers() ??
EmptyList<XamlType>.Value;
ReadOnlyCollection<XamlType>.Empty;
}
return _reflector.ContentWrappers;
}
Expand Down Expand Up @@ -598,7 +598,7 @@ internal ICollection<XamlMember> GetAllExcludedReadOnlyMembers()
EnsureReflector();
if (_reflector.ExcludedReadOnlyMembers is null)
{
_reflector.ExcludedReadOnlyMembers = LookupAllExcludedReadOnlyMembers() ?? EmptyList<XamlMember>.Value;
_reflector.ExcludedReadOnlyMembers = LookupAllExcludedReadOnlyMembers() ?? ReadOnlyCollection<XamlMember>.Empty;
}
return _reflector.ExcludedReadOnlyMembers;
}
Expand All @@ -609,7 +609,7 @@ internal IEnumerable<ConstructorInfo> GetConstructors()
{
if (UnderlyingType is null)
{
return EmptyList<ConstructorInfo>.Value;
return ReadOnlyCollection<ConstructorInfo>.Empty;
}
if (IsPublic)
{
Expand Down Expand Up @@ -680,7 +680,7 @@ protected virtual XamlMember LookupAliasedProperty(XamlDirective directive)

protected virtual IList<XamlType> LookupAllowedContentTypes()
{
IList<XamlType> contentWrappers = ContentWrappers ?? EmptyList<XamlType>.Value;
IList<XamlType> contentWrappers = ContentWrappers ?? ReadOnlyCollection<XamlType>.Empty;
List<XamlType> result = new List<XamlType>(contentWrappers.Count + 1);
result.Add(ItemType);

Expand Down Expand Up @@ -1461,7 +1461,7 @@ internal static ReadOnlyCollection<T> GetReadOnly<T>(IList<T> list)
{
return new ReadOnlyCollection<T>(list);
}
return EmptyList<T>.Value;
return ReadOnlyCollection<T>.Empty;
}

private static ReadOnlyCollection<XamlType> GetTypeArguments(IList<XamlType> typeArguments)
Expand Down Expand Up @@ -1823,11 +1823,5 @@ private static bool TypeArgumentsAreEqual(XamlType xamlType1, XamlType xamlType2
}

#endregion

internal static class EmptyList<T>
{
public static readonly ReadOnlyCollection<T> Value =
new ReadOnlyCollection<T>(Array.Empty<T>());
}
}
}