Skip to content

Commit b7d08d1

Browse files
authored
Optimize PersistParameterBinding (#345)
1 parent 94c97f8 commit b7d08d1

File tree

8 files changed

+60
-90
lines changed

8 files changed

+60
-90
lines changed

Orm/Xtensive.Orm/Orm/Providers/Requests/ParameterBinding.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
// Created by: Dmitri Maximov
55
// Created: 2008.09.26
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
10-
using Xtensive.Core;
117
using Xtensive.Sql;
128
using Xtensive.Sql.Dml;
139

Orm/Xtensive.Orm/Orm/Providers/Requests/ParameterTransmissionType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Xtensive.Orm.Providers
1212
/// Possible way of delivering parameter to server
1313
/// for <see cref="QueryParameterBinding"/> and <see cref="PersistParameterBinding"/>.
1414
/// </summary>
15-
public enum ParameterTransmissionType
15+
public enum ParameterTransmissionType : byte
1616
{
1717
/// <summary>
1818
/// Indicates that no special handling of parameter is performed.
@@ -27,4 +27,4 @@ public enum ParameterTransmissionType
2727
/// </summary>
2828
BinaryLob = 2,
2929
}
30-
}
30+
}

Orm/Xtensive.Orm/Orm/Providers/Requests/PersistParameterBinding.cs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,39 @@
44
// Created by: Dmitri Maximov
55
// Created: 2008.09.25
66

7-
using System;
8-
using Xtensive.Core;
97
using Xtensive.Sql;
108

11-
namespace Xtensive.Orm.Providers
9+
namespace Xtensive.Orm.Providers;
10+
11+
/// <summary>
12+
/// A binding of a parameter for <see cref="PersistRequest"/>.
13+
/// </summary>
14+
public sealed class PersistParameterBinding(
15+
TypeMapping typeMapping,
16+
ushort rowIndex,
17+
ColNum fieldIndex,
18+
ParameterTransmissionType transmissionType = ParameterTransmissionType.Regular,
19+
PersistParameterBindingType bindingType = PersistParameterBindingType.Regular
20+
) : ParameterBinding(typeMapping, transmissionType)
1221
{
13-
/// <summary>
14-
/// A binding of a parameter for <see cref="PersistRequest"/>.
15-
/// </summary>
16-
public sealed class PersistParameterBinding : ParameterBinding
17-
{
18-
public int RowIndex { get; private set; }
19-
20-
public int FieldIndex { get; private set; }
21-
22-
public PersistParameterBindingType BindingType { get; private set; }
23-
24-
// Constructors
25-
26-
public PersistParameterBinding(TypeMapping typeMapping, int rowIndex, int fieldIndex,
27-
ParameterTransmissionType transmissionType, PersistParameterBindingType bindingType)
28-
: base(typeMapping, transmissionType)
29-
{
30-
RowIndex = rowIndex;
31-
FieldIndex = fieldIndex;
32-
BindingType = bindingType;
33-
}
34-
35-
public PersistParameterBinding(TypeMapping typeMapping, int fieldIndex, ParameterTransmissionType transmissionType, PersistParameterBindingType bindingType)
36-
: this(typeMapping, 0, fieldIndex, transmissionType, bindingType)
37-
{
38-
}
22+
public ushort RowIndex { get; } = rowIndex;
23+
public ColNum FieldIndex { get; } = fieldIndex;
24+
public PersistParameterBindingType BindingType { get; } = bindingType;
3925

40-
public PersistParameterBinding(TypeMapping typeMapping, int rowIndex, int fieldIndex, ParameterTransmissionType transmissionType)
41-
: this(typeMapping, rowIndex, fieldIndex, transmissionType, PersistParameterBindingType.Regular)
42-
{
43-
}
26+
// Constructors
4427

45-
public PersistParameterBinding(TypeMapping typeMapping, int fieldIndex, ParameterTransmissionType transmissionType)
46-
: this(typeMapping, fieldIndex, transmissionType, PersistParameterBindingType.Regular)
47-
{
48-
}
28+
public PersistParameterBinding(TypeMapping typeMapping, ColNum fieldIndex, ParameterTransmissionType transmissionType, PersistParameterBindingType bindingType)
29+
: this(typeMapping, 0, fieldIndex, transmissionType, bindingType)
30+
{
31+
}
4932

50-
public PersistParameterBinding(TypeMapping typeMapping, int rowIndex, int fieldIndex)
51-
: this(typeMapping, rowIndex, fieldIndex, ParameterTransmissionType.Regular, PersistParameterBindingType.Regular)
52-
{
53-
}
33+
public PersistParameterBinding(TypeMapping typeMapping, ColNum fieldIndex, ParameterTransmissionType transmissionType)
34+
: this(typeMapping, fieldIndex, transmissionType, PersistParameterBindingType.Regular)
35+
{
36+
}
5437

55-
public PersistParameterBinding(TypeMapping typeMapping, int fieldIndex)
56-
: this(typeMapping, fieldIndex, ParameterTransmissionType.Regular, PersistParameterBindingType.Regular)
57-
{
58-
}
38+
public PersistParameterBinding(TypeMapping typeMapping, ColNum fieldIndex)
39+
: this(typeMapping, fieldIndex, ParameterTransmissionType.Regular, PersistParameterBindingType.Regular)
40+
{
5941
}
60-
}
42+
}

Orm/Xtensive.Orm/Orm/Providers/Requests/PersistParameterBindingType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace Xtensive.Orm.Providers
33
/// <summary>
44
/// Possible types of <see cref="PersistParameterBinding"/>.
55
/// </summary>
6-
public enum PersistParameterBindingType
6+
public enum PersistParameterBindingType : byte
77
{
88
/// <summary>
99
/// Regular parameter. Parameter value is obtained thru difference tuple.
@@ -17,4 +17,4 @@ public enum PersistParameterBindingType
1717
/// </summary>
1818
VersionFilter = 1,
1919
}
20-
}
20+
}

Orm/Xtensive.Orm/Orm/Providers/Requests/PersistRequestBuilder.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal IReadOnlyList<PersistRequest> Build(StorageNode node, PersistRequestBui
6363
return result.AsSafeWrapper();
6464
}
6565

66-
protected virtual List<PersistRequest> BuildInsertRequest(PersistRequestBuilderContext context)
66+
protected virtual List<PersistRequest> BuildInsertRequest(in PersistRequestBuilderContext context)
6767
{
6868
var result = new List<PersistRequest>();
6969
foreach (var index in context.AffectedIndexes) {
@@ -88,7 +88,7 @@ protected virtual List<PersistRequest> BuildInsertRequest(PersistRequestBuilderC
8888
return result;
8989
}
9090

91-
protected virtual List<PersistRequest> BuildUpdateRequest(PersistRequestBuilderContext context)
91+
protected virtual List<PersistRequest> BuildUpdateRequest(in PersistRequestBuilderContext context)
9292
{
9393
var result = new List<PersistRequest>();
9494
foreach (var index in context.AffectedIndexes) {
@@ -130,7 +130,7 @@ protected virtual List<PersistRequest> BuildUpdateRequest(PersistRequestBuilderC
130130
return result;
131131
}
132132

133-
protected virtual List<PersistRequest> BuildRemoveRequest(PersistRequestBuilderContext context)
133+
protected virtual List<PersistRequest> BuildRemoveRequest(in PersistRequestBuilderContext context)
134134
{
135135
var result = new List<PersistRequest>();
136136
for (var i = context.AffectedIndexes.Count - 1; i >= 0; i--) {
@@ -147,7 +147,7 @@ protected virtual List<PersistRequest> BuildRemoveRequest(PersistRequestBuilderC
147147
return result;
148148
}
149149

150-
private SqlExpression BuildKeyFilter(PersistRequestBuilderContext context, SqlTableRef filteredTable, List<PersistParameterBinding> currentBindings)
150+
private SqlExpression BuildKeyFilter(in PersistRequestBuilderContext context, SqlTableRef filteredTable, List<PersistParameterBinding> currentBindings)
151151
{
152152
SqlExpression result = null;
153153
foreach (var column in context.PrimaryIndex.KeyColumns.Keys) {
@@ -163,7 +163,7 @@ private SqlExpression BuildKeyFilter(PersistRequestBuilderContext context, SqlTa
163163
return result;
164164
}
165165

166-
private SqlExpression BuildVersionFilter(PersistRequestBuilderContext context, SqlTableRef filteredTable, List<PersistParameterBinding> currentBindings)
166+
private SqlExpression BuildVersionFilter(in PersistRequestBuilderContext context, SqlTableRef filteredTable, List<PersistParameterBinding> currentBindings)
167167
{
168168
SqlExpression result = null;
169169
foreach (var column in context.Type.GetVersionColumns()) {
@@ -193,7 +193,7 @@ private SqlExpression BuildVersionFilter(PersistRequestBuilderContext context, S
193193
return result;
194194
}
195195

196-
private bool AddFakeVersionColumnUpdate(PersistRequestBuilderContext context, SqlUpdate update, SqlTableRef filteredTable)
196+
private bool AddFakeVersionColumnUpdate(in PersistRequestBuilderContext context, SqlUpdate update, SqlTableRef filteredTable)
197197
{
198198
foreach (var column in context.Type.GetVersionColumns()) {
199199
var columnExpression = filteredTable[column.Name];
@@ -210,7 +210,7 @@ private bool AddFakeVersionColumnUpdate(PersistRequestBuilderContext context, Sq
210210
return false;
211211
}
212212

213-
private PersistParameterBinding GetBinding(PersistRequestBuilderContext context, ColumnInfo column, Table table, int fieldIndex)
213+
private PersistParameterBinding GetBinding(PersistRequestBuilderContext context, ColumnInfo column, Table table, ColNum fieldIndex)
214214
{
215215
if (!context.ParameterBindings.TryGetValue(column, out var binding)) {
216216
var typeMapping = driver.GetTypeMapping(column);
@@ -236,15 +236,10 @@ private ParameterTransmissionType GetTransmissionType(TableColumn column)
236236
: ParameterTransmissionType.Regular;
237237
}
238238

239-
private static int GetFieldIndex(TypeInfo type, ColumnInfo column)
240-
{
241-
if (!type.Fields.TryGetValue(column.Field.Name, out var field)
242-
|| field.Column == null
243-
|| field.Column.ValueType != column.ValueType) {
244-
return -1;
245-
}
246-
return field.MappingInfo.Offset;
247-
}
239+
private static ColNum GetFieldIndex(TypeInfo type, ColumnInfo column) =>
240+
type.Fields.TryGetValue(column.Field.Name, out var field) && field.Column?.ValueType == column.ValueType
241+
? field.MappingInfo.Offset
242+
: (ColNum)(-1);
248243

249244
/// <inheritdoc/>
250245
protected override void Initialize()
@@ -263,4 +258,4 @@ public PersistRequestBuilder()
263258
{
264259
}
265260
}
266-
}
261+
}

Orm/Xtensive.Orm/Orm/Providers/Requests/PersistRequestBuilderContext.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// Created by: Dmitri Maximov
55
// Created: 2008.08.29
66

7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using Xtensive.Collections;
107
using Xtensive.Core;
118
using Xtensive.Orm.Configuration;
129
using Xtensive.Orm.Model;
@@ -16,23 +13,23 @@ namespace Xtensive.Orm.Providers
1613
/// <summary>
1714
/// <see cref="PersistRequestBuilder"/> context.
1815
/// </summary>
19-
public sealed class PersistRequestBuilderContext
16+
public readonly struct PersistRequestBuilderContext
2017
{
21-
public PersistRequestBuilderTask Task { get; private set; }
18+
public PersistRequestBuilderTask Task { get; }
2219

23-
public ModelMapping Mapping { get; private set; }
20+
public ModelMapping Mapping { get; }
2421

25-
public NodeConfiguration NodeConfiguration { get; private set; }
22+
public NodeConfiguration NodeConfiguration { get; }
2623

27-
public TypeInfo Type { get; private set; }
24+
public TypeInfo Type { get; }
2825

29-
public IReadOnlyList<IndexInfo> AffectedIndexes { get; private set;}
26+
public IReadOnlyList<IndexInfo> AffectedIndexes { get; }
3027

31-
public IndexInfo PrimaryIndex { get; private set; }
28+
public IndexInfo PrimaryIndex { get; }
3229

33-
public Dictionary<ColumnInfo, PersistParameterBinding> ParameterBindings { get; private set; }
30+
public Dictionary<ColumnInfo, PersistParameterBinding> ParameterBindings { get; }
3431

35-
public Dictionary<ColumnInfo, PersistParameterBinding> VersionParameterBindings { get; private set; }
32+
public Dictionary<ColumnInfo, PersistParameterBinding> VersionParameterBindings { get; }
3633

3734
// Constructors
3835

@@ -60,4 +57,4 @@ public PersistRequestBuilderContext(PersistRequestBuilderTask task, ModelMapping
6057
VersionParameterBindings = new Dictionary<ColumnInfo, PersistParameterBinding>();
6158
}
6259
}
63-
}
60+
}

Orm/Xtensive.Orm/Orm/Providers/TemporaryTables/TemporaryTableManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, strin
106106

107107
return result;
108108

109-
Lazy<PersistRequest> CreateLazyPersistRequest(int batchSize)
109+
Lazy<PersistRequest> CreateLazyPersistRequest(ushort batchSize)
110110
{
111111
return new Lazy<PersistRequest>(() => {
112112
var bindings = new List<PersistParameterBinding>(batchSize);
@@ -214,16 +214,16 @@ private SqlSelect MakeUpSelectQuery(SqlTableRef temporaryTable, bool hasColumns)
214214
}
215215

216216
private SqlInsert MakeUpInsertQuery(SqlTableRef temporaryTable,
217-
TypeMapping[] typeMappings, List<PersistParameterBinding> storeRequestBindings, bool hasColumns, int rows = 1)
217+
TypeMapping[] typeMappings, List<PersistParameterBinding> storeRequestBindings, bool hasColumns, ushort rows = 1)
218218
{
219219
var insertStatement = SqlDml.Insert(temporaryTable);
220220
if (!hasColumns) {
221221
insertStatement.ValueRows.Add(new Dictionary<SqlColumn, SqlExpression>(1) { { temporaryTable.Columns[0], SqlDml.Literal(0) } });
222222
return insertStatement;
223223
}
224224

225-
for (var rowIndex = 0; rowIndex < rows; ++rowIndex) {
226-
var fieldIndex = 0;
225+
for (ushort rowIndex = 0; rowIndex < rows; ++rowIndex) {
226+
ColNum fieldIndex = 0;
227227
var row = new Dictionary<SqlColumn, SqlExpression>(temporaryTable.Columns.Count);
228228
foreach (var column in temporaryTable.Columns) {
229229
var typeMapping = typeMappings[fieldIndex];
@@ -249,4 +249,4 @@ protected override void Initialize()
249249
backEnd = new EmulatedTemporaryTableBackEnd();
250250
}
251251
}
252-
}
252+
}

Orm/Xtensive.Orm/Orm/Upgrade/Internals/Metadata/MetadataWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private IPersistDescriptor CreateDescriptor(string tableName,
111111
var insert = SqlDml.Insert(tableRef);
112112
var bindings = new PersistParameterBinding[columns.Count];
113113
var row = new Dictionary<SqlColumn, SqlExpression>(columns.Count);
114-
for (int i = 0; i < columns.Count; i++) {
114+
for (ColNum i = 0; i < columns.Count; i++) {
115115
var binding = new PersistParameterBinding(mappings[i], i, transmissionTypes[i]);
116116
row.Add(tableRef.Columns[i], binding.ParameterReference);
117117
bindings[i] = binding;

0 commit comments

Comments
 (0)