Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
68 changes: 41 additions & 27 deletions DuckDB.NET.Data/DataChunk/Reader/NumericVectorDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,56 +117,72 @@ private unsafe T GetBigInteger<T>(ulong offset)

var isNegative = (buffer[0] & 0x80) == 0;

var bytes = new List<byte>(data->Length - VarIntHeaderSize);
// Allocate byte array once with proper capacity
var byteCount = data->Length - VarIntHeaderSize;
var bytes = new byte[byteCount];
var currentByteCount = byteCount;

for (var index = VarIntHeaderSize; index < buffer.Length; index++)
for (var index = 0; index < byteCount; index++)
{
if (isNegative)
{
bytes.Add((byte)~buffer[index]);
bytes[index] = (byte)~buffer[index + VarIntHeaderSize];
}
else
{
bytes.Add(buffer[index]);
bytes[index] = buffer[index + VarIntHeaderSize];
}
}

var bigIntegerDigits = new Stack<char>();
// Estimate maximum digit count: log10(256^n) = n * log10(256) ≈ n * 2.408
var maxDigits = (int)(byteCount * 2.5) + 2;
var digitChars = new char[maxDigits];
var digitCount = 0;

while (bytes.Count > 0)
{
var quotient = new List<char>();
// Preallocate quotient buffer
var quotient = new byte[byteCount];

while (currentByteCount > 0)
{
byte remainder = 0;
var quotientCount = 0;

foreach (var @byte in bytes)
for (var i = 0; i < currentByteCount; i++)
{
var newValue = remainder * 256 + @byte;
quotient.Add(DigitToChar(newValue / 10));
var newValue = remainder * 256 + bytes[i];
var digit = (byte)(newValue / 10);

// Only add non-zero or if we already have digits
if (digit != 0 || quotientCount > 0)
{
quotient[quotientCount++] = digit;
}

remainder = (byte)(newValue % 10);
}

bigIntegerDigits.Push(DigitToChar(remainder));
digitChars[digitCount++] = DigitToChar(remainder);

// Remove leading zeros from the quotient
bytes.Clear();

foreach (var digit in quotient)
{
if (digit != '0' || bytes.Count > 0)
{
bytes.Add(CharToDigit(digit));
}
}
// Swap buffers to avoid allocation
var temp = bytes;
bytes = quotient;
quotient = temp;
currentByteCount = quotientCount;
}

if (isNegative)
{
bigIntegerDigits.Push('-');
digitChars[digitCount++] = '-';
}

var integer = BigInteger.Parse(new string(bigIntegerDigits.ToArray()));

// Reverse the digits in place
Array.Reverse(digitChars, 0, digitCount);

#if NET6_0_OR_GREATER
var integer = BigInteger.Parse(digitChars.AsSpan(0, digitCount));
#else
var integer = BigInteger.Parse(new string(digitChars, 0, digitCount));
#endif

try
{
Expand All @@ -178,8 +194,6 @@ private unsafe T GetBigInteger<T>(ulong offset)
}

char DigitToChar(int c) => (char)(c + '0');

byte CharToDigit(char digit) => (byte)(digit-'0');
}

private T GetBigInteger<T>(ulong offset, bool unsigned)
Expand Down
10 changes: 6 additions & 4 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ private bool InitNextReader()

private bool InitChunkData()
{
foreach (var reader in vectorReaders)
// Dispose existing readers using for loop (faster than foreach)
for (int i = 0; i < vectorReaders.Length; i++)
{
reader.Dispose();
vectorReaders[i]?.Dispose();
}

currentChunk?.Dispose();
Expand Down Expand Up @@ -343,9 +344,10 @@ public override void Close()
{
if (closed) return;

foreach (var reader in vectorReaders)
// Dispose readers using for loop (faster than foreach)
for (int i = 0; i < vectorReaders.Length; i++)
{
reader.Dispose();
vectorReaders[i]?.Dispose();
}

currentChunk?.Dispose();
Expand Down
13 changes: 12 additions & 1 deletion DuckDB.NET.Data/PreparedStatement/PreparedStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ private static void BindParameters(DuckDBPreparedStatement preparedStatement, Du
throw new InvalidOperationException($"Invalid number of parameters. Expected {expectedParameters}, got {parameterCollection.Count}");
}

if (parameterCollection.OfType<DuckDBParameter>().Any(p => !string.IsNullOrEmpty(p.ParameterName)))
// Check if any parameter has a name (faster than LINQ)
var hasNamedParameters = false;
for (var i = 0; i < parameterCollection.Count; i++)
{
if (!string.IsNullOrEmpty(parameterCollection[i].ParameterName))
{
hasNamedParameters = true;
break;
}
}

if (hasNamedParameters)
{
foreach (DuckDBParameter param in parameterCollection)
{
Expand Down