Skip to content

Optimize PersistentGenericBag.EqualsSnapshot #2399

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

Merged
merged 5 commits into from
Jun 7, 2020
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,14 +8,16 @@
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using NHibernate.Collection;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.GenericTest.BagGeneric
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class BagGenericFixtureAsync : TestCase
{
Expand Down Expand Up @@ -74,6 +76,51 @@ public async Task SimpleAsync()
s.Close();
}

[Test]
public async Task EqualsSnapshotAsync()
{
var a = new A {Name = "first generic type"};
var i0 = new B {Name = "1"};
var i4 = new B {Name = "4"};
a.Items = new List<B>
{
i0,
i0,
new B {Name = "2"},
new B {Name = "3"},
i4,
i4,
};
var lastIdx = a.Items.Count - 1;
using (var s = OpenSession())
{
await (s.SaveAsync(a));
await (s.FlushAsync());
var collection = (IPersistentCollection) a.Items;
var collectionPersister = Sfi.GetCollectionPersister(collection.Role);

a.Items[0] = i4;
Assert.Multiple(
async () =>
{
Assert.That(await (collection.EqualsSnapshotAsync(collectionPersister, CancellationToken.None)), Is.False, "modify first collection element");

a.Items[lastIdx] = i0;
Assert.That(await (collection.EqualsSnapshotAsync(collectionPersister, CancellationToken.None)), Is.True, "swap elements in collection");

a.Items[0] = i0;
a.Items[lastIdx] = i0;
Assert.That(await (collection.EqualsSnapshotAsync(collectionPersister, CancellationToken.None)), Is.False, "modify last collection element");

a.Items[lastIdx] = i4;
var reversed = a.Items.Reverse().ToArray();
a.Items.Clear();
ArrayHelper.AddAll(a.Items, reversed);
Assert.That(await (collection.EqualsSnapshotAsync(collectionPersister, CancellationToken.None)), Is.True, "reverse collection elements");
});
}
}

[Test]
public async Task CopyAsync()
{
Expand Down
50 changes: 48 additions & 2 deletions src/NHibernate.Test/GenericTest/BagGeneric/BagGenericFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using NHibernate.Collection;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.GenericTest.BagGeneric
Expand Down Expand Up @@ -63,6 +64,51 @@ public void Simple()
s.Close();
}

[Test]
public void EqualsSnapshot()
{
var a = new A {Name = "first generic type"};
var i0 = new B {Name = "1"};
var i4 = new B {Name = "4"};
a.Items = new List<B>
{
i0,
i0,
new B {Name = "2"},
new B {Name = "3"},
i4,
i4,
};
var lastIdx = a.Items.Count - 1;
using (var s = OpenSession())
{
s.Save(a);
s.Flush();
var collection = (IPersistentCollection) a.Items;
var collectionPersister = Sfi.GetCollectionPersister(collection.Role);

a.Items[0] = i4;
Assert.Multiple(
() =>
{
Assert.That(collection.EqualsSnapshot(collectionPersister), Is.False, "modify first collection element");

a.Items[lastIdx] = i0;
Assert.That(collection.EqualsSnapshot(collectionPersister), Is.True, "swap elements in collection");

a.Items[0] = i0;
a.Items[lastIdx] = i0;
Assert.That(collection.EqualsSnapshot(collectionPersister), Is.False, "modify last collection element");

a.Items[lastIdx] = i4;
var reversed = a.Items.Reverse().ToArray();
a.Items.Clear();
ArrayHelper.AddAll(a.Items, reversed);
Assert.That(collection.EqualsSnapshot(collectionPersister), Is.True, "reverse collection elements");
});
}
}

[Test]
public void Copy()
{
Expand Down
11 changes: 7 additions & 4 deletions src/NHibernate/Collection/Generic/PersistentGenericBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,15 @@ public override bool EqualsSnapshot(ICollectionPersister persister)
return false;
}

foreach (var elt in _gbag)
for (var i = 0; i < _gbag.Count; i++)
{
if (CountOccurrences(elt, _gbag, elementType) != CountOccurrences(elt, sn, elementType))
{
if (elementType.IsSame(_gbag[i], sn[i]))
continue;

var elt = _gbag[i];
var countInSnapshot = CountOccurrences(elt, sn, elementType);
Copy link
Member Author

Choose a reason for hiding this comment

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

Added shortcut if element is no longer present in snapshot

if (countInSnapshot == 0 || CountOccurrences(elt, _gbag, elementType) != countInSnapshot)
return false;
}
}

return true;
Expand Down