-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[vm/ffi] Provide memcopy function #43968
Comments
The internal API: sdk/sdk/lib/_internal/vm/lib/ffi_patch.dart Lines 241 to 247 in 706f6b2
A public API should probably have some more guardrails. |
FWIW I would prefer that we simply provide fast path in |
I took a stab at this and modelled the API after the https://dart-review.googlesource.com/c/sdk/+/307040 extension Int64Pointer on Pointer<Int64> {
/// ...
external Int64List asTypedList(
int length, {
@Since('3.1') Pointer<NativeFinalizerFunction>? finalizer,
@Since('3.1') Pointer<Void>? token,
});
/// Copies memory from `this` to [target].
///
/// Copies `8 * length` bytes of memory.
///
/// The memory backing `this` and [target] must not overlap.
///
/// The [length] must not be larger than [target.length].
@Since('3.1')
external memCopyToTypedList(Int64List target, int length);
/// Copies memory from [source] to `this`.
///
/// Copies `8 * length` bytes of memory.
///
/// The memory backing `this` and [source] must not overlap.
///
/// The [length] must not be larger than [source.length].
@Since('3.1')
external memCopyFromTypedList(Int64List source, int length);
/// Copies memory from `this` to [target].
///
/// Copies `8 * length` bytes of memory.
///
/// The memory backing `this` and [target] must not overlap.
@Since('3.1')
external memCopy(Pointer<Int64> target, int length);
} Any feedback on the API is welcome. @mkustermann @lrhn Some design decisions:
|
That is an interesting idea, using |
The only downside of this is that it's not visible from the Dart code whether it's going to be a fast copy or not. E.g. if the call sites are polymorphic, or have mismatched element sizes. How much do we care about predictable performance? @mraleph @mkustermann? If we want predictable performance, and support TypedData copies, we could consider introducing a |
This would greatly accelerate protobuf parsing using https://github.com/protocolbuffers/upb. Profiles currently show ~40% of the overall parsing cost is just copying the data into an FFI buffer. Ideally we could just parse directly out of the |
I agree with @mraleph we should make
Specifically for memory copies of pointers / typed data, I don't see any such concern. We guarantee any |
@sstrickl is going to work on optimizing memory copies (we may not want a separate API though - as this issue suggests - as one can always make views on ffi memory and use existing |
sgtm, I can abandon https://dart-review.googlesource.com/c/sdk/+/307040. @sstrickl you might want to copy the test from that CL (and massage it to use Probably irrelevant but also worth checking: How much overhead does the typed-data object creation add if we're just talking about copying between two pointers? (Or a typed data and a pointer.) final p1 = malloc<Int8>(1024);
final p2 = malloc<Int8>(1024);
p2.asTypedList(1024).setRange(0, 1024, p1.asTypedList(1024)); |
We should try to come to a solution where the |
After adressing #43967 we might also expose a memcopy function in the
dart:ffi
API to provide faster mem copying in and out ofTypedData
that is still GC-safe.The text was updated successfully, but these errors were encountered: