Skip to content

Commit af4427e

Browse files
committed
Optimize GetChangedEntities()
1 parent 5693bbe commit af4427e

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

Extensions/Xtensive.Orm.Tracking/Internals/SessionTrackingMonitor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ private void OnPersisting(object sender, EventArgs e)
5555
{
5656
var frame = stack.Peek();
5757

58-
foreach (var state in accessor.GetChangedEntities(PersistenceState.Removed))
58+
foreach (var state in accessor.GetChangedEntitiesInternal(PersistenceState.Removed))
5959
frame.Register(new TrackingItem(state.Key, TrackingItemState.Deleted, state.DifferentialTuple));
6060

61-
foreach (var state in accessor.GetChangedEntities(PersistenceState.New))
61+
foreach (var state in accessor.GetChangedEntitiesInternal(PersistenceState.New))
6262
frame.Register(new TrackingItem(state.Key, TrackingItemState.Created, state.DifferentialTuple));
6363

64-
foreach (var state in accessor.GetChangedEntities(PersistenceState.Modified))
64+
foreach (var state in accessor.GetChangedEntitiesInternal(PersistenceState.Modified))
6565
frame.Register(new TrackingItem(state.Key, TrackingItemState.Changed, state.DifferentialTuple));
6666
}
6767

Orm/Xtensive.Orm/Orm/Internals/EntityChangeRegistry.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2008-2020 Xtensive LLC.
1+
// Copyright (C) 2008-2022 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Dmitri Maximov
@@ -16,15 +16,14 @@ namespace Xtensive.Orm.Internals
1616
/// </summary>
1717
public sealed class EntityChangeRegistry : SessionBound
1818
{
19-
private readonly HashSet<EntityState> @new = new HashSet<EntityState>();
20-
private readonly HashSet<EntityState> modified = new HashSet<EntityState>();
21-
private readonly HashSet<EntityState> removed = new HashSet<EntityState>();
22-
private int count;
19+
private readonly HashSet<EntityState> @new = new();
20+
private readonly HashSet<EntityState> modified = new();
21+
private readonly HashSet<EntityState> removed = new();
2322

2423
/// <summary>
2524
/// Gets the number of registered entities.
2625
/// </summary>
27-
public int Count { get { return count; } }
26+
public int Count { get; private set; }
2827

2928
/// <summary>
3029
/// Registers the specified item.
@@ -35,7 +34,7 @@ internal void Register(EntityState item)
3534
// Remove-create sequences fix for Issue 690
3635
if (item.PersistenceState == PersistenceState.New && removed.Contains(item)) {
3736
removed.Remove(item);
38-
count--;
37+
Count--;
3938
if (item.DifferentialTuple.Difference == null) {
4039
item.SetPersistenceState(PersistenceState.Synchronized);
4140
return;
@@ -44,17 +43,17 @@ internal void Register(EntityState item)
4443
}
4544
else if (item.PersistenceState == PersistenceState.Removed && @new.Contains(item)) {
4645
@new.Remove(item);
47-
count--;
46+
Count--;
4847
return;
4948
}
5049
else if (item.PersistenceState == PersistenceState.Removed && modified.Contains(item)) {
5150
modified.Remove(item);
52-
count--;
51+
Count--;
5352
}
5453

5554
var container = GetContainer(item.PersistenceState);
5655
if (container.Add(item))
57-
count++;
56+
Count++;
5857
}
5958

6059
/// <summary>
@@ -73,26 +72,20 @@ public IEnumerable<EntityState> GetItems(PersistenceState state)
7372
/// </summary>
7473
public void Clear()
7574
{
76-
count = 0;
75+
Count = 0;
7776
@new.Clear();
7877
modified.Clear();
7978
removed.Clear();
8079
}
8180

8281
/// <exception cref="ArgumentOutOfRangeException"><paramref name="state"/> is out of range.</exception>
83-
private HashSet<EntityState> GetContainer(PersistenceState state)
84-
{
85-
switch (state) {
86-
case PersistenceState.New:
87-
return @new;
88-
case PersistenceState.Modified:
89-
return modified;
90-
case PersistenceState.Removed:
91-
return removed;
92-
default:
93-
throw new ArgumentOutOfRangeException("state");
94-
}
95-
}
82+
internal HashSet<EntityState> GetContainer(PersistenceState state) =>
83+
state switch {
84+
PersistenceState.New => @new,
85+
PersistenceState.Modified => modified,
86+
PersistenceState.Removed => removed,
87+
_ => throw new ArgumentOutOfRangeException(nameof(state))
88+
};
9689

9790

9891
// Constructors

Orm/Xtensive.Orm/Orm/Services/DirectSessionAccessor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public IDisposable NullifySessionTransaction()
7676
/// </summary>
7777
/// <param name="persistenceState">Type of entity change.</param>
7878
/// <returns><see cref="EntityState"/>s with the specified <paramref name="persistenceState"/>.</returns>
79-
public IEnumerable<EntityState> GetChangedEntities(PersistenceState persistenceState)
80-
{
81-
return Session.EntityChangeRegistry.GetItems(persistenceState);
82-
}
79+
public IEnumerable<EntityState> GetChangedEntities(PersistenceState persistenceState) =>
80+
Session.EntityChangeRegistry.GetItems(persistenceState);
8381

82+
internal IEnumerable<EntityState> GetChangedEntitiesInternal(PersistenceState persistenceState) =>
83+
Session.EntityChangeRegistry.GetItems(persistenceState);
8484

8585
// Constructors
8686

Orm/Xtensive.Orm/Properties/Visibility.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
using System.Runtime.CompilerServices;
88

9-
[assembly: InternalsVisibleTo("Xtensive.Orm.Tests.Framework, PublicKey=" +
9+
[assembly: InternalsVisibleTo("Xtensive.Orm.Tests.Framework, PublicKey=" +
1010
"0024000004800000940000000602000000240000525341310004000001000100fbdd689d62e9c6" +
1111
"7bb6356267f95e0b58d478cf56393c4f060fbaff42a9686272e37009ab71bfa2e41046e952f389" +
1212
"f37c6a033d1a2a5354fc97226fc469128e49e6a479ac5d1dd69d7da5607d0dc4ede0765d477745" +
1313
"1034dc3a15f1532d010db3e633e62fc5e67a3ed175457acb9dc6c9d39ccc8ecfdaae62df34d488" +
1414
"c45009b2")]
15-
[assembly: InternalsVisibleTo("Xtensive.Orm.Tests.Core, PublicKey=" +
15+
[assembly: InternalsVisibleTo("Xtensive.Orm.Tests.Core, PublicKey=" +
1616
"0024000004800000940000000602000000240000525341310004000001000100fbdd689d62e9c6" +
1717
"7bb6356267f95e0b58d478cf56393c4f060fbaff42a9686272e37009ab71bfa2e41046e952f389" +
1818
"f37c6a033d1a2a5354fc97226fc469128e49e6a479ac5d1dd69d7da5607d0dc4ede0765d477745" +
1919
"1034dc3a15f1532d010db3e633e62fc5e67a3ed175457acb9dc6c9d39ccc8ecfdaae62df34d488" +
2020
"c45009b2")]
21-
[assembly: InternalsVisibleTo("Xtensive.Orm.Tests, PublicKey=" +
21+
[assembly: InternalsVisibleTo("Xtensive.Orm.Tests, PublicKey=" +
2222
"0024000004800000940000000602000000240000525341310004000001000100fbdd689d62e9c6" +
2323
"7bb6356267f95e0b58d478cf56393c4f060fbaff42a9686272e37009ab71bfa2e41046e952f389" +
2424
"f37c6a033d1a2a5354fc97226fc469128e49e6a479ac5d1dd69d7da5607d0dc4ede0765d477745" +
@@ -29,4 +29,10 @@
2929
"7bb6356267f95e0b58d478cf56393c4f060fbaff42a9686272e37009ab71bfa2e41046e952f389" +
3030
"f37c6a033d1a2a5354fc97226fc469128e49e6a479ac5d1dd69d7da5607d0dc4ede0765d477745" +
3131
"1034dc3a15f1532d010db3e633e62fc5e67a3ed175457acb9dc6c9d39ccc8ecfdaae62df34d488" +
32-
"c45009b2")]
32+
"c45009b2")]
33+
[assembly: InternalsVisibleTo("Xtensive.Orm.Tracking, PublicKey=" +
34+
"00240000048000009400000006020000002400005253413100040000010001007f8790a0573a2a" +
35+
"4f7b77907240977b669d33774bcaef77ef39d5186de4d4c836623fb661be674c7db731bc376184" +
36+
"f6848fc6b64eeba506654c8212de624e9d1f15ad1fe765f769e9a5b59ecaab2866632a983e7828" +
37+
"f3bf20ff7afffa021951cf8adc9ac1e786716430b0637893fb71b7566405fe14acc5e340c4af4a" +
38+
"a293c994")]

0 commit comments

Comments
 (0)