Skip to content

Commit

Permalink
Apply sequence equality comparison to the final Regex incremental val…
Browse files Browse the repository at this point in the history
…ue. (#92835)

* Apply sequence equality comparison to the final Regex incremental value.

* Avoid using SequenceEqual
  • Loading branch information
eiriktsarpalis authored Sep 29, 2023
1 parent 01104d6 commit eaa8ea1
Showing 1 changed file with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -354,5 +357,38 @@ private sealed record class DiagnosticData(DiagnosticDescriptor descriptor, Loca
/// <summary>Create a <see cref="Diagnostic"/> from the data.</summary>
public Diagnostic ToDiagnostic() => Diagnostic.Create(descriptor, location, arg is null ? Array.Empty<object>() : new[] { arg });
}

private sealed class ObjectImmutableArraySequenceEqualityComparer : IEqualityComparer<ImmutableArray<object>>
{
public bool Equals(ImmutableArray<object> left, ImmutableArray<object> 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<object> obj)
{
int hash = 0;
for (int i = 0; i < obj.Length; i++)
hash = (hash, obj[i]).GetHashCode();
return hash;
}
}
}
}

0 comments on commit eaa8ea1

Please sign in to comment.