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

Factor out and improve the vectorization of RegexInterpreter.FindFirstChar #61490

Merged
merged 6 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
RegexCode code;
try
{
code = RegexWriter.Write(RegexParser.Parse(pattern, regexOptions, culture));
code = RegexWriter.Write(RegexParser.Parse(pattern, regexOptions, culture), culture);
}
catch (Exception e)
{
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Text.RegularExpressions/gen/Stubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ namespace System.Threading
{
internal static class InterlockedExtensions
{
public static int Or(ref int location1, int value)
public static uint Or(ref uint location1, uint value)
{
int current = location1;
uint current = location1;
while (true)
{
int newValue = current | value;
int oldValue = Interlocked.CompareExchange(ref location1, newValue, current);
uint newValue = current | value;
uint oldValue = (uint)Interlocked.CompareExchange(ref Unsafe.As<uint, int>(ref location1), (int)newValue, (int)current);
if (oldValue == current)
{
return oldValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<Compile Include="..\src\System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
<Compile Include="..\src\System\Threading\StackHelper.cs" Link="Production\StackHelper.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexBoyerMoore.cs" Link="Production\RegexBoyerMoore.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexCharClass.cs" Link="Production\RegexCharClass.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexCharClass.MappingTable.cs" Link="Production\RegexCharClass.MappingTable.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexCode.cs" Link="Production\RegexCode.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexFindOptimizations.cs" Link="Production\RegexFindOptimizations.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexNode.cs" Link="Production\RegexNode.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexOptions.cs" Link="Production\RegexOptions.cs" />
<Compile Include="..\src\System\Text\RegularExpressions\RegexParseError.cs" Link="Production\RegexParseError.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
<Compile Include="System\Text\RegularExpressions\Regex.Replace.cs" />
<Compile Include="System\Text\RegularExpressions\Regex.Split.cs" />
<Compile Include="System\Text\RegularExpressions\Regex.Timeout.cs" />
<Compile Include="System\Text\RegularExpressions\RegexBoyerMoore.cs" />
<Compile Include="System\Text\RegularExpressions\RegexCharClass.cs" />
<Compile Include="System\Text\RegularExpressions\RegexCharClass.MappingTable.cs" />
<Compile Include="System\Text\RegularExpressions\RegexCode.cs" />
<Compile Include="System\Text\RegularExpressions\RegexCompilationInfo.cs" />
<Compile Include="System\Text\RegularExpressions\RegexFindOptimizations.cs" />
<Compile Include="System\Text\RegularExpressions\RegexGeneratorAttribute.cs" />
<Compile Include="System\Text\RegularExpressions\RegexInterpreter.cs" />
<Compile Include="System\Text\RegularExpressions\RegexMatchTimeoutException.cs" />
Expand Down Expand Up @@ -100,6 +100,7 @@
<Reference Include="System.Memory" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading" />
<!-- References required for RegexOptions.Compiled -->
<Reference Include="System.Reflection.Emit" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static Regex GetOrAdd(string pattern, RegexOptions options, TimeSpan matc
Regex.ValidateOptions(options);
Regex.ValidateMatchTimeout(matchTimeout);

CultureInfo culture = (options & RegexOptions.CultureInvariant) != 0 ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture;
CultureInfo culture = RegexParser.GetTargetCulture(options);
Key key = new Key(pattern, culture.ToString(), options, matchTimeout);

Regex? regex = Get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ internal Regex(string pattern, CultureInfo? culture)
// Call Init directly rather than delegating to a Regex ctor that takes
// options to enable linking / tree shaking to remove the Regex compiler
// and NonBacktracking implementation if it's not used.
Init(pattern, RegexOptions.None, s_defaultMatchTimeout, culture);
Init(pattern, RegexOptions.None, s_defaultMatchTimeout, culture ?? CultureInfo.CurrentCulture);
joperezr marked this conversation as resolved.
Show resolved Hide resolved
}

internal Regex(string pattern, RegexOptions options, TimeSpan matchTimeout, CultureInfo? culture)
{
culture ??= GetTargetCulture(options);
culture ??= RegexParser.GetTargetCulture(options);
Init(pattern, options, matchTimeout, culture);

if ((options & RegexOptions.NonBacktracking) != 0)
Expand All @@ -87,18 +87,14 @@ internal Regex(string pattern, RegexOptions options, TimeSpan matchTimeout, Cult
}
}

/// <summary>Gets the culture to use based on the specified options.</summary>
private static CultureInfo GetTargetCulture(RegexOptions options) =>
(options & RegexOptions.CultureInvariant) != 0 ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture;

/// <summary>Initializes the instance.</summary>
/// <remarks>
/// This is separated out of the constructor so that an app only using 'new Regex(pattern)'
/// rather than 'new Regex(pattern, options)' can avoid statically referencing the Regex
/// compiler, such that a tree shaker / linker can trim it away if it's not otherwise used.
/// </remarks>
[MemberNotNull(nameof(_code))]
private void Init(string pattern, RegexOptions options, TimeSpan matchTimeout, CultureInfo? culture)
private void Init(string pattern, RegexOptions options, TimeSpan matchTimeout, CultureInfo culture)
{
ValidatePattern(pattern);
ValidateOptions(options);
Expand All @@ -107,7 +103,6 @@ private void Init(string pattern, RegexOptions options, TimeSpan matchTimeout, C
this.pattern = pattern;
internalMatchTimeout = matchTimeout;
roptions = options;
culture ??= GetTargetCulture(options);

#if DEBUG
if (IsDebug)
Expand All @@ -121,7 +116,7 @@ private void Init(string pattern, RegexOptions options, TimeSpan matchTimeout, C

// Generate the RegexCode from the node tree. This is required for interpreting,
// and is used as input into RegexOptions.Compiled and RegexOptions.NonBacktracking.
_code = RegexWriter.Write(tree);
_code = RegexWriter.Write(tree, culture);

if ((options & RegexOptions.NonBacktracking) != 0)
{
Expand Down Expand Up @@ -434,7 +429,7 @@ internal void Run<TState>(string input, int startat, ref TState state, MatchCall
/// <summary>Creates a new runner instance.</summary>
private RegexRunner CreateRunner() =>
factory?.CreateInstance() ??
new RegexInterpreter(_code!, GetTargetCulture(roptions));
new RegexInterpreter(_code!, RegexParser.GetTargetCulture(roptions));

/// <summary>True if the <see cref="RegexOptions.Compiled"/> option was set.</summary>
protected bool UseOptionC() => (roptions & RegexOptions.Compiled) != 0;
Expand Down
Loading