Skip to content

Conversation

@joelverhagen
Copy link
Member

Addresses #37435

Before

Method BlobSize Mean Error StdDev Op/s Gen0 Gen1 Gen2 Allocated
OversizedByteArray 100000 105.5 us 15.77 us 46.51 us 9,482.18 - - - 196.14 KB
OversizedByteArray 1000000 904.8 us 28.31 us 83.03 us 1,105.28 - - - 1953.95 KB
OversizedByteArray 10000000 9,847.3 us 144.79 us 128.35 us 101.55 1000.0000 1000.0000 1000.0000 19532.38 KB
OversizedByteArray 100000000 90,330.1 us 1,794.14 us 3,664.96 us 11.07 1000.0000 1000.0000 1000.0000 195313.59 KB

After

Method BlobSize Mean Error StdDev Op/s Allocated
OversizedByteArray 100000 107.5 us 14.21 us 41.89 us 9,300.42 98.46 KB
OversizedByteArray 1000000 797.8 us 39.09 us 114.65 us 1,253.52 977.37 KB
OversizedByteArray 10000000 8,094.1 us 158.51 us 148.27 us 123.55 9766.43 KB
OversizedByteArray 100000000 74,611.9 us 1,428.43 us 1,587.69 us 13.40 97657.05 KB

The test reduces the array size by 1 byte so the allocation is roughly half as expected.


  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
    • I have not. Feel free to close the PR if you don't want the fix.
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
    • Benchmarks, existing tests pass
  • Code follows the same patterns and style as existing code in this repo

Summary of changes
- Use read only span for blob parameter binding
- Add benchmark to demonstrate savings
Copilot AI review requested due to automatic review settings January 2, 2026 20:13
@joelverhagen joelverhagen requested a review from a team as a code owner January 2, 2026 20:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes SQLite blob parameter binding by changing the BindBlob method signature from byte[] to ReadOnlySpan<byte>, reducing memory allocations when large byte arrays are truncated. The benchmark results show approximately 50% reduction in memory allocations.

Key Changes

  • Modified BindBlob abstract method to accept ReadOnlySpan<byte> instead of byte[]
  • Updated truncation logic to use Slice instead of Array.Copy, eliminating temporary array allocation
  • Added comprehensive benchmark tests to verify the optimization

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
SqliteValueBinder.cs Changed abstract method signature to use ReadOnlySpan<byte>
SqliteResultBinder.cs Updated implementation to match new signature, added using System
SqliteParameterBinder.cs Replaced Array.Copy with Slice for efficient truncation
EFCore.Sqlite.Benchmarks.csproj Added SQLitePCL package reference for benchmarks
BlobBindingTests.cs New benchmark class to measure allocation improvements

Copy link
Contributor

@cincuranet cincuranet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joelverhagen Great contributions. Thanks. Just some final cleanup and this is ready to go.

@cincuranet cincuranet added this to the 11.0.0 milestone Jan 7, 2026
@cincuranet cincuranet merged commit c66f54a into dotnet:main Jan 7, 2026
7 checks passed
@roji roji removed this from the 11.0.0 milestone Jan 7, 2026
@cincuranet cincuranet linked an issue Jan 7, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce allocations in Microsoft.Data.Sqlite

4 participants