Skip to content

Commit f256061

Browse files
committed
Changed float to int byte conversion to always use stackalloc
1 parent 002fe16 commit f256061

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/FirebirdSql.Data.FirebirdClient/Client/Managed/XdrReaderWriter.cs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,48 +1026,42 @@ public ValueTask WriteAsync(long value, CancellationToken cancellationToken = de
10261026
return ReturnAfter(task, rented);
10271027
}
10281028

1029-
public void Write(float value)
1029+
static int Float2Int(float value)
10301030
{
1031-
Span<byte> buffer = stackalloc byte[4];
1032-
if (!BitConverter.TryWriteBytes(buffer, value))
1031+
Span<byte> bytes = stackalloc byte[4];
1032+
if (!BitConverter.TryWriteBytes(bytes, value))
10331033
{
10341034
throw new InvalidOperationException("Failed to write Single bytes.");
10351035
}
1036-
Write(BitConverter.ToInt32(buffer));
1036+
return BitConverter.ToInt32(bytes);
1037+
}
1038+
1039+
public void Write(float value)
1040+
{
1041+
Write(Float2Int(value));
10371042
}
10381043
public ValueTask WriteAsync(float value, CancellationToken cancellationToken = default)
10391044
{
1040-
var rented = ArrayPool<byte>.Shared.Rent(4);
1041-
if (!BitConverter.TryWriteBytes(rented, value))
1042-
{
1043-
ArrayPool<byte>.Shared.Return(rented);
1044-
throw new InvalidOperationException("Failed to write Single bytes.");
1045-
}
1046-
var intVal = BitConverter.ToInt32(rented, 0);
1047-
ArrayPool<byte>.Shared.Return(rented);
1048-
return WriteAsync(intVal, cancellationToken);
1045+
return WriteAsync(Float2Int(value), cancellationToken);
10491046
}
10501047

1051-
public void Write(double value)
1048+
static long Double2Long(double value)
10521049
{
10531050
Span<byte> buffer = stackalloc byte[8];
10541051
if (!BitConverter.TryWriteBytes(buffer, value))
10551052
{
10561053
throw new InvalidOperationException("Failed to write Double bytes.");
10571054
}
1058-
Write(BitConverter.ToInt64(buffer));
1055+
return BitConverter.ToInt64(buffer);
1056+
}
1057+
1058+
public void Write(double value)
1059+
{
1060+
Write(Double2Long(value));
10591061
}
10601062
public ValueTask WriteAsync(double value, CancellationToken cancellationToken = default)
10611063
{
1062-
var rented = ArrayPool<byte>.Shared.Rent(8);
1063-
if (!BitConverter.TryWriteBytes(rented, value))
1064-
{
1065-
ArrayPool<byte>.Shared.Return(rented);
1066-
throw new InvalidOperationException("Failed to write Double bytes.");
1067-
}
1068-
var longVal = BitConverter.ToInt64(rented, 0);
1069-
ArrayPool<byte>.Shared.Return(rented);
1070-
return WriteAsync(longVal, cancellationToken);
1064+
return WriteAsync(Double2Long(value), cancellationToken);
10711065
}
10721066

10731067
public void Write(decimal value, int type, int scale)

0 commit comments

Comments
 (0)