Skip to content

Consider removing goto statements on Regex source-generated code #62657

Closed

Description

@joperezr

The generated source from RegexGenerator today heavily uses goto statements which in many cases (like backtracking for example) is very useful but in practice the statement is rarely used due to its tendency to make things harder to read/follow due to its code jumping nature. One of the purposes of the source generator is to make code readable and to look as close as possible to code that any average developer would write so that it is also easier to debug, and so we should probably consider avoid it in a few places if we can.

For example, consider a very simple pattern a{1,2} which will try to match an a one or two times greedily. The generated Go method currently looks something like:

protected override void Go()
{
    // .. some variable initialization

    int iteration = 0;
    while (iteration < 2 && (uint)iteration < (uint)slice.Length && slice[iteration] == 'a')
    {
        iteration++;
    }

    if (iteration == 0)
    {
        goto NoMatch;
    }

    slice = slice.Slice(iteration);
    pos += iteration;

    // The input matched.
    base.runtextpos = pos;
    base.Capture(0, original_pos, pos);
    return;

    // The input didn't match.
    NoMatch:;
}

We could instead produce code that would feel more natural for a C# developer like a function call instead, so that it looks more like:

protected override void Go()
{
    // .. some variable initialization

    int iteration = 0;
    while (iteration < 2 && (uint)iteration < (uint)slice.Length && slice[iteration] == 'a')
    {
        iteration++;
    }

    if (iteration == 0)
    {
        return NoMatch();
    }

    slice = slice.Slice(iteration);
    pos += iteration;

    // The input matched.
    base.runtextpos = pos;
    base.Capture(0, original_pos, pos);
    return;

    void NoMatch()
    {
        //The input didn't match.
    }
}

We should probably consider this not only for NoMatch case, but instead also in our other backtracking options, but if we do decide to remove some uses of goto it could very well be done in stages.

cc: @stephentoub

Activity

  1. added this to the 7.0.0 milestone on Dec 10, 2021
  2. ghost added
    untriaged
    on Dec 10, 2021
  3. added
    enhancement
    and removed
    untriaged
    on Dec 10, 2021
  4. stephentoub commented on Dec 10, 2021

    @stephentoub
    Member

    For the top-level "goto NoMatch", yeah, we can just change that to "return;", if there's nothing to uncapture (no goto or local function call needed). If there is something to uncapture, we could do what you suggest and put the uncapturing inside of a local NoMatch function, though I'm not sure how much that one goto removal would actually help with readability... you need to visually jump to NoMatch to see what it's doing, at which point it's not clear to me putting it into a function actually helps understandability (the uncapturing is one function call itself).

    Beyond the single top-level NoMatch label, I expect most of the other goto usage will be really hard to remove, but please do share suggestions.

  5. stephentoub commented on Dec 11, 2021

    @stephentoub
    Member

    Thinking about your suggestion more, for the top-level NoMatch, it probably makes sense to just emit "return;" or "UncaptureUntil(0); return;" (if there are captures) rather than "goto NoMatch;" as we do today.

  6. joperezr commented on Dec 11, 2021

    @joperezr
    MemberAuthor

    Sounds good I'll take care of that one, and think if any other make sense too.

  7. stephentoub commented on Dec 13, 2021

    @stephentoub
    Member

    You might want to hold off on this one. If we end up implementing Scan by combining the code from FindFirstChar / Go into a single method, essentially FFC's loop wrapped around the contents of Go, we'll likely have an easier time doing so when that body jumps to the end of the FFC loop body, as would happen with the goto code today.

  8. self-assigned this
    on Feb 18, 2022
  9. ghost added
    in-pr
    on Feb 18, 2022
  10. ghost removed
    in-pr
    on Feb 27, 2022
  11. ghost locked as resolved and limited conversation to collaborators on Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Text.RegularExpressionsenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions