Skip to content

Commit 8f33dde

Browse files
authored
Convert Pinner & SyncManager into structs to save allocations (#340)
* Convert `Pinner` into `struct` to save allocations * Convert `Pinner` into `struct` to save allocations * Dont make struct field readonly
1 parent 6d66e27 commit 8f33dde

File tree

3 files changed

+10
-36
lines changed

3 files changed

+10
-36
lines changed

Orm/Xtensive.Orm/Orm/Internals/PairIntegrity/SyncManager.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
// Created by: Dmitri Maximov
55
// Created: 2008.10.08
66

7-
using System;
8-
using Xtensive.Core;
97
using Xtensive.Orm.Internals;
108
using Xtensive.Orm.Model;
119
using Xtensive.Orm.ReferentialIntegrity;
1210

1311
namespace Xtensive.Orm.PairIntegrity
1412
{
15-
internal class SyncManager : SessionBound
13+
internal readonly struct SyncManager(Session session)
1614
{
1715
public void ProcessRecursively(SyncContext context, RemovalContext removalContext,
1816
OperationType type, AssociationInfo association, Entity owner, Entity target,
@@ -62,7 +60,7 @@ private SyncContext CreateContext(RemovalContext removalContext,
6260
context.Enqueue(new SyncAction(slaveActions.Break, association.Reversed, slave1, master1));
6361
break;
6462
case OperationType.Remove:
65-
var currentRemovalContext = Session.RemovalProcessor.Context;
63+
var currentRemovalContext = session.RemovalProcessor.Context;
6664
var isNotYetRemoved = currentRemovalContext==null || !currentRemovalContext.Contains(slave2);
6765
if ((!(association.IsLoop && master1==slave2)) && isNotYetRemoved)
6866
context.Enqueue(new SyncAction(slaveActions.Break, association.Reversed, slave2, master1));
@@ -92,13 +90,5 @@ private static SyncActionSet GetSyncActions(AssociationInfo association)
9290
throw new ArgumentOutOfRangeException("association");
9391
}
9492
}
95-
96-
97-
// Constructors
98-
99-
public SyncManager(Session session)
100-
: base(session)
101-
{
102-
}
10393
}
10494
}

Orm/Xtensive.Orm/Orm/Internals/Pinner.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// Created by: Denis Krjuchkov
55
// Created: 2009.12.11
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
107
using Xtensive.Orm.Model;
118

129
namespace Xtensive.Orm.Internals
@@ -16,7 +13,7 @@ public readonly struct PinnerDisposableRemover(HashSet<EntityState> roots, Entit
1613
public void Dispose() => roots?.Remove(state);
1714
}
1815

19-
internal sealed class Pinner : SessionBound
16+
internal struct Pinner(Session session)
2017
{
2118
private readonly HashSet<EntityState> roots = new();
2219

@@ -38,8 +35,8 @@ public void Process(EntityChangeRegistry registry)
3835
activeRegistry = registry;
3936
PinAll();
4037

41-
PinnedItems = new EntityChangeRegistry(Session);
42-
PersistableItems = new EntityChangeRegistry(Session);
38+
PinnedItems = new EntityChangeRegistry(session);
39+
PersistableItems = new EntityChangeRegistry(session);
4340

4441
ProcessRegistry(PersistenceState.New);
4542
ProcessRegistry(PersistenceState.Modified);
@@ -130,18 +127,5 @@ private IEnumerable<EntityState> QueryRegistry(TypeInfo type)
130127
}
131128

132129
#endregion
133-
134-
135-
// Constructors
136-
137-
/// <summary>
138-
/// Initializes a new instance of this class.
139-
/// </summary>
140-
/// <param name="session"><see cref="Session"/>, to which current instance
141-
/// is bound.</param>
142-
public Pinner(Session session)
143-
: base(session)
144-
{
145-
}
146130
}
147-
}
131+
}

Orm/Xtensive.Orm/Orm/Session.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static readonly Type
8585

8686
private readonly bool allowSwitching;
8787
private readonly long identifier;
88-
private readonly Pinner pinner;
88+
private Pinner pinner;
8989

9090
private int? commandTimeout;
9191
private volatile int isDisposing; // To prevent double-disposing
@@ -237,7 +237,7 @@ public static Func<Session> Resolver
237237

238238
internal HandlerAccessor Handlers { get; private set; }
239239

240-
internal SyncManager PairSyncManager { get; private set; }
240+
internal SyncManager PairSyncManager { get; }
241241

242242
internal RemovalProcessor RemovalProcessor { get; private set; }
243243

@@ -559,9 +559,9 @@ internal Session(Domain domain, StorageNode selectedStorageNode, SessionConfigur
559559
SystemEvents = new SessionEventAccessor(this, true);
560560

561561
// Etc.
562-
PairSyncManager = new SyncManager(this);
562+
PairSyncManager = new(this);
563563
RemovalProcessor = new RemovalProcessor(this);
564-
pinner = new Pinner(this);
564+
pinner = new(this);
565565
Operations = new OperationRegistry(this);
566566
NonPairedReferencesRegistry = new(this);
567567

0 commit comments

Comments
 (0)