Skip to content

Commit 73b5c8d

Browse files
authored
Optimize TruncateToNetDecimal(): use Span (#399)
1 parent b01ab9c commit 73b5c8d

File tree

2 files changed

+6
-30
lines changed

2 files changed

+6
-30
lines changed

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/InternalHelpers.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,19 @@ public static bool ShouldRetryOn(Exception ex)
131131
return ex is TimeoutException;
132132
}
133133

134-
private static UInt128 FromSqlDecimalData(int[] a) =>
135-
new((uint) a[2] | ((ulong) (uint) a[3] << 32), (uint) a[0] | ((ulong) (uint) a[1] << 32));
134+
private static UInt128 FromSqlDecimalData(ReadOnlySpan<uint> a) =>
135+
new(((ulong) a[3] << 32) | a[2], ((ulong) a[1] << 32) | a[0]);
136136

137137
internal static decimal TruncateToNetDecimal(SqlDecimal sqlDecimal)
138138
{
139+
Span<uint> data = stackalloc uint[4];
139140
try {
140141
return sqlDecimal.Value; // throws an OverflowException if the value is out of the decimal range.
141142
}
142143
catch (OverflowException) {
143144
var scale = sqlDecimal.Scale;
144-
var u128 = FromSqlDecimalData(sqlDecimal.Data); // sqlDecimal.Data allocates a new array
145+
sqlDecimal.WriteTdsValue(data);
146+
var u128 = FromSqlDecimalData(data);
145147

146148
for (; scale > 0 && u128 > Max96bitValue; --scale) {
147149
u128 /= Ten;

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Created by: Denis Krjuchkov
55
// Created: 2009.07.02
66

7-
using System;
87
using System.Data;
98
using System.Data.Common;
109
using Microsoft.Data.SqlClient;
@@ -13,7 +12,7 @@
1312

1413
namespace Xtensive.Sql.Drivers.SqlServer.v09
1514
{
16-
internal class TypeMapper : Sql.TypeMapper
15+
internal class TypeMapper(SqlDriver driver) : Sql.TypeMapper(driver)
1716
{
1817
private static readonly SqlValueType
1918
Decimal20Type = new(SqlType.Decimal, 20, 0);
@@ -128,37 +127,12 @@ public override void Initialize()
128127
dateTimeRange = (ValueRange<DateTime>) Driver.ServerInfo.DataTypes.DateTime.ValueRange;
129128
}
130129

131-
private bool TryConvert(SqlDecimal sqlDecimal, out decimal result)
132-
{
133-
var data = sqlDecimal.Data;
134-
if (data[3]==0 && sqlDecimal.Scale <= 28) {
135-
result = new decimal(data[0], data[1], data[2], !sqlDecimal.IsPositive, sqlDecimal.Scale);
136-
return true;
137-
}
138-
result = decimal.Zero;
139-
return false;
140-
}
141-
142-
private SqlDecimal ReducePrecision(SqlDecimal d, int newPrecision)
143-
{
144-
var newScale = newPrecision - d.Precision + d.Scale;
145-
var truncated = SqlDecimal.Truncate(d, newScale);
146-
return SqlDecimal.ConvertToPrecScale(truncated, newPrecision, newScale);
147-
}
148-
149130
private bool ShouldCompareValues(SqlDecimal valueFromDatabase)
150131
{
151132
var floorDigitCount = valueFromDatabase.Precision - valueFromDatabase.Scale;
152133
if (floorDigitCount >= 29) //29 is max count of floor in .net Decimal
153134
return true;
154135
return false;
155136
}
156-
157-
// Constructors
158-
159-
public TypeMapper(SqlDriver driver)
160-
: base(driver)
161-
{
162-
}
163137
}
164138
}

0 commit comments

Comments
 (0)