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

Use ReadOnlySpan<char> instead of strings on SourceGenerator engine #62222

Merged
merged 2 commits into from
Dec 2, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private static void EmitFindFirstChar(IndentedTextWriter writer, RegexMethod rm,
bool hasTextInfo = false;

// Emit locals initialization
writer.WriteLine("string runtext = base.runtext!;");
writer.WriteLine("global::System.ReadOnlySpan<char> runtextSpan = base.runtext;");
writer.WriteLine("int runtextpos = base.runtextpos;");
writer.WriteLine("int runtextend = base.runtextend;");
if (rtl)
Expand Down Expand Up @@ -387,7 +387,7 @@ bool EmitAnchors()
else
{
// TODO: This differs subtly between interpreted and compiled. Why?
using (EmitBlock(writer, "if (runtextpos < runtextend - 1 || (runtextpos == runtextend - 1 && runtext[runtextpos] != '\\n'))"))
using (EmitBlock(writer, "if (runtextpos < runtextend - 1 || (runtextpos == runtextend - 1 && runtextSpan[runtextpos] != '\\n'))"))
{
writer.WriteLine("goto ReturnFalse;");
}
Expand Down Expand Up @@ -421,14 +421,14 @@ bool EmitAnchors()
// to boost our position to the next line, and then continue normally with any searches.
Debug.Assert(!rtl, "RightToLeft isn't implemented and should have been filtered out previously");
writer.WriteLine("// Beginning-of-line anchor");
using (EmitBlock(writer, "if (runtextpos > runtextbeg && runtext[runtextpos - 1] != '\\n')"))
using (EmitBlock(writer, "if (runtextpos > runtextbeg && runtextSpan[runtextpos - 1] != '\\n')"))
{
writer.WriteLine("int newlinePos = runtext.IndexOf('\\n', runtextpos);");
using (EmitBlock(writer, "if (newlinePos == -1 || newlinePos + 1 > runtextend)"))
writer.WriteLine("int newlinePos = global::System.MemoryExtensions.IndexOf(runtextSpan.Slice(runtextpos), '\\n');");
using (EmitBlock(writer, "if (newlinePos == -1 || newlinePos + runtextpos + 1 > runtextend)"))
{
writer.WriteLine("goto ReturnFalse;");
}
writer.WriteLine("runtextpos = newlinePos + 1;");
writer.WriteLine("runtextpos = newlinePos + runtextpos + 1;");
}
writer.WriteLine();
break;
Expand All @@ -441,7 +441,7 @@ bool EmitAnchors()
// Emits a case-sensitive left-to-right prefix search for a string at the beginning of the pattern.
void EmitIndexOf_LeftToRight(string prefix)
{
writer.WriteLine($"int i = global::System.MemoryExtensions.IndexOf(global::System.MemoryExtensions.AsSpan(runtext, runtextpos, runtextend - runtextpos), {Literal(prefix)});");
writer.WriteLine($"int i = global::System.MemoryExtensions.IndexOf(runtextSpan.Slice(runtextpos, runtextend - runtextpos), {Literal(prefix)});");
writer.WriteLine("if (i >= 0)");
writer.WriteLine("{");
writer.WriteLine(" base.runtextpos = runtextpos + i;");
Expand All @@ -452,7 +452,7 @@ void EmitIndexOf_LeftToRight(string prefix)
// Emits a case-sensitive right-to-left prefix search for a string at the beginning of the pattern.
void EmitIndexOf_RightToLeft(string prefix)
{
writer.WriteLine($"int i = global::System.MemoryExtensions.LastIndexOf(global::System.MemoryExtensions.AsSpan(runtext, runtextbeg, runtextpos - runtextbeg), {Literal(prefix)});");
writer.WriteLine($"int i = global::System.MemoryExtensions.LastIndexOf(runtextSpan.Slice(runtextbeg, runtextpos - runtextbeg), {Literal(prefix)});");
writer.WriteLine("if (i >= 0)");
writer.WriteLine("{");
writer.WriteLine($" base.runtextpos = runtextbeg + i + {prefix.Length};");
Expand All @@ -469,7 +469,7 @@ void EmitFixedSet_RightToLeft()

if (set.Chars is { Length: 1 } && !set.CaseInsensitive)
{
writer.WriteLine($"int i = global::System.MemoryExtensions.LastIndexOf(global::System.MemoryExtensions.AsSpan(runtext, runtextbeg, runtextpos - runtextbeg), {Literal(set.Chars[0])});");
writer.WriteLine($"int i = global::System.MemoryExtensions.LastIndexOf(runtextSpan.Slice(runtextbeg, runtextpos - runtextbeg), {Literal(set.Chars[0])});");
writer.WriteLine("if (i >= 0)");
writer.WriteLine("{");
writer.WriteLine(" base.runtextpos = runtextbeg + i + 1;");
Expand All @@ -480,7 +480,7 @@ void EmitFixedSet_RightToLeft()
{
using (EmitBlock(writer, "for (int i = runtextpos - 1; i >= runtextbeg; i--)"))
{
using (EmitBlock(writer, $"if ({MatchCharacterClass(hasTextInfo, options, "runtext[i]", set.Set, set.CaseInsensitive)})"))
using (EmitBlock(writer, $"if ({MatchCharacterClass(hasTextInfo, options, "runtextSpan[i]", set.Set, set.CaseInsensitive)})"))
{
writer.WriteLine("base.runtextpos = i + 1;");
writer.WriteLine("return true;");
Expand All @@ -507,7 +507,7 @@ void EmitFixedSet_LeftToRight()
FinishEmitScope loopBlock = default;
if (needLoop)
{
writer.WriteLine("global::System.ReadOnlySpan<char> span = global::System.MemoryExtensions.AsSpan(runtext, runtextpos, runtextend - runtextpos);");
writer.WriteLine("global::System.ReadOnlySpan<char> span = runtextSpan.Slice(runtextpos, runtextend - runtextpos);");
string upperBound = "span.Length" + (setsToUse > 1 || primarySet.Distance != 0 ? $" - {minRequiredLength - 1}" : "");
loopBlock = EmitBlock(writer, $"for (int i = 0; i < {upperBound}; i++)");
}
Expand All @@ -516,7 +516,7 @@ void EmitFixedSet_LeftToRight()
{
string span = needLoop ?
"span" :
"global::System.MemoryExtensions.AsSpan(runtext, runtextpos, runtextend - runtextpos)";
"runtextSpan.Slice(runtextpos, runtextend - runtextpos)";

span = (needLoop, primarySet.Distance) switch
{
Expand Down