-
Notifications
You must be signed in to change notification settings - Fork 934
Optimize PersistentGenericSet snapshot #2394
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
757bd6a
Optimize PersistentGenericSet snapshot
maca88 7718691
Fix build
maca88 0ac69bd
Add GetSnapshotTyped
maca88 3706b6d
Update src/NHibernate/Collection/Generic/SetHelpers/SetSnapShot.cs
maca88 42005ea
Update src/NHibernate/Collection/Generic/SetHelpers/SetSnapShot.cs
maca88 8896052
Revert "Add GetSnapshotTyped"
maca88 1071b50
Add SetSnapShot tests
maca88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,97 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using NHibernate.Collection.Generic.SetHelpers; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.UtilityTest | ||
{ | ||
[TestFixture] | ||
public class SetSnapShotFixture | ||
{ | ||
[Test] | ||
public void TestNullValue() | ||
{ | ||
var sn = new SetSnapShot<object>(1); | ||
Assert.That(sn, Has.Count.EqualTo(0)); | ||
Assert.That(sn, Is.EquivalentTo(new object[0])); | ||
Assert.That(sn.Contains(null), Is.False); | ||
Assert.That(sn.TryGetValue(null, out _), Is.False); | ||
|
||
sn.Add(null); | ||
Assert.That(sn, Has.Count.EqualTo(1)); | ||
Assert.That(sn, Is.EquivalentTo(new object[] {null})); | ||
|
||
Assert.That(sn.TryGetValue(null, out var value), Is.True); | ||
Assert.That(sn.Contains(null), Is.True); | ||
Assert.That(value, Is.Null); | ||
|
||
Assert.That(sn.Remove(null), Is.True); | ||
Assert.That(sn, Has.Count.EqualTo(0)); | ||
Assert.That(sn, Is.EquivalentTo(new object[0])); | ||
|
||
sn.Add(null); | ||
Assert.That(sn, Has.Count.EqualTo(1)); | ||
|
||
sn.Clear(); | ||
Assert.That(sn, Has.Count.EqualTo(0)); | ||
Assert.That(sn, Is.EquivalentTo(new object[0])); | ||
} | ||
|
||
[Test] | ||
public void TestInitialization() | ||
{ | ||
var list = new List<string> {"test1", null, "test2"}; | ||
var sn = new SetSnapShot<string>(list); | ||
Assert.That(sn, Has.Count.EqualTo(list.Count)); | ||
Assert.That(sn, Is.EquivalentTo(list)); | ||
Assert.That(sn.TryGetValue("test1", out _), Is.True); | ||
Assert.That(sn.TryGetValue(null, out _), Is.True); | ||
} | ||
|
||
[Test] | ||
public void TestCopyTo() | ||
{ | ||
var list = new List<string> {"test1", null, "test2"}; | ||
var sn = new SetSnapShot<string>(list); | ||
|
||
var array = new string[3]; | ||
sn.CopyTo(array, 0); | ||
Assert.That(list, Is.EquivalentTo(array)); | ||
} | ||
|
||
[Test] | ||
public void TestSerialization() | ||
{ | ||
var list = new List<string> {"test1", null, "test2"}; | ||
var sn = new SetSnapShot<string>(list); | ||
|
||
sn = Deserialize<SetSnapShot<string>>(Serialize(sn)); | ||
Assert.That(sn, Has.Count.EqualTo(list.Count)); | ||
Assert.That(sn, Is.EquivalentTo(list)); | ||
Assert.That(sn.TryGetValue("test1", out var item1), Is.True); | ||
Assert.That(item1, Is.EqualTo("test1")); | ||
Assert.That(sn.TryGetValue(null, out var nullValue), Is.True); | ||
Assert.That(nullValue, Is.Null); | ||
} | ||
|
||
private static byte[] Serialize<T>(T obj) | ||
{ | ||
var serializer = new BinaryFormatter(); | ||
using (var stream = new MemoryStream()) | ||
{ | ||
serializer.Serialize(stream, obj); | ||
return stream.ToArray(); | ||
} | ||
} | ||
|
||
private static T Deserialize<T>(byte[] value) | ||
{ | ||
var serializer = new BinaryFormatter(); | ||
using (var stream = new MemoryStream(value)) | ||
{ | ||
return (T) serializer.Deserialize(stream); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
10 changes: 0 additions & 10 deletions
10
src/NHibernate/Collection/Generic/SetHelpers/ISetSnapshot.cs
This file was deleted.
Oops, something went wrong.
153 changes: 112 additions & 41 deletions
153
src/NHibernate/Collection/Generic/SetHelpers/SetSnapShot.cs
This file contains hidden or 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 |
---|---|---|
@@ -1,109 +1,180 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
#if NETCOREAPP2_0 | ||
using System.Runtime.Serialization; | ||
using System.Threading; | ||
#endif | ||
|
||
namespace NHibernate.Collection.Generic.SetHelpers | ||
{ | ||
#if NETFX || NETSTANDARD2_0 | ||
// TODO 6.0: Consider removing this class in case we upgrade to .NET 4.7.2 and NET Standard 2.1, | ||
// which have HashSet<T>.TryGetValue | ||
[Serializable] | ||
internal class SetSnapShot<T> : ISetSnapshot<T> | ||
internal class SetSnapShot<T> : ICollection<T>, IReadOnlyCollection<T>, ICollection | ||
hazzik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private readonly List<T> _elements; | ||
public SetSnapShot() | ||
{ | ||
_elements = new List<T>(); | ||
} | ||
private readonly Dictionary<T, T> _values; | ||
private bool _hasNull; | ||
|
||
public SetSnapShot(int capacity) | ||
{ | ||
_elements = new List<T>(capacity); | ||
_values = new Dictionary<T, T>(capacity); | ||
} | ||
|
||
public SetSnapShot(IEnumerable<T> collection) | ||
{ | ||
_elements = new List<T>(collection); | ||
_values = new Dictionary<T, T>(); | ||
foreach (var item in collection) | ||
{ | ||
if (item == null) | ||
{ | ||
_hasNull = true; | ||
} | ||
else | ||
{ | ||
_values.Add(item, item); | ||
} | ||
} | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
public bool TryGetValue(T equalValue, out T actualValue) | ||
{ | ||
return _elements.GetEnumerator(); | ||
if (equalValue != null) | ||
{ | ||
return _values.TryGetValue(equalValue, out actualValue); | ||
} | ||
|
||
actualValue = default(T); | ||
return _hasNull; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
if (_hasNull) | ||
{ | ||
yield return default(T); | ||
} | ||
|
||
foreach (var item in _values.Keys) | ||
{ | ||
yield return item; | ||
} | ||
} | ||
|
||
public void Add(T item) | ||
{ | ||
_elements.Add(item); | ||
if (item == null) | ||
{ | ||
_hasNull = true; | ||
return; | ||
} | ||
|
||
_values.Add(item, item); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
throw new InvalidOperationException(); | ||
_values.Clear(); | ||
_hasNull = false; | ||
} | ||
|
||
public bool Contains(T item) | ||
{ | ||
return _elements.Contains(item); | ||
return item == null ? _hasNull : _values.ContainsKey(item); | ||
} | ||
|
||
public void CopyTo(T[] array, int arrayIndex) | ||
{ | ||
_elements.CopyTo(array, arrayIndex); | ||
if (_hasNull) | ||
array[arrayIndex] = default(T); | ||
_values.Keys.CopyTo(array, arrayIndex + (_hasNull ? 1 : 0)); | ||
} | ||
|
||
public bool Remove(T item) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
if (item != null) | ||
{ | ||
return _values.Remove(item); | ||
} | ||
|
||
public void CopyTo(Array array, int index) | ||
{ | ||
((ICollection)_elements).CopyTo(array, index); | ||
if (!_hasNull) | ||
{ | ||
return false; | ||
} | ||
|
||
_hasNull = false; | ||
return true; | ||
} | ||
|
||
int ICollection.Count | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
get { return _elements.Count; } | ||
return GetEnumerator(); | ||
} | ||
|
||
public object SyncRoot | ||
void ICollection.CopyTo(Array array, int index) | ||
{ | ||
get { return ((ICollection)_elements).SyncRoot; } | ||
if (!(array is T[] typedArray)) | ||
{ | ||
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array)); | ||
} | ||
|
||
CopyTo(typedArray, index); | ||
} | ||
|
||
public bool IsSynchronized | ||
public int Count => _values.Count + (_hasNull ? 1 : 0); | ||
|
||
public bool IsReadOnly => ((ICollection<KeyValuePair<T, T>>) _values).IsReadOnly; | ||
|
||
public object SyncRoot => ((ICollection) _values).SyncRoot; | ||
|
||
public bool IsSynchronized => ((ICollection) _values).IsSynchronized; | ||
} | ||
#endif | ||
|
||
#if NETCOREAPP2_0 | ||
[Serializable] | ||
internal class SetSnapShot<T> : HashSet<T>, ICollection | ||
{ | ||
[NonSerialized] | ||
private object _syncRoot; | ||
|
||
public SetSnapShot(int capacity) : base(capacity) | ||
{ | ||
get { return ((ICollection)_elements).IsSynchronized; } | ||
} | ||
|
||
int ICollection<T>.Count | ||
public SetSnapShot(IEnumerable<T> collection) : base(collection) | ||
{ | ||
get { return _elements.Count; } | ||
} | ||
|
||
int IReadOnlyCollection<T>.Count | ||
protected SetSnapShot(SerializationInfo info, StreamingContext context) : base(info, context) | ||
{ | ||
get { return _elements.Count; } | ||
} | ||
|
||
public bool IsReadOnly | ||
void ICollection.CopyTo(Array array, int index) | ||
{ | ||
get { return ((ICollection<T>)_elements).IsReadOnly; } | ||
if (!(array is T[] typedArray)) | ||
{ | ||
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array)); | ||
} | ||
|
||
CopyTo(typedArray, index); | ||
} | ||
|
||
public bool TryGetValue(T element, out T value) | ||
bool ICollection.IsSynchronized => false; | ||
|
||
object ICollection.SyncRoot | ||
{ | ||
var idx = _elements.IndexOf(element); | ||
if (idx >= 0) | ||
get | ||
{ | ||
value = _elements[idx]; | ||
return true; | ||
} | ||
if (_syncRoot == null) | ||
{ | ||
Interlocked.CompareExchange<object>(ref _syncRoot, new object(), 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. Used the same logic as |
||
} | ||
|
||
value = default(T); | ||
return false; | ||
return _syncRoot; | ||
} | ||
} | ||
} | ||
#endif | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.