Skip to content

Reduce allocations in Microsoft.Data.Sqlite #37435

@joelverhagen

Description

@joelverhagen

What problem are you trying to solve?

Currently, the SqliteParameterBinder allocates memory unnecessarily. This is likely when using an array pool because the pooled arrays typically have a size larger than what is requests (rounded up to the power of 2).

var blob = value;
if (ShouldTruncate(value.Length))
{
blob = new byte[size!.Value];
Array.Copy(value, blob, size.Value);
}
var rc = sqlite3_bind_blob(stmt, index, blob);
SqliteException.ThrowExceptionForRC(rc, handle);

The underlying sqlite3_bind_blob accepts a ReadOnlySpan<byte>. I believe we can change the blob binding to take a ReadOnlySpan<byte> without any breaking changes (internal methods).

This would avoid the allocation.

Describe the solution you'd like

Don't copy byte arrays unless you really have to.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions