Skip to content

Switch to more current version of sqlitepcl#50841

Merged
33 commits merged intodotnet:masterfrom
CyrusNajmabadi:newSqlite
Feb 4, 2021
Merged

Switch to more current version of sqlitepcl#50841
33 commits merged intodotnet:masterfrom
CyrusNajmabadi:newSqlite

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Jan 27, 2021

Fixes #23424

@ghost ghost added the Area-IDE label Jan 27, 2021
namespace Microsoft.CodeAnalysis.SQLite.Interop
{
internal sealed class SafeSqliteBlobHandle : SafeHandle
internal sealed class SafeSqliteBlobHandle : IDisposable
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@sharwell was not sure how to handle converting these over. the main issue was that the sqlite core types (like sqlite3_blob) are now SafeHandles themselves, and do not expose the .ptr value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i thnik we still want these as your leasing pattern effectively means: as long as we create subhandles (i.e. blobs/statements, they should hold the outer sqlite3 instance alive).

public static extern int sqlite3_bind_blob(SafeSqliteStatementHandle stmt, int index, byte[] val, int nSize, IntPtr nTransient);

internal byte[] GetBlobAt(int columnIndex)
=> NativeMethods.sqlite3_column_blob(_rawStatement, columnIndex);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

unused.

=> _connection.ThrowIfNotOk(sqlite3_bind_blob(_rawStatement, parameterIndex, value, length, new IntPtr(-1)));

[DllImport("e_sqlite3.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern int sqlite3_bind_blob(SafeSqliteStatementHandle stmt, int index, byte[] val, int nSize, IntPtr nTransient);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

was able to get rid of this ugly hack.

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review January 27, 2021 23:03
@CyrusNajmabadi CyrusNajmabadi requested review from a team as code owners January 27, 2021 23:03
public void WriteTo(Span<byte> span)
{
var localCopy = _checksum;
Contract.ThrowIfFalse(MemoryMarshal.TryWrite(span, ref localCopy));
Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Jan 28, 2021

Choose a reason for hiding this comment

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

copy needed, because you can't pass a readonly field by-ref.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@sharwell I've redone how checksums are done entirely in the storage layer. Instead of using generalized paths that go through arbitrary reading/writing of streams, the checksum paths are optimized and can do all their work without an intermediary stream abstraction.

@Therzok
Copy link
Copy Markdown
Contributor

Therzok commented Jan 30, 2021

Hey @CyrusNajmabadi, could #23424 be solved as part of this PR?

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Yes, quite possibly. I'll look into that next week.

Co-authored-by: Sam Harwell <sam@tunnelvisionlabs.com>
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@sharwell ?

// can happen when rowId points to a row that hasn't been written to yet.
return null;
}
Contract.ThrowIfFalse(MemoryMarshal.TryRead(bytes, out Checksum.HashData hashData));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❗ Need to use HashData.FromPointer here.

// Avoid a direct dereferencing assignment since sizeof(HashData) may be greater than HashSize.
//
// ex) "https://bugzilla.xamarin.com/show_bug.cgi?id=60298" - LayoutKind.Explicit, Size = 12 ignored with 64bit alignment
// or "https://github.com/dotnet/roslyn/issues/23722" - Checksum throws on Mono 64-bit
return new Checksum(HashData.FromPointer((HashData*)data));

=> _checksum.WriteTo(writer);

public void WriteTo(Span<byte> span)
=> Contract.ThrowIfFalse(MemoryMarshal.TryWrite(span, ref Unsafe.AsRef(in _checksum)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line also suffers from the sizeof(HashData)/HashSize mismatch 😢

{
fixed (byte* bytes = &span.GetPinnableReference())
{
*(HashData*)bytes = _checksum;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📝 This should write each field individually rather than trying to copy the entire structure to a new location.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

tried that, but i get:

Severity	Code	Description	File
Error	CS0191	A readonly field cannot be assigned to (except in a constructor or init-only setter of the type in which the field is defined or a variable initializer)	C:\github\roslyn\src\Workspaces\Core\Portable\Workspace\Solution\Checksum.cs

public void WriteTo(Span<byte> span)
=> Contract.ThrowIfFalse(MemoryMarshal.TryWrite(span, ref Unsafe.AsRef(in _checksum)));
{
unsafe
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❗ Need to validate span.Length >= HashSize

Copy link
Copy Markdown

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Auto-approval

@ghost ghost merged commit 13a3687 into dotnet:master Feb 4, 2021
@ghost ghost added this to the Next milestone Feb 4, 2021
@CyrusNajmabadi CyrusNajmabadi deleted the newSqlite branch February 4, 2021 01:41
@RikkiGibson RikkiGibson modified the milestones: Next, 16.10.P1 Feb 17, 2021
This pull request was closed.
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.

Allocations caused by SQLitePCL.raw database loading

5 participants