-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix throw in generator comparer #76769
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ internal sealed class InputNode<T> : IIncrementalGeneratorNode<T> | |
private readonly Func<DriverStateTable.Builder, ImmutableArray<T>> _getInput; | ||
private readonly Action<IIncrementalGeneratorOutputNode> _registerOutput; | ||
private readonly IEqualityComparer<T> _inputComparer; | ||
private readonly IEqualityComparer<T> _comparer; | ||
private readonly IEqualityComparer<T>? _comparer; | ||
private readonly string? _name; | ||
|
||
public InputNode(Func<DriverStateTable.Builder, ImmutableArray<T>> getInput, IEqualityComparer<T>? inputComparer = null) | ||
|
@@ -35,7 +35,7 @@ public InputNode(Func<DriverStateTable.Builder, ImmutableArray<T>> getInput, IEq | |
private InputNode(Func<DriverStateTable.Builder, ImmutableArray<T>> getInput, Action<IIncrementalGeneratorOutputNode>? registerOutput, IEqualityComparer<T>? inputComparer = null, IEqualityComparer<T>? comparer = null, string? name = null) | ||
{ | ||
_getInput = getInput; | ||
_comparer = comparer ?? EqualityComparer<T>.Default; | ||
_comparer = comparer; | ||
_inputComparer = inputComparer ?? EqualityComparer<T>.Default; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we control the input comparer and all the inputs it compares. If that's throwing it's a real product bug and we don't want to falsely attribute it to a generator. |
||
_registerOutput = registerOutput ?? (o => throw ExceptionUtilities.Unreachable()); | ||
_name = name; | ||
|
@@ -83,7 +83,7 @@ public NodeStateTable<T> UpdateStateTable(DriverStateTable.Builder graphState, N | |
// This allows us to correctly 'replace' items even when they aren't actually the same. In the case that the | ||
// item really isn't modified, but a new item, we still function correctly as we mostly treat them the same, | ||
// but will perform an extra comparison that is omitted in the pure 'added' case. | ||
var modified = tableBuilder.TryModifyEntry(inputItems[itemIndex], _comparer, elapsedTime, noInputStepsStepInfo, EntryState.Modified); | ||
var modified = tableBuilder.TryModifyEntry(inputItems[itemIndex], elapsedTime, noInputStepsStepInfo, EntryState.Modified); | ||
Debug.Assert(modified); | ||
itemsSet.Remove(inputItems[itemIndex]); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ public Builder(PredicateSyntaxStrategy<T> owner, object key, StateTableStore tab | |
_name = name; | ||
_comparer = comparer; | ||
_key = key; | ||
_filterTable = table.GetStateTableOrEmpty<SyntaxNode>(_owner._filterKey).ToBuilder(stepName: null, trackIncrementalSteps); | ||
_filterTable = table.GetStateTableOrEmpty<SyntaxNode>(_owner._filterKey).ToBuilder(stepName: null, trackIncrementalSteps, equalityComparer: Roslyn.Utilities.ReferenceEqualityComparer.Instance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was code written by Cyrus originally, I'm refactoring it to keep it semantically the same. |
||
_transformTable = table.GetStateTableOrEmpty<T>(_key).ToBuilder(_name, trackIncrementalSteps, _comparer); | ||
} | ||
|
||
|
@@ -85,7 +85,7 @@ public void VisitTree( | |
var stopwatch = SharedStopwatch.StartNew(); | ||
var nodes = getFilteredNodes(root.Value, _owner._filterFunc, cancellationToken); | ||
|
||
if (state != EntryState.Modified || !_filterTable.TryModifyEntries(nodes, Roslyn.Utilities.ReferenceEqualityComparer.Instance, stopwatch.Elapsed, noInputStepsStepInfo, state, out entry)) | ||
if (state != EntryState.Modified || !_filterTable.TryModifyEntries(nodes, stopwatch.Elapsed, noInputStepsStepInfo, state, out entry)) | ||
{ | ||
entry = _filterTable.AddEntries(nodes, state, stopwatch.Elapsed, noInputStepsStepInfo, state); | ||
} | ||
|
@@ -108,7 +108,7 @@ public void VisitTree( | |
// so we never consider the input to the transform as cached. | ||
var transformInputState = state == EntryState.Cached ? EntryState.Modified : state; | ||
|
||
if (transformInputState == EntryState.Added || !_transformTable.TryModifyEntry(transformed, _comparer, stopwatch.Elapsed, noInputStepsStepInfo, transformInputState)) | ||
if (transformInputState == EntryState.Added || !_transformTable.TryModifyEntry(transformed, stopwatch.Elapsed, noInputStepsStepInfo, transformInputState)) | ||
{ | ||
_transformTable.AddEntry(transformed, EntryState.Added, stopwatch.Elapsed, noInputStepsStepInfo, EntryState.Added); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,12 +46,12 @@ public NodeStateTable<TOutput> UpdateStateTable(DriverStateTable.Builder graphSt | |
this.LogTables(stepName, s_tableType, previousTable, previousTable, sourceTable); | ||
if (graphState.DriverState.TrackIncrementalSteps) | ||
{ | ||
return previousTable.CreateCachedTableWithUpdatedSteps(sourceTable, stepName, EqualityComparer<TOutput>.Default); | ||
return previousTable.CreateCachedTableWithUpdatedSteps(sourceTable, stepName, equalityComparer: null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This creates a new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if it's all cached the comparer would never be used. Later on, if there's a modification the table will be re-created and the real comparer will be passed to it. |
||
} | ||
return previousTable; | ||
} | ||
|
||
var tableBuilder = graphState.CreateTableBuilder(previousTable, stepName, EqualityComparer<TOutput>.Default); | ||
var tableBuilder = graphState.CreateTableBuilder(previousTable, stepName, equalityComparer: null); | ||
foreach (var entry in sourceTable) | ||
{ | ||
var inputs = tableBuilder.TrackIncrementalSteps ? ImmutableArray.Create((entry.Step!, entry.OutputIndex)) : default; | ||
|
@@ -71,7 +71,7 @@ public NodeStateTable<TOutput> UpdateStateTable(DriverStateTable.Builder graphSt | |
_action(context, entry.Item, cancellationToken); | ||
var sourcesAndDiagnostics = (sourcesBuilder.ToImmutable(), diagnostics.ToReadOnly()); | ||
|
||
if (entry.State != EntryState.Modified || !tableBuilder.TryModifyEntry(sourcesAndDiagnostics, EqualityComparer<TOutput>.Default, stopwatch.Elapsed, inputs, entry.State)) | ||
if (entry.State != EntryState.Modified || !tableBuilder.TryModifyEntry(sourcesAndDiagnostics, stopwatch.Elapsed, inputs, entry.State)) | ||
{ | ||
tableBuilder.AddEntry(sourcesAndDiagnostics, EntryState.Added, stopwatch.Elapsed, inputs, EntryState.Added); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the check
_comparer is null
serve here? Thenull
case means it's effectively the default comparer so why are we not trying to use that forTryModifyEntry
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an optimization. If we've already determined that one of the two sides is modified then the default comparer will always find the same result. However if there's a custom comparer it's possible that will override the result and return cached.