Skip to content

Commit cf0fc4d

Browse files
Alexander KotAlexander Kot
Alexander Kot
authored and
Alexander Kot
committed
Improve some sparse arrays
1 parent f5a7a1f commit cf0fc4d

File tree

7 files changed

+29
-12
lines changed

7 files changed

+29
-12
lines changed

src/NHibernate/Loader/Loader.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public sealed class QueryCacheInfo
8282
/// <summary>
8383
/// Caches subclass entity aliases for given persister index in <see cref="EntityPersisters"/> and subclass entity name
8484
/// </summary>
85-
private readonly ConcurrentDictionary<Tuple<int, string>, string[][]> _subclassEntityAliasesMap = new ConcurrentDictionary<Tuple<int, string>, string[][]>();
85+
private readonly Lazy<ConcurrentDictionary<Tuple<int, string>, string[][]>> _subclassEntityAliasesMap = new(()=>new ConcurrentDictionary<Tuple<int, string>, string[][]>());
8686

8787
protected Loader(ISessionFactoryImplementor factory)
8888
{
@@ -1322,7 +1322,7 @@ private void LoadFromResultSet(DbDataReader rs, int i, object obj, ILoadable per
13221322
private string[][] GetSubclassEntityAliases(int i, ILoadable persister)
13231323
{
13241324
var cacheKey = System.Tuple.Create(i, persister.EntityName);
1325-
return _subclassEntityAliasesMap.GetOrAdd(
1325+
return _subclassEntityAliasesMap.Value.GetOrAdd(
13261326
cacheKey,
13271327
k => EntityAliases[i].GetSuffixedPropertyAliases(persister));
13281328
}

src/NHibernate/Mapping/Constraint.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace NHibernate.Mapping
1515
public abstract class Constraint : IRelationalModel
1616
{
1717
private string name;
18-
private readonly List<Column> columns = new List<Column>();
18+
private readonly List<Column> columns = new List<Column>(1);
1919
private Table table;
2020

2121
/// <summary>

src/NHibernate/Mapping/ForeignKey.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text;
33
using NHibernate.Util;
44
using System;
5+
using System.Linq;
56

67
namespace NHibernate.Mapping
78
{
@@ -14,7 +15,7 @@ public class ForeignKey : Constraint
1415
private Table referencedTable;
1516
private string referencedEntityName;
1617
private bool cascadeDeleteEnabled;
17-
private readonly List<Column> referencedColumns = new List<Column>();
18+
private List<Column> referencedColumns;
1819

1920
/// <summary>
2021
/// Generates the SQL string to create the named Foreign Key Constraint in the database.
@@ -35,7 +36,7 @@ public override string SqlConstraintString(Dialect.Dialect d, string constraintN
3536
if (IsReferenceToPrimaryKey)
3637
refiter = referencedTable.PrimaryKey.ColumnIterator;
3738
else
38-
refiter = referencedColumns;
39+
refiter = referencedColumns ?? Enumerable.Empty<Column>();
3940
foreach (Column column in ColumnIterator)
4041
{
4142
cols[i] = column.GetQuotedName(d);
@@ -172,13 +173,14 @@ public virtual void AddReferencedColumns(IEnumerable<Column> referencedColumnsIt
172173

173174
private void AddReferencedColumn(Column column)
174175
{
176+
referencedColumns ??= new List<Column>(1);
175177
if (!referencedColumns.Contains(column))
176178
referencedColumns.Add(column);
177179
}
178180

179181
internal void AddReferencedTable(PersistentClass referencedClass)
180182
{
181-
if (referencedColumns.Count > 0)
183+
if (referencedColumns != null && referencedColumns.Count > 0)
182184
{
183185
referencedTable = referencedColumns[0].Value.Table;
184186
}
@@ -199,7 +201,7 @@ public override string ToString()
199201
.Append(string.Join(", ", Columns))
200202
.Append(" ref-columns:")
201203
.Append('(')
202-
.Append(string.Join(", ", ReferencedColumns))
204+
.Append(string.Join(", ", ReferencedColumnsReadOnly))
203205
.Append(") as ")
204206
.Append(Name);
205207
return result.ToString();
@@ -218,7 +220,16 @@ public bool HasPhysicalConstraint
218220

219221
public IList<Column> ReferencedColumns
220222
{
221-
get { return referencedColumns; }
223+
get
224+
{
225+
referencedColumns ??= new List<Column>(1);
226+
return referencedColumns;
227+
}
228+
}
229+
230+
private IEnumerable<Column> ReferencedColumnsReadOnly
231+
{
232+
get { return referencedColumns ?? Enumerable.Empty<Column>(); }
222233
}
223234

224235
public string ReferencedEntityName
@@ -230,7 +241,7 @@ public string ReferencedEntityName
230241
/// <summary>Does this foreignkey reference the primary key of the reference table </summary>
231242
public bool IsReferenceToPrimaryKey
232243
{
233-
get { return referencedColumns.Count == 0; }
244+
get { return referencedColumns == null || referencedColumns.Count == 0; }
234245
}
235246

236247
public string GeneratedConstraintNamePrefix => "FK_";
@@ -242,7 +253,7 @@ public override bool IsGenerated(Dialect.Dialect dialect)
242253
if (dialect.SupportsNullInUnique || IsReferenceToPrimaryKey)
243254
return true;
244255

245-
foreach (var column in ReferencedColumns)
256+
foreach (var column in ReferencedColumnsReadOnly)
246257
{
247258
if (column.IsNullable)
248259
return false;

src/NHibernate/Mapping/Index.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace NHibernate.Mapping
1414
public class Index : IRelationalModel
1515
{
1616
private Table table;
17-
private readonly List<Column> columns = new List<Column>();
17+
private readonly List<Column> columns = new List<Column>(1);
1818
private string name;
1919

2020
public static string BuildSqlCreateIndexString(Dialect.Dialect dialect, string name, Table table,

src/NHibernate/Mapping/SimpleValue.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace NHibernate.Mapping
1515
[Serializable]
1616
public class SimpleValue : IKeyValue
1717
{
18-
private readonly List<ISelectable> columns = new List<ISelectable>();
18+
private readonly List<ISelectable> columns = new List<ISelectable>(1);
1919
private IType type;
2020
private IDictionary<string, string> typeParameters;
2121

src/NHibernate/Mapping/Table.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1064,9 +1064,12 @@ internal ForeignKeyKey(IEnumerable<Column> columns, string referencedClassName,
10641064
{
10651065
this.referencedClassName = referencedClassName;
10661066
this.columns = new List<Column>(columns);
1067+
this.columns.TrimExcess();
1068+
10671069
if (referencedColumns != null)
10681070
{
10691071
this.referencedColumns = new List<Column>(referencedColumns);
1072+
this.referencedColumns.TrimExcess();
10701073
}
10711074
else
10721075
{

src/NHibernate/SqlCommand/SqlString.cs

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ internal SqlString(IEnumerable<object> parts)
215215
_firstPartIndex = _parts.Count > 0 ? 0 : -1;
216216
_lastPartIndex = _parts.Count - 1;
217217
_length = sqlIndex;
218+
219+
_parts.TrimExcess();
220+
_parameters.TrimExcess();
218221
}
219222

220223
#endregion

0 commit comments

Comments
 (0)