Skip to content

Commit fbbbc45

Browse files
authored
Micro-optimization: Move compilation of some Regex generation to compile time instead of runtime, make some static ones compiled (#20287)
Move compilation of some Regex generation to compile time instead of runtime, make some static ones compiled
1 parent fc60b5b commit fbbbc45

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

src/Umbraco.Core/IO/ShadowFileSystem.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Umbraco.Cms.Core.IO;
44

5-
internal sealed class ShadowFileSystem : IFileSystem
5+
internal sealed partial class ShadowFileSystem : IFileSystem
66
{
77
private readonly IFileSystem _sfs;
88

@@ -392,14 +392,28 @@ private void Delete(string path, bool recurse)
392392
}
393393

394394
// copied from System.Web.Util.Wildcard internal
395-
internal sealed class WildcardExpression
395+
internal sealed partial class WildcardExpression
396396
{
397-
private static readonly Regex MetaRegex = new("[\\+\\{\\\\\\[\\|\\(\\)\\.\\^\\$]");
398-
private static readonly Regex QuestRegex = new("\\?");
399-
private static readonly Regex StarRegex = new("\\*");
400-
private static readonly Regex CommaRegex = new(",");
401-
private static readonly Regex SlashRegex = new("(?=/)");
402-
private static readonly Regex BackslashRegex = new("(?=[\\\\:])");
397+
private static readonly Regex MetaRegex = GetMetaRegex();
398+
399+
[GeneratedRegex("[\\+\\{\\\\\\[\\|\\(\\)\\.\\^\\$]")]
400+
private static partial Regex GetMetaRegex();
401+
402+
private static readonly Regex QuestRegex = GetQuestRegex();
403+
404+
[GeneratedRegex("\\?")]
405+
private static partial Regex GetQuestRegex();
406+
407+
private static readonly Regex StarRegex = GetStarRegex();
408+
409+
[GeneratedRegex("\\*")]
410+
private static partial Regex GetStarRegex();
411+
412+
private static readonly Regex CommaRegex = GetCommaRegex();
413+
414+
[GeneratedRegex(",")]
415+
private static partial Regex GetCommaRegex();
416+
403417
private readonly bool _caseInsensitive;
404418
private readonly string _pattern;
405419
private Regex? _regex;

src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public sealed class HtmlLocalLinkParser
1515
// <a type="document" href="/{localLink:eed5fc6b-96fd-45a5-a0f1-b1adfb483c2f}" title="other page">other page</a>
1616
internal static readonly Regex LocalLinkTagPattern = new(
1717
@"<a.+?href=['""](?<locallink>\/?{localLink:(?<guid>[a-fA-F0-9-]+)})[^>]*?>",
18-
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline);
18+
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.Compiled);
1919

2020
internal static readonly Regex TypePattern = new(
2121
"""type=['"](?<type>(?:media|document))['"]""",
22-
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
22+
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
2323

2424
internal static readonly Regex LocalLinkPattern = new(
2525
@"href=['""](?<locallink>\/?(?:\{|\%7B)localLink:(?<guid>[a-zA-Z0-9-://]+)(?:\}|\%7D))",
26-
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
26+
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);
2727

2828
private readonly IPublishedUrlProvider _publishedUrlProvider;
2929

src/Umbraco.Infrastructure/Persistence/NPocoDatabaseExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,18 @@ public static RecordPersistenceType InsertOrUpdate<T>(
225225
/// <returns></returns>
226226
public static string EscapeAtSymbols(string value)
227227
{
228-
if (value.Contains("@") == false)
228+
if (value.Contains('@') == false)
229229
{
230230
return value;
231231
}
232232

233233
// this fancy regex will only match a single @ not a double, etc...
234-
var regex = new Regex("(?<!@)@(?!@)");
235-
return regex.Replace(value, "@@");
234+
return AtRegex().Replace(value, "@@");
236235
}
237236

237+
[GeneratedRegex("(?<!@)@(?!@)")]
238+
private static partial Regex AtRegex();
239+
238240
/// <summary>
239241
/// Returns the underlying connection as a typed connection - this is used to unwrap the profiled mini profiler stuff
240242
/// </summary>

0 commit comments

Comments
 (0)