Improve incremental handling in regex generator #83868
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed that the regex generator wasn't being as incremental as was intended. One issue that had previously been pointed out was that the data returned from the initial pipeline stage included a SyntaxNode, which caused comparison of the cached result against a new compilation to fail. Another issue @chsienki found was that the initial stage was doing the regex parsing and including the parsed regex tree in the result, but that doesn't define anything other than reference equality, so it was also prohibiting reuse of the cached result. On top of that, even if the caching worked, the regex parsing and analysis is relatively expensive, and it would end up being done on every recompilation regardless of whether the result could be cached.
Chris fixed the location issue by creating a new Location with the relevant file path/span from the original but without referencing the syntax tree. And he fixed the parsing by moving that logic out into a subsequent stage.
We also cleaned up the parsing routine, which no longer needed to find the GeneratedRegexAttribute on its own, as it's now handed into the method in the context supplied by ForAttributeWithMetadataName.