Use ToArray instead of ToList

(cherry picked from commit f99378c5d3)
This commit is contained in:
CyberReiter
2022-10-12 07:51:43 +02:00
committed by Sebastiaan Janssen
parent c0d5b544f5
commit d46d5aac0d
2 changed files with 61 additions and 4 deletions

View File

@@ -486,7 +486,7 @@ WHERE cmsContentNu.nodeId IN (
Guid mediaObjectType = Constants.ObjectTypes.Media;
// remove all - if anything fails the transaction will rollback
if (contentTypeIds == null || contentTypeIds.Count == 0)
if (contentTypeIds is null || contentTypeIds.Count == 0)
{
// must support SQL-CE
Database.Execute(
@@ -513,7 +513,7 @@ WHERE cmsContentNu.nodeId IN (
// insert back - if anything fails the transaction will rollback
IQuery<IMedia> query = SqlContext.Query<IMedia>();
if (contentTypeIds != null && contentTypeIds.Count > 0)
if (contentTypeIds is not null && contentTypeIds.Count > 0)
{
query = query.WhereIn(x => x.ContentTypeId, contentTypeIds); // assume number of ctypes won't blow IN(...)
}
@@ -526,9 +526,9 @@ WHERE cmsContentNu.nodeId IN (
// the tree is locked, counting and comparing to total is safe
IEnumerable<IMedia> descendants =
_mediaRepository.GetPage(query, pageIndex++, groupSize, out total, null, Ordering.By("Path"));
var items = descendants.Select(m => GetDto(m, false, serializer)).ToList();
var items = descendants.Select(m => GetDto(m, false, serializer)).ToArray();
Database.BulkInsertRecords(items);
processed += items.Count;
processed += items.Length;
}
while (processed < total);
}

View File

@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace Umbraco.Tests.Benchmarks
{
[MemoryDiagnoser]
public class CollectionBenchmarks
{
private static readonly IEnumerable<int> _enumerable = Enumerable.Range(0, 1000);
private static readonly int[] _array = _enumerable.ToArray();
private static readonly List<int> _list = _enumerable.ToList();
[Benchmark]
public int[] ToArray()
{
return _enumerable.ToArray();
}
[Benchmark]
public List<int> ToList()
{
return _enumerable.ToList();
}
[Benchmark]
public void IterateArray()
{
foreach (int item in _array)
{
}
}
[Benchmark]
public void IterateList()
{
foreach (int item in _list)
{
}
}
//BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.2006 (21H2)
//Intel Core i9-10885H CPU 2.40GHz, 1 CPU, 16 logical and 8 physical cores
//.NET SDK= 6.0.401
// [Host] : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT
// DefaultJob : .NET 6.0.9 (6.0.922.41905), X64 RyuJIT
//
//
//| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Allocated |
//|------------- |-----------:|---------:|---------:|-------:|-------:|----------:|
//| ToArray | 503.8 ns | 5.11 ns | 4.53 ns | 0.4807 | 0.0067 | 4,024 B |
//| ToList | 1,369.0 ns | 25.38 ns | 49.50 ns | 0.4845 | 0.0134 | 4,056 B |
//| IterateArray | 244.9 ns | 3.29 ns | 2.75 ns | - | - | - |
//| IterateList | 620.5 ns | 4.45 ns | 3.95 ns | - | - | - |
}
}