Skip to content

Improve async string perf and fix reading chars with initial offset. #3377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13133,8 +13133,7 @@ bool writeDataSizeToSnapshot

totalCharsRead = (startOffsetByteCount >> 1);
charsLeft -= totalCharsRead;
offst = totalCharsRead;

offst += totalCharsRead;

while (charsLeft > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13326,7 +13326,7 @@ bool writeDataSizeToSnapshot

totalCharsRead = (startOffsetByteCount >> 1);
charsLeft -= totalCharsRead;
offst = totalCharsRead;
offst += totalCharsRead;


while (charsLeft > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
Expand Down Expand Up @@ -659,6 +660,94 @@ DROP TABLE IF EXISTS [{tableName}]
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task CanGetCharsSequentially()
{
const CommandBehavior commandBehavior = CommandBehavior.SequentialAccess | CommandBehavior.SingleResult;
const int length = 32000;
const string sqlCharWithArg = "SELECT CONVERT(BIGINT, 1) AS [Id], CONVERT(NVARCHAR(MAX), @input) AS [Value];";

using (var sqlConnection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
await sqlConnection.OpenAsync();

StringBuilder inputBuilder = new StringBuilder(length);
Random random = new Random();
for (int i = 0; i < length; i++)
{
inputBuilder.Append((char)random.Next(0x30, 0x5A));
}
string input = inputBuilder.ToString();

using (var sqlCommand = new SqlCommand())
{
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandTimeout = 0;
sqlCommand.CommandText = sqlCharWithArg;
sqlCommand.Parameters.Add(new SqlParameter("@input", SqlDbType.NVarChar, -1) { Value = input });

using (var sqlReader = await sqlCommand.ExecuteReaderAsync(commandBehavior))
{
if (await sqlReader.ReadAsync())
{
long id = sqlReader.GetInt64(0);
if (id != 1)
{
Assert.Fail("Id not 1");
}

var sliced = GetPooledChars(sqlReader, 1, input);
if (!sliced.SequenceEqual(input.ToCharArray()))
{
Assert.Fail("sliced != input");
}
}
}
}
}

static char[] GetPooledChars(SqlDataReader sqlDataReader, int ordinal, string input)
{
var buffer = ArrayPool<char>.Shared.Rent(8192);
int offset = 0;
while (true)
{
int read = (int)sqlDataReader.GetChars(ordinal, offset, buffer, offset, buffer.Length - offset);
if (read == 0)
{
break;
}

ReadOnlySpan<char> fetched = buffer.AsSpan(offset, read);
ReadOnlySpan<char> origin = input.AsSpan(offset, read);

if (!fetched.Equals(origin, StringComparison.Ordinal))
{
Assert.Fail($"chunk (start:{offset}, for:{read}), is not the same as the input");
}

offset += read;

if (buffer.Length - offset < 128)
{
buffer = Resize(buffer);
}
}

var sliced = buffer.AsSpan(0, offset).ToArray();
ArrayPool<char>.Shared.Return(buffer);
return sliced;

static char[] Resize(char[] buffer)
{
var newBuffer = ArrayPool<char>.Shared.Rent(buffer.Length * 2);
Array.Copy(buffer, newBuffer, buffer.Length);
ArrayPool<char>.Shared.Return(buffer);
return newBuffer;
}
}
}

// Synapse: Cannot find data type 'rowversion'.
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))]
public static void CheckLegacyNullRowVersionIsEmptyArray()
Expand Down
Loading