From 822c8374ef48c2c4a02540cbf1f7cc991bbec1dc Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Mon, 31 Mar 2025 14:12:54 +0200 Subject: [PATCH] Avoid allocating new empty arrays, reuse existing empty array --- .../Services/ApiMediaQueryService.cs | 2 +- src/Umbraco.Core/Models/Membership/User.cs | 8 ++++---- .../Repositories/Implement/RelationRepository.cs | 4 ++-- .../Repositories/Implement/RelationTypeRepository.cs | 8 ++++---- .../Security/BackOfficeIdentityUser.cs | 2 +- .../Security/BackOfficeUserStore.cs | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Umbraco.Cms.Api.Delivery/Services/ApiMediaQueryService.cs b/src/Umbraco.Cms.Api.Delivery/Services/ApiMediaQueryService.cs index 0eeeb4d6da..15ecaa1423 100644 --- a/src/Umbraco.Cms.Api.Delivery/Services/ApiMediaQueryService.cs +++ b/src/Umbraco.Cms.Api.Delivery/Services/ApiMediaQueryService.cs @@ -185,7 +185,7 @@ internal sealed class ApiMediaQueryService : IApiMediaQueryService } - private Attempt, ApiMediaQueryOperationStatus> PagedResult(IEnumerable children, int skip, int take) + private static Attempt, ApiMediaQueryOperationStatus> PagedResult(IEnumerable children, int skip, int take) { IPublishedContent[] childrenAsArray = children as IPublishedContent[] ?? children.ToArray(); var result = new PagedModel diff --git a/src/Umbraco.Core/Models/Membership/User.cs b/src/Umbraco.Core/Models/Membership/User.cs index b51d207aa3..487f9c4209 100644 --- a/src/Umbraco.Core/Models/Membership/User.cs +++ b/src/Umbraco.Core/Models/Membership/User.cs @@ -53,8 +53,8 @@ public class User : EntityBase, IUser, IProfile _language = globalSettings.DefaultUILanguage; _isApproved = true; _isLockedOut = false; - _startContentIds = new int[] { }; - _startMediaIds = new int[] { }; + _startContentIds = []; + _startMediaIds = []; // cannot be null _rawPasswordValue = string.Empty; @@ -101,8 +101,8 @@ public class User : EntityBase, IUser, IProfile _userGroups = new HashSet(); _isApproved = true; _isLockedOut = false; - _startContentIds = new int[] { }; - _startMediaIds = new int[] { }; + _startContentIds = []; + _startMediaIds = []; } /// diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs index a38bf4547f..ec06c5f86e 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs @@ -34,10 +34,10 @@ internal class RelationRepository : EntityRepositoryBase, IRelat } public IEnumerable GetPagedParentEntitiesByChildId(int childId, long pageIndex, int pageSize, out long totalRecords, params Guid[] entityTypes) - => GetPagedParentEntitiesByChildId(childId, pageIndex, pageSize, out totalRecords, new int[0], entityTypes); + => GetPagedParentEntitiesByChildId(childId, pageIndex, pageSize, out totalRecords, [], entityTypes); public IEnumerable GetPagedChildEntitiesByParentId(int parentId, long pageIndex, int pageSize, out long totalRecords, params Guid[] entityTypes) - => GetPagedChildEntitiesByParentId(parentId, pageIndex, pageSize, out totalRecords, new int[0], entityTypes); + => GetPagedChildEntitiesByParentId(parentId, pageIndex, pageSize, out totalRecords, [], entityTypes); public Task> GetPagedByChildKeyAsync(Guid childKey, int skip, int take, string? relationTypeAlias) { diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationTypeRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationTypeRepository.cs index af7458bab0..8c84f2ae65 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationTypeRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationTypeRepository.cs @@ -17,7 +17,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement; /// /// Represents a repository for doing CRUD operations for /// -internal class RelationTypeRepository : EntityRepositoryBase, IRelationTypeRepository +internal sealed class RelationTypeRepository : EntityRepositoryBase, IRelationTypeRepository { public RelationTypeRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger logger) : base(scopeAccessor, cache, logger) @@ -27,7 +27,7 @@ internal class RelationTypeRepository : EntityRepositoryBase protected override IRepositoryCachePolicy CreateCachePolicy() => new FullDataSetRepositoryCachePolicy(GlobalIsolatedCache, ScopeAccessor, GetEntityId, /*expires:*/ true); - private void CheckNullObjectTypeValues(IRelationType entity) + private static void CheckNullObjectTypeValues(IRelationType entity) { if (entity.ParentObjectType.HasValue && entity.ParentObjectType == Guid.Empty) { @@ -66,12 +66,12 @@ internal class RelationTypeRepository : EntityRepositoryBase public IEnumerable GetMany(params Guid[]? ids) { // should not happen due to the cache policy - if (ids?.Any() ?? false) + if (ids is { Length: not 0 }) { throw new NotImplementedException(); } - return GetMany(new int[0]); + return GetMany(Array.Empty()); } protected override IEnumerable PerformGetByQuery(IQuery query) diff --git a/src/Umbraco.Infrastructure/Security/BackOfficeIdentityUser.cs b/src/Umbraco.Infrastructure/Security/BackOfficeIdentityUser.cs index 634641723b..c08f125955 100644 --- a/src/Umbraco.Infrastructure/Security/BackOfficeIdentityUser.cs +++ b/src/Umbraco.Infrastructure/Security/BackOfficeIdentityUser.cs @@ -63,7 +63,7 @@ public class BackOfficeIdentityUser : UmbracoIdentityUser get => _startContentIds; set { - value ??= new int[0]; + value ??= []; BeingDirty.SetPropertyValueAndDetectChanges(value, ref _startContentIds!, nameof(StartContentIds), _startIdsComparer); } diff --git a/src/Umbraco.Infrastructure/Security/BackOfficeUserStore.cs b/src/Umbraco.Infrastructure/Security/BackOfficeUserStore.cs index 588aed7f63..e86d9dfc68 100644 --- a/src/Umbraco.Infrastructure/Security/BackOfficeUserStore.cs +++ b/src/Umbraco.Infrastructure/Security/BackOfficeUserStore.cs @@ -138,8 +138,8 @@ public class BackOfficeUserStore : var userEntity = new User(_globalSettings, user.Name, user.Email, user.UserName, emptyPasswordValue) { Language = user.Culture ?? _globalSettings.DefaultUILanguage, - StartContentIds = user.StartContentIds ?? new int[] { }, - StartMediaIds = user.StartMediaIds ?? new int[] { }, + StartContentIds = user.StartContentIds ?? [], + StartMediaIds = user.StartMediaIds ?? [], IsLockedOut = user.IsLockedOut, Key = user.Key, Kind = user.Kind