Skip to content
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
63 changes: 63 additions & 0 deletions benchmark/EFCore.Sqlite.Benchmarks/Binding/BlobBindingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.Data.Sqlite;

// ReSharper disable ReturnValueOfPureMethodIsNotUsed

namespace Microsoft.EntityFrameworkCore.Benchmarks.Binding;

[DisplayName(nameof(BlobBindingTests))]
[MemoryDiagnoser]
public class BlobBindingTests
{
private SqliteConnection _connection;

[Params(100_000, 1_000_000, 10_000_000, 100_000_000)]
public int BlobSize { get; set; }

[GlobalSetup]
public void OpenConnection()
{
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();
using var command = _connection.CreateCommand();
command.CommandText = "CREATE TABLE Files (FileId INTEGER PRIMARY KEY, Data BLOB NOT NULL)";
command.ExecuteNonQuery();
}

[GlobalCleanup]
public void CloseConnection()
{
_connection.Dispose();
}

[IterationSetup]
public void ClearTable()
{
using var command = _connection.CreateCommand();
command.CommandText = "DELETE FROM Files";
command.ExecuteNonQuery();
}

[Benchmark]
public async Task OversizedByteArray()
{
using var command = _connection.CreateCommand();
command.CommandText = "INSERT INTO Files (Data) VALUES (@Data)";

var buffer = new byte[BlobSize + 1]; // add one to ensure the array size is different from the parameter size

var parameter = command.CreateParameter();
parameter.ParameterName = "@Data";
parameter.Size = BlobSize;
parameter.Value = buffer;
command.Parameters.Add(parameter);

await command.ExecuteNonQueryAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<ProjectReference Include="..\..\src\EFCore.Sqlite.Core\EFCore.Sqlite.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" />
</ItemGroup>

<ItemGroup>
<None Update="AdventureWorks2014.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Data.Sqlite.Core/SqliteParameterBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace Microsoft.Data.Sqlite;
internal class SqliteParameterBinder(sqlite3_stmt stmt, sqlite3 handle, int index, object value, int? size, SqliteType? sqliteType)
: SqliteValueBinder(value, sqliteType)
{
protected override void BindBlob(byte[] value)
protected override void BindBlob(ReadOnlySpan<byte> value)
{
var blob = value;
if (ShouldTruncate(value.Length))
{
blob = new byte[size!.Value];
Array.Copy(value, blob, size.Value);
blob = value.Slice(0, size!.Value);
}

var rc = sqlite3_bind_blob(stmt, index, blob);

SqliteException.ThrowExceptionForRC(rc, handle);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.Data.Sqlite.Core/SqliteResultBinder.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using SQLitePCL;
using static SQLitePCL.raw;

namespace Microsoft.Data.Sqlite;

internal class SqliteResultBinder(sqlite3_context ctx, object? value) : SqliteValueBinder(value)
{
protected override void BindBlob(byte[] value)
protected override void BindBlob(ReadOnlySpan<byte> value)
=> sqlite3_result_blob(ctx, value);

protected override void BindDoubleCore(double value)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Data.Sqlite.Core/SqliteValueBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected virtual void BindDouble(double value)

protected abstract void BindText(string value);

protected abstract void BindBlob(byte[] value);
protected abstract void BindBlob(ReadOnlySpan<byte> value);

protected abstract void BindNull();

Expand Down
Loading