Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Regex to Reduce Matching Time #4160

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ internal sealed partial class TerminalTestReporter : IDisposable

private bool? _shouldShowPassedTests;

#if NET7_0_OR_GREATER
[GeneratedRegex(@$"^ at ((?<code>.+) in (?<file>.+):line (?<line>\d+)|(?<code1>.+))$", RegexOptions.ExplicitCapture, 1000)]
private static partial Regex GetFrameRegex();
#else
private static Regex? s_regex;

#if NET7_0_OR_GREATER

[GeneratedRegex(@"^ at (?<code>.+\))( in (?<file>.+):line (?<line>\d+))?$", RegexOptions.ExplicitCapture, 1000)]
internal static partial Regex GetStandardFrameRegex();

[GeneratedRegex(@"^ at (?<code>.+\))", RegexOptions.ExplicitCapture, 1000)]
internal static partial Regex GetAOTFrameRegex();

internal static Regex GetFrameRegex() => s_regex ??=
System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported
? GetStandardFrameRegex()
: GetAOTFrameRegex();
#else
[MemberNotNull(nameof(s_regex))]
private static Regex GetFrameRegex()
internal static Regex GetFrameRegex()
{
if (s_regex != null)
{
Expand All @@ -75,7 +84,9 @@ private static Regex GetFrameRegex()
{
// Get these resources: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
#pragma warning disable RS0030 // Do not use banned APIs
MethodInfo? getResourceStringMethod = typeof(Environment).GetMethod("GetResourceString", BindingFlags.Static | BindingFlags.NonPublic, null, [typeof(string)], null);
MethodInfo? getResourceStringMethod = typeof(Environment).GetMethod(
"GetResourceString",
BindingFlags.Static | BindingFlags.NonPublic, null, [typeof(string)], null);
#pragma warning restore RS0030 // Do not use banned APIs
if (getResourceStringMethod is not null)
{
Expand All @@ -96,7 +107,7 @@ private static Regex GetFrameRegex()

string inPattern = string.Format(CultureInfo.InvariantCulture, inString, "(?<file>.+)", @"(?<line>\d+)");

s_regex = new Regex(@$"^ {atString} ((?<code>.+) {inPattern}|(?<code1>.+))$", RegexOptions.Compiled | RegexOptions.ExplicitCapture, matchTimeout: TimeSpan.FromSeconds(1));
s_regex = new Regex($@"^ {atString} (?<code>.+\))( {inPattern})?$", RegexOptions.Compiled | RegexOptions.ExplicitCapture, matchTimeout: TimeSpan.FromSeconds(1));
return s_regex;
}
#endif
Expand Down Expand Up @@ -653,13 +664,14 @@ private static void AppendAssemblyLinkTargetFrameworkAndArchitecture(ITerminal t
bool weHaveFilePathAndCodeLine = !RoslynString.IsNullOrWhiteSpace(match.Groups["code"].Value);
terminal.Append(PlatformResources.StackFrameAt);
terminal.Append(' ');

if (weHaveFilePathAndCodeLine)
{
terminal.Append(match.Groups["code"].Value);
}
else
{
terminal.Append(match.Groups["code1"].Value);
terminal.Append(match.Groups["line"].Value);
}

if (weHaveFilePathAndCodeLine)
Expand Down
Loading