Skip to content

Commit 2723e4f

Browse files
authored
Avoid unneeded Dictionary operations (#18890)
1 parent 1d9a031 commit 2723e4f

File tree

7 files changed

+18
-37
lines changed

7 files changed

+18
-37
lines changed

src/Umbraco.Core/Collections/ObservableDictionary.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ protected override void InsertItem(int index, TValue item)
160160

161161
if (index != Count)
162162
{
163-
foreach (TKey k in Indecies.Keys.Where(k => Indecies[k] >= index).ToList())
163+
foreach (KeyValuePair<TKey, int> largerOrEqualToIndex in Indecies.Where(kvp => kvp.Value >= index))
164164
{
165-
Indecies[k]++;
165+
Indecies[largerOrEqualToIndex.Key] = largerOrEqualToIndex.Value + 1;
166166
}
167167
}
168168

@@ -185,9 +185,9 @@ protected override void RemoveItem(int index)
185185

186186
Indecies.Remove(key);
187187

188-
foreach (TKey k in Indecies.Keys.Where(k => Indecies[k] > index).ToList())
188+
foreach (KeyValuePair<TKey, int> largerThanIndex in Indecies.Where(kvp => kvp.Value > index))
189189
{
190-
Indecies[k]--;
190+
Indecies[largerThanIndex.Key] = largerThanIndex.Value - 1;
191191
}
192192
}
193193

src/Umbraco.Core/Models/Content.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public IEnumerable<string>? EditedCultures
197197

198198
/// <inheritdoc />
199199
[IgnoreDataMember]
200-
public IEnumerable<string> PublishedCultures => _publishInfos?.Keys ?? Enumerable.Empty<string>();
200+
public IEnumerable<string> PublishedCultures => _publishInfos?.Keys ?? [];
201201

202202
/// <inheritdoc />
203203
public bool IsCulturePublished(string culture)

src/Umbraco.Core/Models/Navigation/NavigationNode.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public void AddChild(ConcurrentDictionary<Guid, NavigationNode> navigationStruct
3939
child.SortOrder = _children.Count;
4040

4141
_children.Add(childKey);
42-
43-
// Update the navigation structure
44-
navigationStructure[childKey] = child;
4542
}
4643

4744
public void RemoveChild(ConcurrentDictionary<Guid, NavigationNode> navigationStructure, Guid childKey)
@@ -53,8 +50,5 @@ public void RemoveChild(ConcurrentDictionary<Guid, NavigationNode> navigationStr
5350

5451
_children.Remove(childKey);
5552
child.Parent = null;
56-
57-
// Update the navigation structure
58-
navigationStructure[childKey] = child;
5953
}
6054
}

src/Umbraco.Core/Services/LocalizedTextService.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,7 @@ private static IDictionary<string, IDictionary<string, string>> GetAreaStoredTra
361361
result.TryAdd(dictionaryKey, key.Value);
362362
}
363363

364-
if (!overallResult.ContainsKey(areaAlias))
365-
{
366-
overallResult.Add(areaAlias, result);
367-
}
364+
overallResult.TryAdd(areaAlias, result);
368365
}
369366

370367
// Merge English Dictionary

src/Umbraco.Infrastructure/Persistence/Repositories/Implement/MemberTypeRepository.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,11 @@ private void EnsureExplicitDataTypeForBuiltInProperties(IContentTypeBase memberT
228228
ConventionsHelper.GetStandardPropertyTypeStubs(_shortStringHelper);
229229
foreach (IPropertyType propertyType in memberType.PropertyTypes)
230230
{
231-
if (builtinProperties.ContainsKey(propertyType.Alias))
231+
// this reset's its current data type reference which will be re-assigned based on the property editor assigned on the next line
232+
if (builtinProperties.TryGetValue(propertyType.Alias, out PropertyType? propDefinition))
232233
{
233-
// this reset's its current data type reference which will be re-assigned based on the property editor assigned on the next line
234-
if (builtinProperties.TryGetValue(propertyType.Alias, out PropertyType? propDefinition))
235-
{
236-
propertyType.DataTypeId = propDefinition.DataTypeId;
237-
propertyType.DataTypeKey = propDefinition.DataTypeKey;
238-
}
239-
else
240-
{
241-
propertyType.DataTypeId = 0;
242-
propertyType.DataTypeKey = default;
243-
}
234+
propertyType.DataTypeId = propDefinition.DataTypeId;
235+
propertyType.DataTypeKey = propDefinition.DataTypeKey;
244236
}
245237
}
246238
}

src/Umbraco.Infrastructure/Persistence/Repositories/Implement/PublishStatusRepository.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using NPoco;
2-
using Umbraco.Cms.Core.DeliveryApi;
3-
using Umbraco.Cms.Core.Media.EmbedProviders;
42
using Umbraco.Cms.Core.Models;
53
using Umbraco.Cms.Core.Persistence.Repositories;
64
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
@@ -67,7 +65,7 @@ public async Task<ISet<string>> GetPublishStatusAsync(Guid documentKey, Cancella
6765
List<PublishStatusDto>? databaseRecords = await Database.FetchAsync<PublishStatusDto>(sql);
6866

6967
IDictionary<Guid, ISet<string>> result = Map(databaseRecords);
70-
return result.ContainsKey(documentKey) ? result[documentKey] : new HashSet<string>();
68+
return result.TryGetValue(documentKey, out ISet<string>? value) ? value : new HashSet<string>();
7169
}
7270

7371
public async Task<IDictionary<Guid, ISet<string>>> GetDescendantsOrSelfPublishStatusAsync(Guid rootDocumentKey, CancellationToken cancellationToken)
@@ -98,7 +96,7 @@ private IDictionary<Guid, ISet<string>> Map(List<PublishStatusDto> databaseRecor
9896
x=> (ISet<string>) x.Where(x=> IsPublished(x)).Select(y=>y.IsoCode).ToHashSet());
9997
}
10098

101-
private bool IsPublished(PublishStatusDto publishStatusDto)
99+
private static bool IsPublished(PublishStatusDto publishStatusDto)
102100
{
103101
switch ((ContentVariation)publishStatusDto.ContentTypeVariation)
104102
{
@@ -112,7 +110,7 @@ private bool IsPublished(PublishStatusDto publishStatusDto)
112110
}
113111
}
114112

115-
private class PublishStatusDto
113+
private sealed class PublishStatusDto
116114
{
117115

118116
public const string DocumentVariantPublishStatusColumnName = "variantPublished";
@@ -133,5 +131,4 @@ private class PublishStatusDto
133131
[Column(DocumentVariantPublishStatusColumnName)]
134132
public bool DocumentVariantPublishStatus { get; set; }
135133
}
136-
137134
}

src/Umbraco.Web.Common/Helpers/OAuthOptionsHelper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Authentication;
22
using Microsoft.AspNetCore.Authentication.OAuth;
33
using Microsoft.Extensions.Options;
4+
using Microsoft.Extensions.Primitives;
45
using Umbraco.Cms.Core.Configuration.Models;
56
using Umbraco.Extensions;
67

@@ -14,7 +15,7 @@ public class OAuthOptionsHelper
1415
{
1516
// https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2.1
1617
// we omit "state" and "error_uri" here as it hold no value in determining the message to display to the user
17-
private static readonly IReadOnlyCollection<string> _oathCallbackErrorParams = new string[] { "error", "error_description" };
18+
private static readonly string[] _oathCallbackErrorParams = ["error", "error_description"];
1819

1920
private readonly IOptions<SecuritySettings> _securitySettings;
2021

@@ -43,7 +44,7 @@ private Task HandleResponseWithDefaultUmbracoRedirect(HandleRequestContext<Remot
4344
SetUmbracoRedirectWithFilteredParams(context, providerFriendlyName, eventName)
4445
.HandleResponse();
4546

46-
return Task.FromResult(0);
47+
return Task.CompletedTask;
4748
}
4849

4950
/// <summary>
@@ -60,9 +61,9 @@ public T SetUmbracoRedirectWithFilteredParams<T>(T context, string providerFrien
6061

6162
foreach (var oathCallbackErrorParam in _oathCallbackErrorParams)
6263
{
63-
if (context.Request.Query.ContainsKey(oathCallbackErrorParam))
64+
if (context.Request.Query.TryGetValue(oathCallbackErrorParam, out StringValues paramValue))
6465
{
65-
callbackPath = callbackPath.AppendQueryStringToUrl($"{oathCallbackErrorParam}={context.Request.Query[oathCallbackErrorParam]}");
66+
callbackPath = callbackPath.AppendQueryStringToUrl($"{oathCallbackErrorParam}={paramValue}");
6667
}
6768
}
6869

0 commit comments

Comments
 (0)