Skip to content

Commit 9aa2902

Browse files
authored
Merge pull request #245 from servicetitan/upstream/Optimize_FunctionCall
Optimize SqlFunctionCall/SqlCustomFunctionCall - Make abstract class SqlFunctionCallBase with functionality common for both SqlFunctionCall & SqlCustomFunctionCall - Change type of SqlFunctionCall.Arguments to IReadOnlyList to avoid unnecessary allocations. Argument list is now immutable. - Replace Collection<SqlExpression> to List<SqlExpression>. List is more lightweight with same functionality - Make ArgumentValidator.EnsureArgumentIs<T>() to return T to avoid double type casting after call - Make EnsureArgumentIs's parameter parameterName automatic by using [CallerArgumentExpression] attribute - Remove invocation of EnsureArgumentNotNull before EnsureArgumentIs because the later checks for null as well.
2 parents a4e02e6 + 8b22a85 commit 9aa2902

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+139
-256
lines changed

Orm/Xtensive.Orm.Firebird/Sql.Drivers.Firebird/v2_5/Compiler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Xtensive.Sql.Compiler;
1010
using Xtensive.Sql.Ddl;
1111
using Xtensive.Sql.Dml;
12+
using Xtensive.Core;
1213

1314
namespace Xtensive.Sql.Drivers.Firebird.v2_5
1415
{
@@ -171,9 +172,7 @@ public override void Visit(SqlFunctionCall node)
171172
{
172173
switch (node.FunctionType) {
173174
case SqlFunctionType.Concat:
174-
var exprs = new SqlExpression[node.Arguments.Count];
175-
node.Arguments.CopyTo(exprs, 0);
176-
Visit(SqlDml.Concat(exprs));
175+
Visit(SqlDml.Concat(node.Arguments.ToArray(node.Arguments.Count)));
177176
return;
178177
case SqlFunctionType.DateTimeTruncate:
179178
Visit(SqlDml.Cast(node.Arguments[0], new SqlValueType("Date")));

Orm/Xtensive.Orm.MySql/Sql.Drivers.MySql/v5_0/Compiler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Xtensive.Sql.Ddl;
1010
using Xtensive.Sql.Dml;
1111
using Xtensive.Sql.Model;
12+
using Xtensive.Core;
1213

1314
namespace Xtensive.Sql.Drivers.MySql.v5_0
1415
{
@@ -142,9 +143,7 @@ public override void Visit(SqlFunctionCall node)
142143
SqlDml.FunctionCall("TRUNCATE", argument, SqlDml.Literal(0)).AcceptVisitor(this);
143144
return;
144145
case SqlFunctionType.Concat:
145-
var exprs = new SqlExpression[node.Arguments.Count];
146-
node.Arguments.CopyTo(exprs, 0);
147-
Visit(SqlDml.Concat(exprs));
146+
Visit(SqlDml.Concat(node.Arguments.ToArray(node.Arguments.Count)));
148147
return;
149148
case SqlFunctionType.CharLength:
150149
SqlDml.FunctionCall(translator.TranslateToString(SqlFunctionType.CharLength), node.Arguments[0]).AcceptVisitor(this);

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/v09/Compiler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ public override void Visit(SqlFunctionCall node)
179179
return;
180180
case SqlFunctionType.Substring:
181181
if (node.Arguments.Count == 2) {
182-
node = SqlDml.Substring(node.Arguments[0], node.Arguments[1]);
183182
SqlExpression len = SqlDml.CharLength(node.Arguments[0]);
184-
node.Arguments.Add(len);
183+
node = SqlDml.Substring(node.Arguments[0], node.Arguments[1], len);
185184
Visit(node);
186185
return;
187186
}

Orm/Xtensive.Orm/Core/ArgumentValidator.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ public static void EnsureArgumentNotNullOrEmptyOrWhiteSpace(string value, [Invok
9191
/// <param name="value">Value to compare check.</param>
9292
/// <param name="parameterName">Name of the method parameter.</param>
9393
/// <typeparam name="T">The expected type of value.</typeparam>
94+
/// <returns><paramref name="value" /> parameter casted to type <typeparamref name="T" /></returns>
9495
[MethodImpl(MethodImplOptions.AggressiveInlining)]
95-
public static void EnsureArgumentIs<T>(object value, [InvokerParameterName] string parameterName)
96+
public static T EnsureArgumentIs<T>(object value,
97+
[InvokerParameterName, CallerArgumentExpression("value")] string parameterName = null)
9698
{
97-
EnsureArgumentNotNull(value, parameterName);
98-
if (!(value is T)) {
99-
throw new ArgumentException(string.Format(Strings.ExInvalidArgumentType, typeof(T)), parameterName);
99+
if (value is T result) {
100+
return result;
100101
}
102+
EnsureArgumentNotNull(value, parameterName);
103+
throw new ArgumentException(string.Format(Strings.ExInvalidArgumentType, typeof(T)), parameterName);
101104
}
102105

103106
/// <summary>

Orm/Xtensive.Orm/Modelling/Nesting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal Nesting(Node node, string propertyName)
8181

8282
internal Nesting(Node node)
8383
{
84-
ArgumentValidator.EnsureArgumentIs<IModel>(node, "node");
84+
_ = ArgumentValidator.EnsureArgumentIs<IModel>(node);
8585
Node = node;
8686
Initialize();
8787
}

Orm/Xtensive.Orm/Modelling/Node.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ protected virtual void ValidateMove(Node newParent, string newName, int newIndex
433433
}
434434

435435
// Validating parent model
436-
ArgumentValidator.EnsureArgumentNotNull(newParent, nameof(newParent));
437-
ArgumentValidator.EnsureArgumentIs<Node>(newParent, nameof(newParent));
436+
_ = ArgumentValidator.EnsureArgumentIs<Node>(newParent);
438437
var model = Model;
439438
if (model != null) {
440439
var newModel = newParent.Model;

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlAggregate.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public SqlExpression Expression
3333

3434
public override void ReplaceWith(SqlExpression expression)
3535
{
36-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
37-
ArgumentValidator.EnsureArgumentIs<SqlAggregate>(expression, "expression");
38-
var replacingExpression = (SqlAggregate) expression;
36+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlAggregate>(expression);
3937
NodeType = replacingExpression.NodeType;
4038
distinct = replacingExpression.Distinct;
4139
this.expression = replacingExpression.Expression;

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlArray{T}.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ public static implicit operator SqlArray<T>(T[] value)
3737

3838
public override void ReplaceWith(SqlExpression expression)
3939
{
40-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
41-
ArgumentValidator.EnsureArgumentIs<SqlArray<T>>(expression, "expression");
42-
var replacingExpression = (SqlArray<T>) expression;
40+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlArray<T>>(expression);
4341
Values = replacingExpression.Values;
4442
}
4543

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlBetween.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public SqlExpression Expression
4646

4747
public override void ReplaceWith(SqlExpression expression)
4848
{
49-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
50-
ArgumentValidator.EnsureArgumentIs<SqlBetween>(expression, "expression");
51-
SqlBetween replacingExpression = expression as SqlBetween;
49+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlBetween>(expression);
5250
NodeType = replacingExpression.NodeType;
5351
left = replacingExpression.Left;
5452
right = replacingExpression.Right;

Orm/Xtensive.Orm/Sql/Dml/Expressions/SqlBinary.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ public static bool operator false(SqlBinary operand)
8383

8484
public override void ReplaceWith(SqlExpression expression)
8585
{
86-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
87-
ArgumentValidator.EnsureArgumentIs<SqlBinary>(expression, "expression");
88-
var replacingExpression = (SqlBinary) expression;
86+
var replacingExpression = ArgumentValidator.EnsureArgumentIs<SqlBinary>(expression);
8987
NodeType = replacingExpression.NodeType;
9088
Left = replacingExpression.Left;
9189
Right = replacingExpression.Right;

0 commit comments

Comments
 (0)