Skip to content

Determine if/how to handle default timeouts in RegexGenerator #59491

@stephentoub

Description

@stephentoub

With the regex source generator merged in #59186, a developer can specify timeouts via the attribute, e.g.

[RegexGenerator("abc", RegexOptions.None, 10_000)] // 10 seconds
private static partial Regex Abc();

If no timeout is specified, no timeout is applied, and the generator avoids spitting out the code necessary to check for timeouts, making it leaner and faster.

This is almost the same thing that RegexOptions.Compiled does. If there's no timeout, RegexOptions.Compiled will similarly avoid generating the code for the checks. The difference, however, is that whether there's a timeout is influenced not just by whether one is passed to the Regex ctor, but if one isn't passed to the Regex ctor, then the REGEX_DEFAULT_MATCH_TIMEOUT AppContext value is consulted and used.

So, we're currently not paying attention to REGEX_DEFAULT_MATCH_TIMEOUT in RegexGenerator. Options:

  1. That's fine. If you want a timeout, specify a timeout. If you don't, don't.
  2. Have the generator respect the REGEX_DEFAULT_MATCH_TIMEOUT at the time of build and from the build process. That's... weird. A developer would need to know what process is actually running the generator and figure out how to configure it.
  3. If no timeout is supplied, change the generator to generate the timeout code as well as code to query REGEX_DEFAULT_MATCH_TIMEOUT on first use (e.g. in an assignment to a static readonly) and check for timeouts if it's not infinite. We might be able to structure this in a way where, with tiering, we could get the JIT to elide the timeout checks if no default timeout was supplied, though that would likely require opting-in to tiering with loops (which might become the default, anyway, as part of dynamic PGO enablement). Note that if an infinite timeout (-1) is explicitly specified, we are still free to avoid emitting timeout code, just as is the case with RegexOptions.Compiled.

cc: @GrabYourPitchforks

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions