Skip to content

Commit

Permalink
perf: Enable LINQ-related performance analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Sep 6, 2022
1 parent 95f45dc commit 6ae0751
Show file tree
Hide file tree
Showing 30 changed files with 46 additions and 40 deletions.
7 changes: 5 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ dotnet_diagnostic.RS0030.severity = error
dotnet_analyzer_diagnostic.category-Performance.severity = none #error - Uncomment when all violations are fixed.

dotnet_diagnostic.CA1805.severity = error
dotnet_diagnostic.CA1826.severity = error
dotnet_diagnostic.CA1827.severity = error
dotnet_diagnostic.CA1828.severity = error
dotnet_diagnostic.CA1829.severity = error

dotnet_diagnostic.CA1061.severity = none
dotnet_diagnostic.CA1802.severity = none
dotnet_diagnostic.CA1014.severity = none
Expand Down Expand Up @@ -290,7 +295,6 @@ dotnet_diagnostic.CA1508.severity = none
dotnet_diagnostic.CA2008.severity = none
dotnet_diagnostic.CA1810.severity = none
dotnet_diagnostic.CA1309.severity = none
dotnet_diagnostic.CA1829.severity = none
dotnet_diagnostic.CA3075.severity = none
dotnet_diagnostic.CA1815.severity = none
dotnet_diagnostic.CA1819.severity = none
Expand All @@ -302,7 +306,6 @@ dotnet_diagnostic.CA2214.severity = none
dotnet_diagnostic.CA1033.severity = none
dotnet_diagnostic.CA2213.severity = none
dotnet_diagnostic.CA1068.severity = none
dotnet_diagnostic.CA1826.severity = none
dotnet_diagnostic.CA2016.severity = none
dotnet_diagnostic.CA1835.severity = none
dotnet_diagnostic.CA1069.severity = none
Expand Down
3 changes: 2 additions & 1 deletion src/SamplesApp/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[*.cs]
dotnet_analyzer_diagnostic.category-Performance.severity = none
dotnet_diagnostic.CA1805.severity = none
dotnet_diagnostic.CA1805.severity = none
dotnet_diagnostic.CA1036.severity = none
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ from sample in category.SamplesContent

if (_log.IsEnabled(LogLevel.Debug))
{
_log.Debug($"Generating tests for {tests.Count()} test in {folderName}");
_log.Debug($"Generating tests for {tests.Length} test in {folderName}");
}

var target = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override Style SelectStyleCore(object item, DependencyObject container
return FirstItemStyle;
}

var itemsCount = itemsControl.Items?.Count() ?? (itemsControl.ItemsSource as IEnumerable)?.Cast<object>().Count() ?? 0;
var itemsCount = itemsControl.Items?.Count ?? (itemsControl.ItemsSource as IEnumerable)?.Cast<object>().Count() ?? 0;
if (index == itemsCount - 1)
{
return LastItemStyle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void TryGetCompatibleXamlNamespace ()
public void GetXamlTypeAndAllXamlTypes ()
{
var ctx = new XamlSchemaContext (new Assembly [] {typeof (string).Assembly}); // build with corlib.
Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count (), "#0"); // premise
Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count, "#0"); // premise

var xt = ctx.GetXamlType (typeof (string));
Assert.IsNotNull (xt, "#1");
Expand All @@ -176,7 +176,7 @@ public void GetXamlTypeAndAllXamlTypes ()
Assert.IsTrue (object.ReferenceEquals (ctx.GetXamlType (GetType ()), ctx.GetXamlType (GetType ())), "#4");

// after getting these types, it still returns 0. So it's not all about caching.
Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count (), "#5");
Assert.AreEqual (0, ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace).Count, "#5");
}

[Test]
Expand All @@ -197,7 +197,7 @@ public void GetAllXamlTypesInXaml2006Namespace ()
// There are some special types that have non-default name: MemberDefinition, PropertyDefinition

var l = ctx.GetAllXamlTypes (XamlLanguage.Xaml2006Namespace);
Assert.IsTrue (l.Count () > 40, "#1");
Assert.IsTrue (l.Count > 40, "#1");
Assert.IsTrue (l.Any (t => t.UnderlyingType == typeof (MemberDefinition)), "#2");
Assert.IsTrue (l.Any (t => t.Name == "AmbientAttribute"), "#3");
Assert.IsTrue (l.Any (t => t.Name == "XData"), "#4");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void CalculateSize()
{
string[] storageFiles = Directory.GetFiles(StorageFolder, "*.*");

_storageCountFiles = storageFiles.Count();
_storageCountFiles = storageFiles.Length;

long storageSizeInBytes = 0;
foreach (string file in storageFiles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ private string[] GetResourceKeys(CancellationToken ct)
}

#if DEBUG
Console.Write(resourceKeys.Count() + " localization keys found");
Console.Write(resourceKeys.Length + " localization keys found");
#endif
return resourceKeys;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3263,7 +3263,7 @@ private void BuildExtendedProperties(IIndentedStringBuilder outerwriter, XamlObj
closureName
);

if (member.Objects.Count() == 1)
if (member.Objects.Count == 1)
{
BuildChild(writer, member, member.Objects.First());
}
Expand Down Expand Up @@ -6217,7 +6217,7 @@ private string GenerateConstructorParameters(XamlType xamlType)
if (type != null)
{
var q = from m in type.Constructors
where m.Parameters.Count() == 1
where m.Parameters.Length == 1
where SymbolEqualityComparer.Default.Equals(m.Parameters.First().Type, _androidContentContextSymbol)
select m;

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Foundation/UriExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static IDictionary<string, string> GetParameters(this Uri uri)
.OriginalString
.Split(new[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Split(new[] { '=' }))
.Where(parts => parts.Count() > 1)
.Where(parts => parts.Length > 1)
.ToDictionary(parts => parts[0], parts => String.Join("=", parts.Skip(1)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2556,7 +2556,7 @@ await RunOnUIThread(() =>
});


for (var i = 0; i < testData.snapPoints.Count(); i++)
for (var i = 0; i < testData.snapPoints.Length; i++)
{
LOG_OUTPUT("scroll down to next snap point and wait for viewchanged event.");
TestServices.InputHelper.ScrollMouseWheel(cv, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ await RunOnUIThread.ExecuteAsync(() =>

await RunOnUIThread.ExecuteAsync(() =>
{
var flyout = VisualTreeHelper.GetOpenPopups(Window.Current).Last();
var openPopups = VisualTreeHelper.GetOpenPopups(Window.Current);
var flyout = openPopups[openPopups.Count - 1];
Verify.IsNotNull(flyout, "Flyout could not be retrieved");
var ellipsisItemsRepeater = TestUtilities.FindDescendents<ItemsRepeater>(flyout).Single();
Verify.IsNotNull(ellipsisItemsRepeater, "The underlying flyout items repeater (1) could not be retrieved");
Expand Down Expand Up @@ -388,7 +389,8 @@ await RunOnUIThread.ExecuteAsync(() =>

await RunOnUIThread.ExecuteAsync(() =>
{
var flyout = VisualTreeHelper.GetOpenPopups(Window.Current).Last();
var openPopups = VisualTreeHelper.GetOpenPopups(Window.Current);
var flyout = openPopups[openPopups.Count - 1];
Verify.IsNotNull(flyout, "Flyout could not be retrieved");
var ellipsisItemsRepeater = TestUtilities.FindDescendents<ItemsRepeater>(flyout).Single();
Verify.IsNotNull(ellipsisItemsRepeater, "The underlying flyout items repeater (1) could not be retrieved");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1375,9 +1375,9 @@ private void ValidateSelection(
Verify.AreEqual(selectionModel.SelectedItem, GetData(selectionModel, expectedSelected[0]));
}

int itemsCount = selectionModel.SelectedItems.Count();
int itemsCount = selectionModel.SelectedItems.Count;
Verify.AreEqual(selectionModel.Source != null ? expectedSelected.Count - selectedInnerNodes : 0, itemsCount);
int indicesCount = selectionModel.SelectedIndices.Count();
int indicesCount = selectionModel.SelectedIndices.Count;
Verify.AreEqual(expectedSelected.Count - selectedInnerNodes, indicesCount);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Test_Chinese_With_Script_Subtag()
{
ApplicationLanguages.PrimaryLanguageOverride = "zh-Hans-CN";

ApplicationLanguages.Languages.First().Should().Be("zh-Hans-CN");
ApplicationLanguages.Languages[0].Should().Be("zh-Hans-CN");
CultureInfo.CurrentCulture.Name.Should().BeOneOf("zh-CN", "zh-Hans-CN");
CultureInfo.CurrentUICulture.Name.Should().BeOneOf("zh-CN", "zh-Hans-CN");
}
Expand All @@ -30,7 +30,7 @@ public void Test_French_With_Script_Subtag()
{
ApplicationLanguages.PrimaryLanguageOverride = "fr-Latn-CA";

ApplicationLanguages.Languages.First().Should().Be("fr-Latn-CA");
ApplicationLanguages.Languages[0].Should().Be("fr-Latn-CA");
CultureInfo.CurrentCulture.Name.Should().BeOneOf("fr-CA", "fr-Latn-CA");
CultureInfo.CurrentUICulture.Name.Should().BeOneOf("fr-CA", "fr-Latn-CA");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ private static void AssertSegmentsMatch(ExpectedSegment[] expectedSegments, IRea
if (expected.LineBreakLength == 2)
{
Assert.AreEqual(expected.Text.Length - 1, result.Glyphs.Count);
Assert.AreEqual(start + expected.Text.Length - 2, result.Glyphs.Last().Cluster);
Assert.AreEqual(start + expected.Text.Length - 2, result.Glyphs[result.Glyphs.Count - 1].Cluster);
}
else
{
Assert.AreEqual(expected.Text.Length, result.Glyphs.Count);
Assert.AreEqual(start + expected.Text.Length - 1, result.Glyphs.Last().Cluster);
Assert.AreEqual(start + expected.Text.Length - 1, result.Glyphs[result.Glyphs.Count - 1].Cluster);
}

Assert.AreEqual(start, result.Glyphs[0].Cluster);
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI.TestComparer/AppCenterTestsDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task Download(string testCloudApiKey, string basePath, int runLimit

foreach (var run in validRuns.Select((v, i) => new { Index = i, Value = v }))
{
Console.WriteLine($"Getting run {run.Index + 1} of {validRuns.Count()} for {run.Value.Platform} at {run.Value.Date}...");
Console.WriteLine($"Getting run {run.Index + 1} of {validRuns.Length} for {run.Value.Platform} at {run.Value.Date}...");

var runName = $"{run.Value.Date:yyyyMMdd-hhmmss}-{run.Value.Id}";
var fullPath = Path.Combine(basePath, run.Value.Platform, runName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public void When_Enumerate_ChildNodes()
{
Assert.IsInstanceOfType(childNode, typeof(XmlElement));
}
Assert.AreEqual("title", childNodes.First().NodeName);
Assert.AreEqual("para", childNodes.Last().NodeName);
Assert.AreEqual("title", childNodes[0].NodeName);
Assert.AreEqual("para", childNodes[(int)childNodes.Length - 1].NodeName);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void When_UsingVariousLanguages(string format, string language, string ex
{
var sut = new DateTimeFormatter(format, new[] {language});

var firstPattern = sut.Patterns.First();
var firstPattern = sut.Patterns[0];

using var _ = new AssertionScope();

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/Controls/NativeFramePresenter.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ bool CorrelatesNavigatingRequest()
if (newItem != null
&& e.NewStartingIndex == (collection.Count - 1)
&& (frameRequest.NavigationMode == NavigationMode.New || frameRequest.NavigationMode == NavigationMode.Forward)
&& newItem.SourcePageType == frameRequest.BackStackPageTypes.LastOrDefault())
&& newItem.SourcePageType == (frameRequest.BackStackPageTypes.Count == 0 ? null : frameRequest.BackStackPageTypes[frameRequest.BackStackPageTypes.Count - 1]))
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/Controls/UIFontHelper.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private static UIFont GetFontFromFamilyName(nfloat size, string familyName)
{
//If only one font exists for this family name, use it. Otherwise we will need to inspect the file for the right font name
var fontNames = UIFont.FontNamesForFamilyName(familyName);
return fontNames.Count() == 1 ? UIFont.FromName(fontNames[0], size) : null;
return fontNames.Length == 1 ? UIFont.FromName(fontNames[0], size) : null;
}

private static UIFont GetFontFromFile(nfloat size, string file)
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/DirectUI/TrackableDateCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ internal void CountOf(DateTime value, out uint pCount)
uint count = 0;
pCount = 0;

for (uint i = 0; i < m_vector.Count(); ++i)
for (uint i = 0; i < m_vector.Count; ++i)
{
if (m_areEquivalentComparer(m_vector[(int)i], value))
{
Expand All @@ -177,7 +177,7 @@ internal void CountOf(DateTime value, out uint pCount)
internal void RemoveAll(DateTime value, uint? pFromHint = null)
{
int from = (int)(pFromHint.HasValue ? pFromHint : 0);
int i = (int)(m_vector.Count()) - 1;
int i = (int)(m_vector.Count) - 1;

for (; i >= from; --i)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public NativeFramePresenterUIGestureRecognizerDelegate(Func<UINavigationControll
}
public override bool ShouldBegin(UIGestureRecognizer recognizer)
{
return _navigationController()?.ViewControllers?.Count() > 1;
return _navigationController()?.ViewControllers?.Length > 1;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/ItemCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public bool Remove(object item)
}
}

public int Count => _itemsSource == null ? _inner.Count : _itemsSource.Count();
public int Count => _itemsSource == null ? _inner.Count : _itemsSource.Count;

public uint Size => (uint)Count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ partial void UpdateItems()
_adapter?.NotifyDataSetChanged();
if (_pager != null)
{
_pager.OffscreenPageLimit = Items.Count();
_pager.OffscreenPageLimit = Items.Count;
}
_tabStrip?.Update();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/Pivot/PivotAdapter.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override AndroidX.Fragment.App.Fragment GetItem(int position)

public override int Count
{
get { return _fragments.Count(); }
get { return _fragments.Count; }
}

public override Java.Lang.ICharSequence GetPageTitleFormatted(int position)
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Data/CollectionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public int Count

foreach (ICollectionViewGroup group in CollectionGroups)
{
count += group.GroupItems.Count();
count += group.GroupItems.Count;
}

return count;
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Globalization/ApplicationLanguages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void ApplyLanguages()
Languages = languages.Distinct().ToArray();
}

var primaryLanguage = Languages.FirstOrDefault();
var primaryLanguage = Languages.Count > 0 ? Languages[0] : null;

if(primaryLanguage is not null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static IReadOnlyList<HostName> GetHostNames()
string androCanonical = interfaceAddress.Address.CanonicalHostName;
// seems like == androCanonical
string androHostName = interfaceAddress.Address.HostName;
bool androIPv46 = (interfaceAddress.Address.GetAddress().Count() == 4);
bool androIPv46 = (interfaceAddress.Address.GetAddress().Length == 4);

// we have all required data from Android, and we can use them
HostName newHost = new HostName();
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Storage/Pickers/FileOpenPicker.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static bool TryHandleIntent(Intent intent, Result resultCode)
private async Task<StorageFile?> PickSingleFileTaskAsync(CancellationToken token)
{
var files = await PickFilesAsync(false, token);
return files.FirstOrDefault();
return files.Count == 0 ? null : files[0];
}

private async Task<IReadOnlyList<StorageFile>> PickMultipleFilesTaskAsync(CancellationToken token)
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Storage/Pickers/FileOpenPicker.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class FileOpenPicker
private async Task<StorageFile?> PickSingleFileTaskAsync(CancellationToken token)
{
var files = await PickFilesAsync(false, token);
return files.FirstOrDefault();
return files.Count == 0 ? null : files[0];
}

private async Task<IReadOnlyList<StorageFile>> PickMultipleFilesTaskAsync(CancellationToken token) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Storage/Pickers/FileOpenPicker.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static bool IsNativePickerSupported()
private async Task<StorageFile?> PickSingleFileTaskAsync(CancellationToken token)
{
var files = await PickFilesAsync(false, token);
return files.FirstOrDefault();
return files.Count > 0 ? files[0] : null;
}

private async Task<IReadOnlyList<StorageFile>> PickMultipleFilesTaskAsync(CancellationToken token)
Expand Down

0 comments on commit 6ae0751

Please sign in to comment.