diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs index 4a3040b657deb..57588275b0c74 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs @@ -112,7 +112,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context) }) // Combine all of the generated text outputs into a single batch. We then generate a single source output from that batch. - .Collect(); + .Collect() + + // Apply sequence equality comparison on the result array for incremental caching. + .WithComparer(new ObjectImmutableArraySequenceEqualityComparer()); // When there something to output, take all the generated strings and concatenate them to output, // and raise all of the created diagnostics. @@ -354,5 +357,38 @@ private sealed record class DiagnosticData(DiagnosticDescriptor descriptor, Loca /// Create a from the data. public Diagnostic ToDiagnostic() => Diagnostic.Create(descriptor, location, arg is null ? Array.Empty() : new[] { arg }); } + + private sealed class ObjectImmutableArraySequenceEqualityComparer : IEqualityComparer> + { + public bool Equals(ImmutableArray left, ImmutableArray right) + { + if (left.Length != right.Length) + { + return false; + } + + for (int i = 0; i < left.Length; i++) + { + bool areEqual = left[i] is { } leftElem + ? leftElem.Equals(right[i]) + : right[i] is null; + + if (!areEqual) + { + return false; + } + } + + return true; + } + + public int GetHashCode([DisallowNull] ImmutableArray obj) + { + int hash = 0; + for (int i = 0; i < obj.Length; i++) + hash = (hash, obj[i]).GetHashCode(); + return hash; + } + } } }