From 2723e4f77cbbc1474be8473e543322bd65beebcc Mon Sep 17 00:00:00 2001 From: Henrik Date: Mon, 7 Apr 2025 15:02:28 +0200 Subject: [PATCH] Avoid unneeded Dictionary operations (#18890) --- .../Collections/ObservableDictionary.cs | 8 ++++---- src/Umbraco.Core/Models/Content.cs | 2 +- .../Models/Navigation/NavigationNode.cs | 6 ------ .../Services/LocalizedTextService.cs | 5 +---- .../Implement/MemberTypeRepository.cs | 16 ++++------------ .../Implement/PublishStatusRepository.cs | 9 +++------ .../Helpers/OAuthOptionsHelper.cs | 9 +++++---- 7 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/Umbraco.Core/Collections/ObservableDictionary.cs b/src/Umbraco.Core/Collections/ObservableDictionary.cs index 16683b3dfc..216b384e5f 100644 --- a/src/Umbraco.Core/Collections/ObservableDictionary.cs +++ b/src/Umbraco.Core/Collections/ObservableDictionary.cs @@ -160,9 +160,9 @@ public class ObservableDictionary : ObservableCollection, if (index != Count) { - foreach (TKey k in Indecies.Keys.Where(k => Indecies[k] >= index).ToList()) + foreach (KeyValuePair largerOrEqualToIndex in Indecies.Where(kvp => kvp.Value >= index)) { - Indecies[k]++; + Indecies[largerOrEqualToIndex.Key] = largerOrEqualToIndex.Value + 1; } } @@ -185,9 +185,9 @@ public class ObservableDictionary : ObservableCollection, Indecies.Remove(key); - foreach (TKey k in Indecies.Keys.Where(k => Indecies[k] > index).ToList()) + foreach (KeyValuePair largerThanIndex in Indecies.Where(kvp => kvp.Value > index)) { - Indecies[k]--; + Indecies[largerThanIndex.Key] = largerThanIndex.Value - 1; } } diff --git a/src/Umbraco.Core/Models/Content.cs b/src/Umbraco.Core/Models/Content.cs index ce8f769cad..880c7b6bd0 100644 --- a/src/Umbraco.Core/Models/Content.cs +++ b/src/Umbraco.Core/Models/Content.cs @@ -197,7 +197,7 @@ public class Content : ContentBase, IContent /// [IgnoreDataMember] - public IEnumerable PublishedCultures => _publishInfos?.Keys ?? Enumerable.Empty(); + public IEnumerable PublishedCultures => _publishInfos?.Keys ?? []; /// public bool IsCulturePublished(string culture) diff --git a/src/Umbraco.Core/Models/Navigation/NavigationNode.cs b/src/Umbraco.Core/Models/Navigation/NavigationNode.cs index d2db0c6294..f57191b9d1 100644 --- a/src/Umbraco.Core/Models/Navigation/NavigationNode.cs +++ b/src/Umbraco.Core/Models/Navigation/NavigationNode.cs @@ -39,9 +39,6 @@ public sealed class NavigationNode child.SortOrder = _children.Count; _children.Add(childKey); - - // Update the navigation structure - navigationStructure[childKey] = child; } public void RemoveChild(ConcurrentDictionary navigationStructure, Guid childKey) @@ -53,8 +50,5 @@ public sealed class NavigationNode _children.Remove(childKey); child.Parent = null; - - // Update the navigation structure - navigationStructure[childKey] = child; } } diff --git a/src/Umbraco.Core/Services/LocalizedTextService.cs b/src/Umbraco.Core/Services/LocalizedTextService.cs index df4f7b3940..343224a6e9 100644 --- a/src/Umbraco.Core/Services/LocalizedTextService.cs +++ b/src/Umbraco.Core/Services/LocalizedTextService.cs @@ -361,10 +361,7 @@ public class LocalizedTextService : ILocalizedTextService result.TryAdd(dictionaryKey, key.Value); } - if (!overallResult.ContainsKey(areaAlias)) - { - overallResult.Add(areaAlias, result); - } + overallResult.TryAdd(areaAlias, result); } // Merge English Dictionary diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberTypeRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberTypeRepository.cs index d4790a387a..61127b766b 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberTypeRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberTypeRepository.cs @@ -228,19 +228,11 @@ internal class MemberTypeRepository : ContentTypeRepositoryBase, IM ConventionsHelper.GetStandardPropertyTypeStubs(_shortStringHelper); foreach (IPropertyType propertyType in memberType.PropertyTypes) { - if (builtinProperties.ContainsKey(propertyType.Alias)) + // this reset's its current data type reference which will be re-assigned based on the property editor assigned on the next line + if (builtinProperties.TryGetValue(propertyType.Alias, out PropertyType? propDefinition)) { - // this reset's its current data type reference which will be re-assigned based on the property editor assigned on the next line - if (builtinProperties.TryGetValue(propertyType.Alias, out PropertyType? propDefinition)) - { - propertyType.DataTypeId = propDefinition.DataTypeId; - propertyType.DataTypeKey = propDefinition.DataTypeKey; - } - else - { - propertyType.DataTypeId = 0; - propertyType.DataTypeKey = default; - } + propertyType.DataTypeId = propDefinition.DataTypeId; + propertyType.DataTypeKey = propDefinition.DataTypeKey; } } } diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/PublishStatusRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/PublishStatusRepository.cs index 126a62674a..366c5c6a26 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/PublishStatusRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/PublishStatusRepository.cs @@ -1,6 +1,4 @@ using NPoco; -using Umbraco.Cms.Core.DeliveryApi; -using Umbraco.Cms.Core.Media.EmbedProviders; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Persistence.Repositories; using Umbraco.Cms.Infrastructure.Persistence.Dtos; @@ -67,7 +65,7 @@ public class PublishStatusRepository: IPublishStatusRepository List? databaseRecords = await Database.FetchAsync(sql); IDictionary> result = Map(databaseRecords); - return result.ContainsKey(documentKey) ? result[documentKey] : new HashSet(); + return result.TryGetValue(documentKey, out ISet? value) ? value : new HashSet(); } public async Task>> GetDescendantsOrSelfPublishStatusAsync(Guid rootDocumentKey, CancellationToken cancellationToken) @@ -98,7 +96,7 @@ public class PublishStatusRepository: IPublishStatusRepository x=> (ISet) x.Where(x=> IsPublished(x)).Select(y=>y.IsoCode).ToHashSet()); } - private bool IsPublished(PublishStatusDto publishStatusDto) + private static bool IsPublished(PublishStatusDto publishStatusDto) { switch ((ContentVariation)publishStatusDto.ContentTypeVariation) { @@ -112,7 +110,7 @@ public class PublishStatusRepository: IPublishStatusRepository } } - private class PublishStatusDto + private sealed class PublishStatusDto { public const string DocumentVariantPublishStatusColumnName = "variantPublished"; @@ -133,5 +131,4 @@ public class PublishStatusRepository: IPublishStatusRepository [Column(DocumentVariantPublishStatusColumnName)] public bool DocumentVariantPublishStatus { get; set; } } - } diff --git a/src/Umbraco.Web.Common/Helpers/OAuthOptionsHelper.cs b/src/Umbraco.Web.Common/Helpers/OAuthOptionsHelper.cs index 255187babc..515d3b18d8 100644 --- a/src/Umbraco.Web.Common/Helpers/OAuthOptionsHelper.cs +++ b/src/Umbraco.Web.Common/Helpers/OAuthOptionsHelper.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.OAuth; using Microsoft.Extensions.Options; +using Microsoft.Extensions.Primitives; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Extensions; @@ -14,7 +15,7 @@ public class OAuthOptionsHelper { // https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1 // we omit "state" and "error_uri" here as it hold no value in determining the message to display to the user - private static readonly IReadOnlyCollection _oathCallbackErrorParams = new string[] { "error", "error_description" }; + private static readonly string[] _oathCallbackErrorParams = ["error", "error_description"]; private readonly IOptions _securitySettings; @@ -43,7 +44,7 @@ public class OAuthOptionsHelper SetUmbracoRedirectWithFilteredParams(context, providerFriendlyName, eventName) .HandleResponse(); - return Task.FromResult(0); + return Task.CompletedTask; } /// @@ -60,9 +61,9 @@ public class OAuthOptionsHelper foreach (var oathCallbackErrorParam in _oathCallbackErrorParams) { - if (context.Request.Query.ContainsKey(oathCallbackErrorParam)) + if (context.Request.Query.TryGetValue(oathCallbackErrorParam, out StringValues paramValue)) { - callbackPath = callbackPath.AppendQueryStringToUrl($"{oathCallbackErrorParam}={context.Request.Query[oathCallbackErrorParam]}"); + callbackPath = callbackPath.AppendQueryStringToUrl($"{oathCallbackErrorParam}={paramValue}"); } }