Skip to content

use Uint8List instead of list<int> #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gcxfd opened this issue May 25, 2021 · 5 comments
Open

use Uint8List instead of list<int> #36

gcxfd opened this issue May 25, 2021 · 5 comments

Comments

@gcxfd
Copy link

gcxfd commented May 25, 2021

see https://medium.com/flutter-community/working-with-bytes-in-dart-6ece83455721

@simolus3
Copy link
Owner

Can you please be more specific on what the issue is? This package uses Uint8List internally all the time, and sqlite blobs are returned as Uint8List as well.

@gcxfd
Copy link
Author

gcxfd commented May 26, 2021

see the image below , i means function return Uint8List instead of list
image

@Leptopoda
Copy link

Hi @simolus3 what is the status here?
Are you open for PRs?

@simolus3
Copy link
Owner

I still don't really see the problem here.

Whenever this package reports data from sqlite3, say because it's reading a BLOB column or because a BLOB value is passed to a user-defined function, it is already exporting the data as Uint8List.
For data that is passed from Dart to sqlite3, we accept List<int>'s in general. As Uint8List implements List<int>, you can already use Uint8Lists to bind blob values to prepared statements. There way we allocate a List<int> as a native pointer is this:

Pointer<Uint8> allocateBytes(List<int> bytes, {int additionalLength = 0}) {
final ptr = allocate.allocate<Uint8>(bytes.length + additionalLength);
ptr.asTypedList(bytes.length + additionalLength)
..setAll(0, bytes)
..fillRange(bytes.length, bytes.length + additionalLength, 0);
return ptr;
}

setAll will check if the argument passed is a Uint8List and does a memcpy in that case, so I expect no overhead of us accepting a general List<int>. The other way around is much more important, and we're consistently using Uint8Lists there. Again, you can also pass Uint8Lists to this package without issue.


In the future, there might be a way in Dart to pass Uint8Lists to C without copying - then there's a benefit of only accepting them. But we can still do an is check to determine whether to share the buffer or whether it needs to be copied.

@Leptopoda
Copy link

Thank you for the fast response.
I was able to pin the issue to a faulty conversion within our code. I must have gotten a bit confused when initially debugging our issue.

Sorry for the noise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@simolus3 @Leptopoda @gcxfd and others