Skip to content
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 a couple of bugs in SG #55032

Merged
merged 3 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using Xunit;
using Roslyn.Test.Utilities.TestGenerators;
using Roslyn.Test.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.Semantic.UnitTests.SourceGeneration
{
Expand Down Expand Up @@ -295,6 +297,59 @@ public void Driver_Table_Builder_Doesnt_Build_Twice()
Assert.Equal(2, callCount);
}

[Fact]
public void Batch_Node_Is_Cached_If_All_Inputs_Are_Cached()
{
var inputNode = new InputNode<int>((_) => ImmutableArray.Create(1, 2, 3));
BatchNode<int> batchNode = new BatchNode<int>(inputNode);

// first time through will always be added (because it's not been run before)
DriverStateTable.Builder dstBuilder = GetBuilder(DriverStateTable.Empty);
_ = dstBuilder.GetLatestStateTableForNode(batchNode);

// second time through should show as cached
dstBuilder = GetBuilder(dstBuilder.ToImmutable());
var table = dstBuilder.GetLatestStateTableForNode(batchNode);

AssertTableEntries(table, new[] { (ImmutableArray.Create(1, 2, 3), EntryState.Cached) });
}

[Fact]
public void Batch_Node_Is_Not_Cached_When_Inputs_Are_Changed()
{
int third = 3;

var inputNode = new InputNode<int>((_) => ImmutableArray.Create(1, 2, third++));
BatchNode<int> batchNode = new BatchNode<int>(inputNode);

// first time through will always be added (because it's not been run before)
DriverStateTable.Builder dstBuilder = GetBuilder(DriverStateTable.Empty);
_ = dstBuilder.GetLatestStateTableForNode(batchNode);

// second time through should show as modified
dstBuilder = GetBuilder(dstBuilder.ToImmutable());
var table = dstBuilder.GetLatestStateTableForNode(batchNode);

AssertTableEntries(table, new[] { (ImmutableArray.Create(1, 2, 4), EntryState.Modified) });
}

[Fact]
public void User_Comparer_Is_Not_Used_To_Determine_Inputs()
{
var inputNode = new InputNode<int>((_) => ImmutableArray.Create(1, 2, 3))
.WithComparer(new LambdaComparer<int>((a, b) => false));

// first time through will always be added (because it's not been run before)
DriverStateTable.Builder dstBuilder = GetBuilder(DriverStateTable.Empty);
_ = dstBuilder.GetLatestStateTableForNode(inputNode);

// second time through should show as cached, even though we supplied a comparer (comparer should only used to turn modified => cached)
dstBuilder = GetBuilder(dstBuilder.ToImmutable());
var table = dstBuilder.GetLatestStateTableForNode(inputNode);

AssertTableEntries(table, new[] { (1, EntryState.Cached), (2, EntryState.Cached), (3, EntryState.Cached) });
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

private void AssertTableEntries<T>(NodeStateTable<T> table, IList<(T item, EntryState state)> expected)
{
int index = 0;
Expand All @@ -306,6 +361,17 @@ private void AssertTableEntries<T>(NodeStateTable<T> table, IList<(T item, Entry
}
}

private void AssertTableEntries<T>(NodeStateTable<ImmutableArray<T>> table, IList<(ImmutableArray<T> item, EntryState state)> expected)
{
int index = 0;
foreach (var entry in table)
{
AssertEx.Equal(expected[index].item, entry.item);
Assert.Equal(expected[index].state, entry.state);
index++;
}
}

private DriverStateTable.Builder GetBuilder(DriverStateTable previous)
{
var options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ public NodeStateTable<ImmutableArray<TInput>> UpdateStateTable(DriverStateTable.
// - Added when the previous table was empty
// - Modified otherwise

var source = ImmutableArray.Create(sourceTable.Batch());
var source = sourceTable.Batch();

// update the table
var newTable = previousTable.ToBuilder();
if (!newTable.TryModifyEntries(source, _comparer))
if (!sourceTable.IsCached || !newTable.TryUseCachedEntries())
{
newTable.AddEntries(source, EntryState.Added);
if (!newTable.TryModifyEntry(source, _comparer))
{
newTable.AddEntry(source, EntryState.Added);
}
}

return newTable.ToImmutableAndFree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public NodeStateTable<T> UpdateStateTable(DriverStateTable.Builder graphState, N
var inputItems = _getInput(graphState);

// create a mutable hashset of the new items we can check against
HashSet<T> itemsSet = new HashSet<T>(_comparer);
HashSet<T> itemsSet = new HashSet<T>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we using the custom comparer here?

Copy link
Contributor Author

@chsienki chsienki Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the bug. Because these are 'input' items (compilations / syntax trees / additional files etc) we control their lifetime. If the user comparer is used they can say that two additional files are different when they are not; that breaks our internal assumptions around comparers that they can only be used to change a modified value back to a cached value.

Basically the only time the comparer should be used is if we think the item has changed, the user has to the ability to override it.

foreach (var item in inputItems)
{
var added = itemsSet.Add(item);
Expand Down