Skip to content

Commit c7577b8

Browse files
authored
ARCH-6225: Optimize ModelMapping memory consumption (#152)
* ARCH-6225: Optimize ModelMapping memory consumption * Invoke Array.Resize() with +10 increment to avoid resizeing on every iteration in worse case * Process negative typeId correctly
1 parent eb41ef9 commit c7577b8

File tree

2 files changed

+19
-44
lines changed

2 files changed

+19
-44
lines changed

Orm/Xtensive.Orm/Orm/Model/TypeIdRegistry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ public void Register(int typeId, TypeInfo type)
120120
EnsureNotLocked();
121121

122122
if (mapping is null) {
123-
if (typeId <= UInt16.MaxValue && type.SharedId <= UInt16.MaxValue) {
123+
if ((uint)typeId <= UInt16.MaxValue && type.SharedId <= UInt16.MaxValue) {
124124
sharedIdToTypeId ??= new UInt16[1];
125125
typeIdToSharedId ??= new UInt16[1];
126-
Array.Resize(ref sharedIdToTypeId, Math.Max(type.SharedId + 1, Math.Max(sharedIdToTypeInfo.Count, sharedIdToTypeId.Length)));
126+
Array.Resize(ref sharedIdToTypeId, Math.Max(type.SharedId + 10, Math.Max(sharedIdToTypeInfo.Count, sharedIdToTypeId.Length)));
127127
sharedIdToTypeId[type.SharedId] = (UInt16)typeId;
128-
Array.Resize(ref typeIdToSharedId, Math.Max(typeIdToSharedId.Length, typeId + 1));
128+
Array.Resize(ref typeIdToSharedId, Math.Max(typeIdToSharedId.Length, typeId + 10));
129129
typeIdToSharedId[typeId] = (UInt16) type.SharedId;
130130
return;
131131
}

Orm/Xtensive.Orm/Orm/Providers/ModelMapping.cs

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Created by: Dmitri Maximov
55
// Created: 2008.09.23
66

7+
using System;
78
using System.Collections.Generic;
89
using System.Linq;
910
using Xtensive.Core;
@@ -18,67 +19,49 @@ namespace Xtensive.Orm.Providers
1819
/// </summary>
1920
public sealed class ModelMapping : LockableBase
2021
{
21-
private readonly Dictionary<TypeInfo, Table> tableMap = new Dictionary<TypeInfo, Table>();
22-
private readonly Dictionary<SequenceInfo, SchemaNode> sequenceMap = new Dictionary<SequenceInfo, SchemaNode>();
22+
private Table[] tableMap = new Table[1]; // Indexed by TypeInfo.SharedId
23+
private readonly Dictionary<SequenceInfo, SchemaNode> sequenceMap = new();
2324

2425
private string temporaryTableDatabase;
2526
private string temporaryTableSchema;
2627
private string temporaryTableCollation;
2728

2829
public string TemporaryTableDatabase
2930
{
30-
get { return temporaryTableDatabase; }
31-
set
32-
{
31+
get => temporaryTableDatabase;
32+
set {
3333
EnsureNotLocked();
3434
temporaryTableDatabase = value;
3535
}
3636
}
3737

3838
public string TemporaryTableSchema
3939
{
40-
get { return temporaryTableSchema; }
41-
set
42-
{
40+
get => temporaryTableSchema;
41+
set {
4342
EnsureNotLocked();
4443
temporaryTableSchema = value;
4544
}
4645
}
4746

4847
public string TemporaryTableCollation
4948
{
50-
get { return temporaryTableCollation; }
51-
set
52-
{
49+
get => temporaryTableCollation;
50+
set {
5351
EnsureNotLocked();
5452
temporaryTableCollation = value;
5553
}
5654
}
5755

58-
public Table this[TypeInfo typeInfo]
59-
{
60-
get
61-
{
62-
Table result;
63-
tableMap.TryGetValue(typeInfo, out result);
64-
return result;
65-
}
66-
}
56+
public Table this[TypeInfo typeInfo] => typeInfo.SharedId < tableMap.Length ? tableMap[typeInfo.SharedId] : null;
6757

68-
public SchemaNode this[SequenceInfo sequenceInfo]
69-
{
70-
get
71-
{
72-
SchemaNode result;
73-
sequenceMap.TryGetValue(sequenceInfo, out result);
74-
return result;
75-
}
76-
}
58+
public SchemaNode this[SequenceInfo sequenceInfo] => sequenceMap.GetValueOrDefault(sequenceInfo);
7759

7860
public void Register(TypeInfo typeInfo, Table table)
7961
{
8062
EnsureNotLocked();
81-
tableMap[typeInfo] = table;
63+
Array.Resize(ref tableMap, Math.Max(tableMap.Length, typeInfo.SharedId + 10));
64+
tableMap[typeInfo.SharedId] = table;
8265
}
8366

8467
public void Register(SequenceInfo sequenceInfo, SchemaNode sequence)
@@ -87,15 +70,7 @@ public void Register(SequenceInfo sequenceInfo, SchemaNode sequence)
8770
sequenceMap[sequenceInfo] = sequence;
8871
}
8972

90-
internal IList<SchemaNode> GetAllSchemaNodes()
91-
{
92-
return tableMap.Values.Union(sequenceMap.Values).ToList();
93-
}
94-
95-
// Constructors
96-
97-
internal ModelMapping()
98-
{
99-
}
73+
internal IEnumerable<SchemaNode> GetAllSchemaNodes() =>
74+
tableMap.Where(static o => o != null).Union(sequenceMap.Values);
10075
}
101-
}
76+
}

0 commit comments

Comments
 (0)