Skip to content

Commit 1e0338b

Browse files
ArgoZhangice6
andauthored
doc(WebSiteOptions): improve performance for search code (#5075)
* perf: 使用性能更高的字典 * refactor: 增加小写键值转化 * refactor: 增加键值匹配规则 --------- Co-Authored-By: ice6 <[email protected]>
1 parent 6113b85 commit 1e0338b

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/BootstrapBlazor.Server/Components/Components/ThemeChooser.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private void OnClickTheme(SelectedItem item)
4242
{
4343
_currentTheme.Clear();
4444
WebsiteOption.CurrentValue.CurrentTheme = item.Value;
45-
var theme = WebsiteOption.CurrentValue.Themes.Find(i => i.Key == item.Value);
45+
var theme = WebsiteOption.CurrentValue.Themes.FirstOrDefault(i => i.Key == item.Value);
4646
if (theme is { Files: not null })
4747
{
4848
_currentTheme.AddRange(theme.Files);

src/BootstrapBlazor.Server/Data/WebsiteOptions.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6+
using System.Collections.Frozen;
7+
68
namespace BootstrapBlazor.Server.Data;
79

810
/// <summary>
9-
///
11+
/// WebsiteOptions 网站配置类
1012
/// </summary>
1113
public class WebsiteOptions
1214
{
@@ -102,12 +104,12 @@ public class WebsiteOptions
102104
/// <summary>
103105
/// 获得/设置 资源配置集合
104106
/// </summary>
105-
public Dictionary<string, string?> SourceCodes { get; private set; }
107+
public FrozenDictionary<string, string?> SourceCodes { get; private set; }
106108

107109
/// <summary>
108110
/// 获得/设置 资源配置集合
109111
/// </summary>
110-
public Dictionary<string, string?> Videos { get; private set; }
112+
public FrozenDictionary<string, string?> Videos { get; private set; }
111113

112114
/// <summary>
113115
/// 获得/设置 当前主题
@@ -127,23 +129,23 @@ public class WebsiteOptions
127129
/// <summary>
128130
/// 获得/设置 当前网站友联集合
129131
/// </summary>
130-
public Dictionary<string, string?> Links { get; set; }
132+
public FrozenDictionary<string, string?> Links { get; set; }
131133

132134
/// <summary>
133135
/// 获得/设置 网站主题配置集合
134136
/// </summary>
135137
[NotNull]
136-
public List<ThemeOption>? Themes { get; set; }
138+
public HashSet<ThemeOption>? Themes { get; set; }
137139

138140
/// <summary>
139141
/// 构造函数
140142
/// </summary>
141143
public WebsiteOptions()
142144
{
143145
var config = GetConfiguration("docs.json");
144-
SourceCodes = config.GetSection("src").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToDictionary(item => item.Key, item => item.Value);
145-
Videos = config.GetSection("video").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToDictionary(item => item.Key, item => item.Value);
146-
Links = config.GetSection("link").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToDictionary(item => item.Key, item => item.Value);
146+
SourceCodes = config.GetSection("src").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToFrozenDictionary(item => item.Key, item => item.Value);
147+
Videos = config.GetSection("video").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToFrozenDictionary(item => item.Key, item => item.Value);
148+
Links = config.GetSection("link").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToFrozenDictionary(item => item.Key, item => item.Value);
147149

148150
#if DEBUG
149151
IsDevelopment = true;

src/BootstrapBlazor.Server/Services/CodeSnippetService.cs

Lines changed: 3 additions & 2 deletions
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.Options;
7+
using System.Collections.Frozen;
78

89
namespace BootstrapBlazor.Server.Services;
910

@@ -15,7 +16,7 @@ class CodeSnippetService
1516

1617
private string SourceCodePath { get; set; }
1718

18-
private Dictionary<string, string?> SourceCodes { get; set; }
19+
private FrozenDictionary<string, string?> SourceCodes { get; set; }
1920

2021
private bool IsDevelopment { get; }
2122

@@ -62,7 +63,7 @@ public async Task<string> GetCodeAsync(string codeFile)
6263
// codeFile = ajax.razor.cs
6364
var segs = codeFile.Split('.');
6465
var key = segs[0];
65-
var typeName = SourceCodes.TryGetValue(key, out var value) ? value : string.Empty;
66+
var typeName = SourceCodes.TryGetValue(key.ToLowerInvariant(), out var value) ? value : string.Empty;
6667
if (!string.IsNullOrEmpty(typeName))
6768
{
6869
var fileName = codeFile.Replace(key, typeName);

0 commit comments

Comments
 (0)