SQLite.jl icon indicating copy to clipboard operation
SQLite.jl copied to clipboard

relax bind! Vector{UInt8} -> AbstractVector{UInt8}

Open maxfreu opened this issue 1 year ago • 8 comments

This allows zero-copy writes e.g. for ReinterpretArrays.

Do you want this to be tested explicitly?

maxfreu avatar Apr 03 '24 09:04 maxfreu

Sorry for the slow review on this; I'd like to merge, but can you look at the failing test and see if we should update the test or if we need something else to maintain desired behavior?

quinnj avatar Apr 14 '24 00:04 quinnj

I'd be afraid that this would lose a requirement that the Vector be contiguous in memory (which is I believe true for the underlying SQLite C code).

You might need to add a new method, that specifically takes Base.ReinterpretArray, but only with <: DenseVector.

ScottPJones avatar Apr 14 '24 15:04 ScottPJones

Sooo: I'm trying to understand what happens in the test. Here https://github.com/JuliaDatabases/SQLite.jl/blob/95131f8ebab6af9586a78b1cb4809dba9f2bf9e2/test/runtests.jl#L632 we write a bytearray of type Base.CodeUnits{UInt8, String} to a newly created table in a BLOB column. Before this change, the bytearray was written as a serialized julia object, including type information. So after reading it back in, its type is completely restored, but at the cost of storing it in a julia-only readable format (not as plain bytearray). After this patch, the bytes corresponding to the string "bytearray" are written. After reading them back in, it's just a plain vector. In consequence, the test would have to be changed.

I'd be afraid that this would lose a requirement that the Vector be contiguous in memory (which is I believe true for the underlying SQLite C code).

Ok if that is true, we have to take it seriously. So far it has worked for me. Can you point to a resource for more information?

maxfreu avatar Apr 15 '24 11:04 maxfreu

Ok, I just implemented a non-contiguous AbstractVector and confirmed that it does not work. Implementing Base.iterate and getindex is not enough, you have to specify the conversion to a pointer - and that's just impossible for non-contiguous data.

I'll come up with sth along the lines you suggested.

maxfreu avatar Apr 15 '24 12:04 maxfreu

Can you trigger tests again?

maxfreu avatar Apr 16 '24 14:04 maxfreu

Looks like this is getting closer. There's a failure on the nightly CI though w/ trying to get a pointer to ReinterpretArray. I'm not sure if there's a valid way to do that after the Memory changes in Base julia? It might be worth asking around (public julia slack/zulip/discord) or snooping around the ReinterpretArray code in base to see what's possible.

quinnj avatar Apr 20 '24 13:04 quinnj

I now pass a pointer to the ccall directly, so it doesn't rely on the broken conversion. That seems to work with old and new versions.

maxfreu avatar Apr 21 '24 07:04 maxfreu

...aaand replaced pointer by Ref, because pointer is not safe from GC.

maxfreu avatar Apr 21 '24 08:04 maxfreu

@quinnj ping :)

maxfreu avatar May 21 '24 16:05 maxfreu