Skip to content

Commit 46d5f0b

Browse files
committed
Optimize also IdentityRemove
1 parent 52ef196 commit 46d5f0b

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/NHibernate/Async/Collection/AbstractPersistentCollection.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,24 @@ protected virtual async Task<ICollection> GetOrphansAsync(ICollection oldElement
215215
public async Task IdentityRemoveAsync(IList list, object obj, string entityName, ISessionImplementor session, CancellationToken cancellationToken)
216216
{
217217
cancellationToken.ThrowIfCancellationRequested();
218-
if (obj != null && await (ForeignKeys.IsNotTransientSlowAsync(entityName, obj, session, cancellationToken)).ConfigureAwait(false))
219-
{
220-
IType idType = session.Factory.GetEntityPersister(entityName).IdentifierType;
218+
if (obj == null || await (ForeignKeys.IsTransientSlowAsync(entityName, obj, session, cancellationToken)).ConfigureAwait(false))
219+
return;
221220

222-
object idOfCurrent = await (ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(entityName, obj, session, cancellationToken)).ConfigureAwait(false);
223-
List<object> toRemove = new List<object>(list.Count);
224-
foreach (object current in list)
221+
var persister = session.Factory.GetEntityPersister(entityName);
222+
IType idType = persister.IdentifierType;
223+
object idToRemove = persister.GetIdentifier(obj);
224+
225+
for (var index = list.Count - 1; index >= 0; index--)
226+
{
227+
object current = list[index];
228+
if (current == null)
225229
{
226-
if (current == null)
227-
{
228-
continue;
229-
}
230-
object idOfOld = await (ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(entityName, current, session, cancellationToken)).ConfigureAwait(false);
231-
if (idType.IsEqual(idOfCurrent, idOfOld, session.Factory))
232-
{
233-
toRemove.Add(current);
234-
}
230+
continue;
235231
}
236-
foreach (object ro in toRemove)
232+
233+
if (obj == current || idType.IsEqual(idToRemove, persister.GetIdentifier(current), session.Factory))
237234
{
238-
list.Remove(ro);
235+
list.RemoveAt(index);
239236
}
240237
}
241238
}

src/NHibernate/Collection/AbstractPersistentCollection.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -756,27 +756,24 @@ protected virtual ICollection GetOrphans(ICollection oldElements, ICollection cu
756756

757757
public void IdentityRemove(IList list, object obj, string entityName, ISessionImplementor session)
758758
{
759-
if (obj != null && ForeignKeys.IsNotTransientSlow(entityName, obj, session))
760-
{
761-
IType idType = session.Factory.GetEntityPersister(entityName).IdentifierType;
759+
if (obj == null || ForeignKeys.IsTransientSlow(entityName, obj, session))
760+
return;
762761

763-
object idOfCurrent = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, obj, session);
764-
List<object> toRemove = new List<object>(list.Count);
765-
foreach (object current in list)
762+
var persister = session.Factory.GetEntityPersister(entityName);
763+
IType idType = persister.IdentifierType;
764+
object idToRemove = persister.GetIdentifier(obj);
765+
766+
for (var index = list.Count - 1; index >= 0; index--)
767+
{
768+
object current = list[index];
769+
if (current == null)
766770
{
767-
if (current == null)
768-
{
769-
continue;
770-
}
771-
object idOfOld = ForeignKeys.GetEntityIdentifierIfNotUnsaved(entityName, current, session);
772-
if (idType.IsEqual(idOfCurrent, idOfOld, session.Factory))
773-
{
774-
toRemove.Add(current);
775-
}
771+
continue;
776772
}
777-
foreach (object ro in toRemove)
773+
774+
if (obj == current || idType.IsEqual(idToRemove, persister.GetIdentifier(current), session.Factory))
778775
{
779-
list.Remove(ro);
776+
list.RemoveAt(index);
780777
}
781778
}
782779
}

0 commit comments

Comments
 (0)