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

Remove some unnecessary slicing from generated Regex code #61701

Merged
merged 2 commits into from
Nov 17, 2021

Conversation

stephentoub
Copy link
Member

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.

cc: @joperezr, @danmoseley

Matching "defg" as part of a pattern before
// Multi "defg"
{
    byteSpan = global::System.Runtime.InteropServices.MemoryMarshal.AsBytes(textSpan);
    if ((uint)textSpan.Length < 4 ||
        global::System.Buffers.Binary.BinaryPrimitives.ReadUInt64LittleEndian(byteSpan.Slice(0)) != 0x67006600650064ul)
    {
        goto NoMatch;
    }
}
Matching "defg" as part of a pattern after
// Multi "defg"
{
    byteSpan = global::System.Runtime.InteropServices.MemoryMarshal.AsBytes(textSpan);
    if ((uint)textSpan.Length < 4 ||
        global::System.Buffers.Binary.BinaryPrimitives.ReadUInt64LittleEndian(byteSpan) != 0x67006600650064ul)
    {
        goto NoMatch;
    }
}

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.
@ghost
Copy link

ghost commented Nov 16, 2021

Tagging subscribers to this area: @eerhardt, @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Issue Details

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.

cc: @joperezr, @danmoseley

Matching "defg" as part of a pattern before
// Multi "defg"
{
    byteSpan = global::System.Runtime.InteropServices.MemoryMarshal.AsBytes(textSpan);
    if ((uint)textSpan.Length < 4 ||
        global::System.Buffers.Binary.BinaryPrimitives.ReadUInt64LittleEndian(byteSpan.Slice(0)) != 0x67006600650064ul)
    {
        goto NoMatch;
    }
}
Matching "defg" as part of a pattern after
// Multi "defg"
{
    byteSpan = global::System.Runtime.InteropServices.MemoryMarshal.AsBytes(textSpan);
    if ((uint)textSpan.Length < 4 ||
        global::System.Buffers.Binary.BinaryPrimitives.ReadUInt64LittleEndian(byteSpan) != 0x67006600650064ul)
    {
        goto NoMatch;
    }
}
Author: stephentoub
Assignees: -
Labels:

area-System.Text.RegularExpressions

Milestone: 7.0.0

Copy link
Member

@joperezr joperezr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the one comment this looks good. Is it worth adding a unit test that ensures the Slice is not there in this case? (if the tiny optimization doesn't warrant a test that's ok too)

@stephentoub
Copy link
Member Author

Other than the one comment this looks good. Is it worth adding a unit test that ensures the Slice is not there in this case?

This would likely manifest as a test which generated code and compared the generated output to a known string. We currently don't have any such tests. We could certainly add some (separately), though they would likely be a non-trivial time sink until the code has stabilized further, as many changes would likely require regenerating all such pre-generated snippets.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants