Avoid unneeded Dictionary operations (#18890)

This commit is contained in:
Henrik
2025-04-07 15:02:28 +02:00
committed by GitHub
parent 1d9a031c1c
commit 2723e4f77c
7 changed files with 18 additions and 37 deletions

View File

@@ -160,9 +160,9 @@ public class ObservableDictionary<TKey, TValue> : ObservableCollection<TValue>,
if (index != Count)
{
foreach (TKey k in Indecies.Keys.Where(k => Indecies[k] >= index).ToList())
foreach (KeyValuePair<TKey, int> largerOrEqualToIndex in Indecies.Where(kvp => kvp.Value >= index))
{
Indecies[k]++;
Indecies[largerOrEqualToIndex.Key] = largerOrEqualToIndex.Value + 1;
}
}
@@ -185,9 +185,9 @@ public class ObservableDictionary<TKey, TValue> : ObservableCollection<TValue>,
Indecies.Remove(key);
foreach (TKey k in Indecies.Keys.Where(k => Indecies[k] > index).ToList())
foreach (KeyValuePair<TKey, int> largerThanIndex in Indecies.Where(kvp => kvp.Value > index))
{
Indecies[k]--;
Indecies[largerThanIndex.Key] = largerThanIndex.Value - 1;
}
}

View File

@@ -197,7 +197,7 @@ public class Content : ContentBase, IContent
/// <inheritdoc />
[IgnoreDataMember]
public IEnumerable<string> PublishedCultures => _publishInfos?.Keys ?? Enumerable.Empty<string>();
public IEnumerable<string> PublishedCultures => _publishInfos?.Keys ?? [];
/// <inheritdoc />
public bool IsCulturePublished(string culture)

View File

@@ -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<Guid, NavigationNode> navigationStructure, Guid childKey)
@@ -53,8 +50,5 @@ public sealed class NavigationNode
_children.Remove(childKey);
child.Parent = null;
// Update the navigation structure
navigationStructure[childKey] = child;
}
}

View File

@@ -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

View File

@@ -228,19 +228,11 @@ internal class MemberTypeRepository : ContentTypeRepositoryBase<IMemberType>, 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;
}
}
}

View File

@@ -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<PublishStatusDto>? databaseRecords = await Database.FetchAsync<PublishStatusDto>(sql);
IDictionary<Guid, ISet<string>> result = Map(databaseRecords);
return result.ContainsKey(documentKey) ? result[documentKey] : new HashSet<string>();
return result.TryGetValue(documentKey, out ISet<string>? value) ? value : new HashSet<string>();
}
public async Task<IDictionary<Guid, ISet<string>>> GetDescendantsOrSelfPublishStatusAsync(Guid rootDocumentKey, CancellationToken cancellationToken)
@@ -98,7 +96,7 @@ public class PublishStatusRepository: IPublishStatusRepository
x=> (ISet<string>) 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; }
}
}

View File

@@ -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<string> _oathCallbackErrorParams = new string[] { "error", "error_description" };
private static readonly string[] _oathCallbackErrorParams = ["error", "error_description"];
private readonly IOptions<SecuritySettings> _securitySettings;
@@ -43,7 +44,7 @@ public class OAuthOptionsHelper
SetUmbracoRedirectWithFilteredParams(context, providerFriendlyName, eventName)
.HandleResponse();
return Task.FromResult(0);
return Task.CompletedTask;
}
/// <summary>
@@ -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}");
}
}