Skip to content

Commit 05b0c60

Browse files
committed
perf: use Frozen data type improve performance
1 parent 98bdb30 commit 05b0c60

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

src/BootstrapBlazor.Server/Extensions/CacheManagerExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.Extensions.Caching.Memory;
7+
using System.Collections.Frozen;
78
using System.Globalization;
89

910
namespace BootstrapBlazor.Server.Extensions;
@@ -20,7 +21,7 @@ internal static class CacheManagerExtensions
2021
/// <param name="typeName"></param>
2122
/// <param name="options"></param>
2223
/// <returns></returns>
23-
public static IEnumerable<LocalizedString> GetLocalizedStrings(this ICacheManager cache, string typeName, JsonLocalizationOptions options)
24+
public static FrozenSet<LocalizedString> GetLocalizedStrings(this ICacheManager cache, string typeName, JsonLocalizationOptions options)
2425
{
2526
var key = $"Snippet-{CultureInfo.CurrentUICulture.Name}-{nameof(GetLocalizedStrings)}-{typeName}";
2627
return cache.GetOrCreate(key, entry =>

src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
using System.Reflection;
1010
using System.Resources;
1111

12+
#if NET8_0_OR_GREATER
13+
using System.Collections.Frozen;
14+
#endif
15+
1216
namespace BootstrapBlazor.Components;
1317

1418
/// <summary>
@@ -97,14 +101,19 @@ public override LocalizedString this[string name]
97101
}
98102
}
99103

100-
private string? GetValueFromCache(IEnumerable<LocalizedString>? localizerStrings, string name)
104+
#if NET8_0_OR_GREATER
105+
private string? GetValueFromCache(FrozenSet<LocalizedString>? localizerStrings, string name)
106+
#else
107+
private string? GetValueFromCache(HashSet<LocalizedString>? localizerStrings, string name)
108+
#endif
101109
{
102110
string? ret = null;
103111
var cultureName = CultureInfo.CurrentUICulture.Name;
104112
var cacheKey = $"{nameof(GetValueFromCache)}&name={name}&{Assembly.GetUniqueName()}&type={typeName}&culture={cultureName}";
105113
if (!CacheManager.GetMissingLocalizerByKey(cacheKey))
106114
{
107-
var l = GetLocalizedString();
115+
var l = localizerStrings?.FirstOrDefault(i => i.Name == name)
116+
?? CacheManager.GetAllStringsFromResolve().FirstOrDefault(i => i.Name == name);
108117
if (l is { ResourceNotFound: false })
109118
{
110119
ret = l.Value;
@@ -116,16 +125,6 @@ public override LocalizedString this[string name]
116125
}
117126
}
118127
return ret;
119-
120-
LocalizedString? GetLocalizedString()
121-
{
122-
LocalizedString? localizer = null;
123-
if (localizerStrings != null)
124-
{
125-
localizer = localizerStrings.FirstOrDefault(i => i.Name == name);
126-
}
127-
return localizer ?? CacheManager.GetAllStringsFromResolve().FirstOrDefault(i => i.Name == name);
128-
}
129128
}
130129

131130
private string? GetLocalizerValueFromCache(IStringLocalizer localizer, string name)

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
using System.Linq.Expressions;
1313
using System.Reflection;
1414

15+
#if NET8_0_OR_GREATER
16+
using System.Collections.Frozen;
17+
#endif
18+
1519
namespace BootstrapBlazor.Components;
1620

1721
/// <summary>
@@ -206,8 +210,12 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
206210
/// </summary>
207211
/// <param name="assembly"></param>
208212
/// <param name="typeName"></param>
209-
/// <returns></returns>
210-
public static IEnumerable<LocalizedString>? GetAllStringsByTypeName(Assembly assembly, string typeName) => GetJsonStringByTypeName(GetJsonLocalizationOption(), assembly, typeName, CultureInfo.CurrentUICulture.Name);
213+
#if NET8_0_OR_GREATER
214+
public static FrozenSet<LocalizedString>? GetAllStringsByTypeName(Assembly assembly, string typeName)
215+
#else
216+
public static HashSet<LocalizedString>? GetAllStringsByTypeName(Assembly assembly, string typeName)
217+
#endif
218+
=> GetJsonStringByTypeName(GetJsonLocalizationOption(), assembly, typeName, CultureInfo.CurrentUICulture.Name);
211219

212220
/// <summary>
213221
/// 通过指定程序集获取所有本地化信息键值集合
@@ -218,7 +226,11 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
218226
/// <param name="cultureName">cultureName 未空时使用 CultureInfo.CurrentUICulture.Name</param>
219227
/// <param name="forceLoad">默认 false 使用缓存值 设置 true 时内部强制重新加载</param>
220228
/// <returns></returns>
221-
public static IEnumerable<LocalizedString>? GetJsonStringByTypeName(JsonLocalizationOptions option, Assembly assembly, string typeName, string? cultureName = null, bool forceLoad = false)
229+
#if NET8_0_OR_GREATER
230+
public static FrozenSet<LocalizedString>? GetJsonStringByTypeName(JsonLocalizationOptions option, Assembly assembly, string typeName, string? cultureName = null, bool forceLoad = false)
231+
#else
232+
public static HashSet<LocalizedString>? GetJsonStringByTypeName(JsonLocalizationOptions option, Assembly assembly, string typeName, string? cultureName = null, bool forceLoad = false)
233+
#endif
222234
{
223235
if (assembly.IsDynamic)
224236
{
@@ -236,9 +248,14 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
236248
return Instance.GetOrCreate(typeKey, entry =>
237249
{
238250
var sections = Instance.GetOrCreate(key, entry => option.GetJsonStringFromAssembly(assembly, cultureName));
239-
return sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))?
251+
var items = sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))?
240252
.GetChildren()
241253
.SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value!, false, typeName) });
254+
#if NET8_0_OR_GREATER
255+
return items?.ToFrozenSet();
256+
#else
257+
return items?.ToHashSet();
258+
#endif
242259
});
243260
}
244261

@@ -247,7 +264,11 @@ private static JsonLocalizationOptions GetJsonLocalizationOption()
247264
/// </summary>
248265
/// <param name="includeParentCultures"></param>
249266
/// <returns></returns>
250-
public static IEnumerable<LocalizedString> GetAllStringsFromResolve(bool includeParentCultures = true) => Instance.GetOrCreate($"{nameof(GetAllStringsFromResolve)}-{CultureInfo.CurrentUICulture.Name}", entry => Instance.Provider.GetRequiredService<ILocalizationResolve>().GetAllStringsByCulture(includeParentCultures));
267+
#if NET8_0_OR_GREATER
268+
public static FrozenSet<LocalizedString> GetAllStringsFromResolve(bool includeParentCultures = true) => Instance.GetOrCreate($"{nameof(GetAllStringsFromResolve)}-{CultureInfo.CurrentUICulture.Name}", entry => Instance.Provider.GetRequiredService<ILocalizationResolve>().GetAllStringsByCulture(includeParentCultures).ToFrozenSet());
269+
#else
270+
public static HashSet<LocalizedString> GetAllStringsFromResolve(bool includeParentCultures = true) => Instance.GetOrCreate($"{nameof(GetAllStringsFromResolve)}-{CultureInfo.CurrentUICulture.Name}", entry => Instance.Provider.GetRequiredService<ILocalizationResolve>().GetAllStringsByCulture(includeParentCultures).ToHashSet());
271+
#endif
251272

252273
/// <summary>
253274
/// 查询缺失本地化资源项目

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
using System.Linq.Expressions;
1111
using System.Reflection;
1212

13+
#if NET8_0_OR_GREATER
14+
using System.Collections.Frozen;
15+
#endif
16+
1317
namespace BootstrapBlazor.Components;
1418

1519
/// <summary>
@@ -156,8 +160,11 @@ public static class Utility
156160
/// <param name="typeName">类名称</param>
157161
/// <param name="cultureName">cultureName 未空时使用 CultureInfo.CurrentUICulture.Name</param>
158162
/// <param name="forceLoad">默认 false 使用缓存值 设置 true 时内部强制重新加载</param>
159-
/// <returns></returns>
160-
public static IEnumerable<LocalizedString> GetJsonStringByTypeName(JsonLocalizationOptions option, Assembly assembly, string typeName, string? cultureName = null, bool forceLoad = false) => CacheManager.GetJsonStringByTypeName(option, assembly, typeName, cultureName, forceLoad) ?? [];
163+
#if NET8_0_OR_GREATER
164+
public static FrozenSet<LocalizedString> GetJsonStringByTypeName(JsonLocalizationOptions option, Assembly assembly, string typeName, string? cultureName = null, bool forceLoad = false) => CacheManager.GetJsonStringByTypeName(option, assembly, typeName, cultureName, forceLoad) ?? FrozenSet<LocalizedString>.Empty;
165+
#else
166+
public static HashSet<LocalizedString> GetJsonStringByTypeName(JsonLocalizationOptions option, Assembly assembly, string typeName, string? cultureName = null, bool forceLoad = false) => CacheManager.GetJsonStringByTypeName(option, assembly, typeName, cultureName, forceLoad) ?? new HashSet<LocalizedString>();
167+
#endif
161168

162169
/// <summary>
163170
/// 通过指定程序集与类型获得 IStringLocalizer 实例
@@ -367,7 +374,7 @@ public static IEnumerable<ITableColumn> GetTableColumns(Type type, IEnumerable<I
367374
return defaultOrderCallback?.Invoke(cols) ?? cols;
368375
}
369376

370-
internal static IEnumerable<ITableColumn> OrderFunc(this IEnumerable<ITableColumn> cols) => cols
377+
internal static IEnumerable<ITableColumn> OrderFunc(this List<ITableColumn> cols) => cols
371378
.Where(a => a.Order > 0).OrderBy(a => a.Order)
372379
.Concat(cols.Where(a => a.Order == 0))
373380
.Concat(cols.Where(a => a.Order < 0).OrderBy(a => a.Order));
@@ -404,7 +411,6 @@ public static void CreateDisplayByFieldType(this RenderTreeBuilder builder, IEdi
404411
builder.AddAttribute(50, "class", col.CssClass);
405412
}
406413
builder.AddMultipleAttributes(60, item.ComponentParameters);
407-
builder.CloseComponent();
408414
}
409415
else if (item.ComponentType == typeof(Textarea) || item.Rows > 0)
410416
{
@@ -422,7 +428,6 @@ public static void CreateDisplayByFieldType(this RenderTreeBuilder builder, IEdi
422428
builder.AddAttribute(60, "class", col.CssClass);
423429
}
424430
builder.AddMultipleAttributes(70, item.ComponentParameters);
425-
builder.CloseComponent();
426431
}
427432
else
428433
{
@@ -448,8 +453,9 @@ public static void CreateDisplayByFieldType(this RenderTreeBuilder builder, IEdi
448453
builder.AddAttribute(90, "class", col.CssClass);
449454
}
450455
builder.AddMultipleAttributes(100, item.ComponentParameters);
451-
builder.CloseComponent();
452456
}
457+
458+
builder.CloseComponent();
453459
}
454460

455461
/// <summary>

0 commit comments

Comments
 (0)