Skip to content

Commit 9006998

Browse files
authored
Use optimized Dictionary.Remove(key, out value) in .NET Core (#2381)
1 parent fd16d36 commit 9006998

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

src/NHibernate/AdoNet/AbstractBatcher.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,7 @@ private Stopwatch GetReaderStopwatch(DbDataReader reader)
410410
var nhReader = reader as NHybridDataReader;
411411
var actualReader = nhReader == null ? reader : nhReader.Target;
412412

413-
Stopwatch duration;
414-
if (_readersDuration.TryGetValue(actualReader, out duration))
415-
_readersDuration.Remove(actualReader);
413+
_readersDuration.Remove(actualReader, out var duration);
416414
return duration;
417415
}
418416

src/NHibernate/Engine/StatefulPersistenceContext.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,9 @@ public IPersistentCollection UseUnownedCollection(CollectionKey key)
238238
{
239239
return null;
240240
}
241-
else
242-
{
243-
IPersistentCollection tempObject;
244-
if (unownedCollections.TryGetValue(key, out tempObject))
245-
unownedCollections.Remove(key);
246-
return tempObject;
247-
}
241+
242+
unownedCollections.Remove(key, out var tempObject);
243+
return tempObject;
248244
}
249245

250246
/// <summary> Clear the state of the persistence context</summary>
@@ -447,8 +443,9 @@ public bool ContainsEntity(EntityKey key)
447443
/// </summary>
448444
public object RemoveEntity(EntityKey key)
449445
{
450-
object tempObject = entitiesByKey[key];
451-
entitiesByKey.Remove(key);
446+
if (!entitiesByKey.Remove(key, out var tempObject))
447+
throw new KeyNotFoundException(key.ToString());
448+
452449
object entity = tempObject;
453450
List<EntityUniqueKey> toRemove = new List<EntityUniqueKey>();
454451
foreach (KeyValuePair<EntityUniqueKey, object> pair in entitiesByUniqueKey)
@@ -1099,9 +1096,7 @@ public object RemoveProxy(EntityKey key)
10991096
batchFetchQueue.RemoveBatchLoadableEntityKey(key);
11001097
batchFetchQueue.RemoveSubselect(key);
11011098
}
1102-
INHibernateProxy tempObject;
1103-
if (proxiesByKey.TryGetValue(key, out tempObject))
1104-
proxiesByKey.Remove(key);
1099+
proxiesByKey.Remove(key, out INHibernateProxy tempObject);
11051100
return tempObject;
11061101
}
11071102

@@ -1384,8 +1379,9 @@ public bool IsReadOnly(object entityOrProxy)
13841379

13851380
public void ReplaceDelayedEntityIdentityInsertKeys(EntityKey oldKey, object generatedId)
13861381
{
1387-
object tempObject = entitiesByKey[oldKey];
1388-
entitiesByKey.Remove(oldKey);
1382+
if (!entitiesByKey.Remove(oldKey, out var tempObject))
1383+
throw new KeyNotFoundException(oldKey.ToString());
1384+
13891385
object entity = tempObject;
13901386
object tempObject2 = entityEntries[entity];
13911387
entityEntries.Remove(entity);

src/NHibernate/Util/CollectionHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,16 @@ public static bool DictionaryEquals<K, V>(IDictionary<K, V> m1, IDictionary<K, V
733733
(comparer == null ? DictionaryEquals(m1, m2, EqualityComparer<V>.Default) :
734734
m1.All(kv => m2.TryGetValue(kv.Key, out var value) && comparer.Equals(kv.Value, value)));
735735

736+
//It's added to make use of optimized .NET Core Dictionary.Remove(key, out value) method
737+
internal static bool Remove<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, out TValue value)
738+
{
739+
if (!dic.TryGetValue(key, out value))
740+
return false;
741+
742+
dic.Remove(key);
743+
return true;
744+
}
745+
736746
private static bool? FastCheckEquality<T>(IEnumerable<T> c1, IEnumerable<T> c2)
737747
{
738748
if (c1 == c2)

src/NHibernate/Util/LinkedHashMap.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,12 @@ private Entry Last
349349

350350
private bool RemoveImpl(TKey key)
351351
{
352-
Entry e;
353-
bool result = false;
354-
if (entries.TryGetValue(key, out e))
355-
{
356-
result = entries.Remove(key);
357-
version++;
358-
RemoveEntry(e);
359-
}
360-
return result;
352+
if (!entries.Remove(key, out var e))
353+
return false;
354+
355+
version++;
356+
RemoveEntry(e);
357+
return true;
361358
}
362359

363360
void IDeserializationCallback.OnDeserialization(object sender)

0 commit comments

Comments
 (0)