forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better caching for RazorSourceGenerator when SuppressRazorSourceGener…
…ator changes (dotnet#23358) In VisualStudio SuppressRazorSourceGenerator changes when it's being invoked by the EnC and tooling. In our current implementation, while we no-op the operation, when SuppressRazorSourceGenerator=true, the code also evicts the valid previously cached results when SuppressRazorSourceGenerator=false by returning null values, causing us do work the next time its false. Contributes to dotnet/aspnetcore#32867
- Loading branch information
Showing
8 changed files
with
706 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/RazorSdk/SourceGenerators/RazorSourceGeneratorComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.NET.Sdk.Razor.SourceGenerators | ||
{ | ||
/// <summary> | ||
/// A comparer used to determine if an incremental steps needs to be re-run accounting for <see cref="RazorSourceGenerationOptions.SuppressRazorSourceGenerator"/>. | ||
/// <para> | ||
/// In VS, design time builds are executed with <see cref="RazorSourceGenerationOptions.SuppressRazorSourceGenerator"/> set to <c>true</c>. In this case, RSG can safely | ||
/// allow previously cached results to be used, while no-oping in the step that adds sources to the context. This allows source generator caches from being evicted | ||
/// when the value of this property flip-flips during a hot-reload / EnC session. | ||
/// </para> | ||
/// </summary> | ||
internal sealed class RazorSourceGeneratorComparer<T> : IEqualityComparer<(T Left, RazorSourceGenerationOptions Right)> where T : notnull | ||
{ | ||
private readonly Func<T, T, bool> _equals; | ||
public RazorSourceGeneratorComparer(Func<T, T, bool>? equals = null) | ||
{ | ||
_equals = equals ?? EqualityComparer<T>.Default.Equals; | ||
} | ||
|
||
public bool Equals((T Left, RazorSourceGenerationOptions Right) x, (T Left, RazorSourceGenerationOptions Right) y) | ||
{ | ||
if (y.Right.SuppressRazorSourceGenerator) | ||
{ | ||
// If source generation is suppressed, we can always use previously cached results. | ||
return true; | ||
} | ||
|
||
return _equals(x.Left, y.Left) && x.Right.EqualsIgnoringSupression(y.Right); | ||
} | ||
|
||
public int GetHashCode((T Left, RazorSourceGenerationOptions Right) obj) => obj.Left.GetHashCode(); | ||
} | ||
} |
Oops, something went wrong.