Skip to content

Commit 83661ff

Browse files
authored
Remove some unnecessary slicing from generated Regex code (#61701)
* Remove some unnecessary slicing from generated Regex code When we're outputting code to match a "multi" (a sequence of multiple characters), we're currently issuing a Slice for the known tracked offset even if that offset is 0. We can skip that nop. * Address PR feedback
1 parent 44d28bf commit 83661ff

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,15 +1529,17 @@ void EmitOr()
15291529
while (byteStr.Length >= sizeof(ulong))
15301530
{
15311531
EmitOr();
1532-
writer.Write($"global::System.Buffers.Binary.BinaryPrimitives.ReadUInt64LittleEndian(byteSpan.Slice({textSpanPos * sizeof(char)})) != 0x{BinaryPrimitives.ReadUInt64LittleEndian(byteStr):X}ul");
1532+
string byteSpan = textSpanPos > 0 ? $"byteSpan.Slice({textSpanPos * sizeof(char)})" : "byteSpan";
1533+
writer.Write($"global::System.Buffers.Binary.BinaryPrimitives.ReadUInt64LittleEndian({byteSpan}) != 0x{BinaryPrimitives.ReadUInt64LittleEndian(byteStr):X}ul");
15331534
textSpanPos += sizeof(ulong) / sizeof(char);
15341535
byteStr = byteStr.Slice(sizeof(ulong));
15351536
}
15361537

15371538
while (byteStr.Length >= sizeof(uint))
15381539
{
15391540
EmitOr();
1540-
writer.Write($"global::System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(byteSpan.Slice({textSpanPos * sizeof(char)})) != 0x{BinaryPrimitives.ReadUInt32LittleEndian(byteStr):X}u");
1541+
string byteSpan = textSpanPos > 0 ? $"byteSpan.Slice({textSpanPos * sizeof(char)})" : "byteSpan";
1542+
writer.Write($"global::System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian({byteSpan}) != 0x{BinaryPrimitives.ReadUInt32LittleEndian(byteStr):X}u");
15411543
textSpanPos += sizeof(uint) / sizeof(char);
15421544
byteStr = byteStr.Slice(sizeof(uint));
15431545
}
@@ -1565,7 +1567,8 @@ void EmitOr()
15651567
// character-by-character while respecting the culture.
15661568
if (!caseInsensitive)
15671569
{
1568-
using (EmitBlock(writer, $"if (!global::System.MemoryExtensions.StartsWith({textSpanLocal}.Slice({textSpanPos}), {Literal(node.Str)}))"))
1570+
string sourceSpan = textSpanPos > 0 ? $"{textSpanLocal}.Slice({textSpanPos})" : textSpanLocal;
1571+
using (EmitBlock(writer, $"if (!global::System.MemoryExtensions.StartsWith({sourceSpan}, {Literal(node.Str)}))"))
15691572
{
15701573
writer.WriteLine($"goto {doneLabel};");
15711574
}
@@ -1577,7 +1580,8 @@ void EmitOr()
15771580
string i = NextLocalName("i");
15781581
using (EmitBlock(writer, $"for (int {i} = 0; {i} < {Literal(node.Str)}.Length; {i}++)"))
15791582
{
1580-
using (EmitBlock(writer, $"if ({ToLower(hasTextInfo, options, $"{textSpanLocal}[{textSpanPos} + {i}]")} != {Literal(str)}[{i}])"))
1583+
string textSpanIndex = textSpanPos > 0 ? $"{i} + {textSpanPos}" : i;
1584+
using (EmitBlock(writer, $"if ({ToLower(hasTextInfo, options, $"{textSpanLocal}[{textSpanIndex}]")} != {Literal(str)}[{i}])"))
15811585
{
15821586
writer.WriteLine($"goto {doneLabel};");
15831587
}

0 commit comments

Comments
 (0)