-
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
Dart_PostCObject_DL has a noticeable latency #53156
Comments
I'd expect this to be much faster (it only has to allocate & initialize @MegatronKing How do you measure (before/after a call to |
I can not give you a demo project, but I can try to explain my code. I called dart from a C++ thead, like this: auto start = CurrentTimeMillis();
// Post data to dart with Dart_PostCObject_DL
CallDart();
// Waiting the reply from dart
std::unique_lock<std::mutex> lock(mLock);
if (mCondition.wait_for(lock, std::chrono::seconds(10)) == std::cv_status::timeout) {
// Timeout
return 1;
}
// Received the reply from dart
auto end = CurrentTimeMillis();
// Print the cost time
std::cout << "Bridge " << end - start << std::endl; And receive the message in dart, like this: final ReceivePort nativePort = ReceivePort();
nativePort.listen((message) {
// Receive messages from C++
// Do nothing here, and send a reply message to C++ immediately.
// Call the C api with ffi method here
}); Receive dart reply messages in C++, like this: // Notify to continue
std::unique_lock<std::mutex> lock(mLock);
mCondition.notify_one(); When the above steps are called frequently, I get the following logs: While most of the time-consuming is stable, but still could find some noticeable latency:
|
Thanks, that clears things up a little: So what you measure is right before The timings you see are as follows:
What we see here is that the vast majority of messages are handled with rather low latency (90% <= 2ms). So this issue is about the tail latencies (very infrequently the messages get delayed by double-digit ms), for which there can be multiple reasons:
@MegatronKing I think the best for you to find out where these tail latencies are coming from would be to obtain a timeline for your app. Our DevTools has a timeline viewer built-in, see docs.flutter.dev/tools/devtools. You can emit custom events that will be visible in the timeline via APIs from |
Thanks for the reply, your explanation is make sense, I'll take a look in profile mode later. By the way, I need to call dart in c++ and get the execution result. Is there any other more stable and efficient way, even if the queue is busy. |
Calling into Dart from 3rd party threads and getting a result is currently not directly supported. We have been discussing whether we want to offer this or not (see #37022). For now we support making a 3rd party C thread be able to call into dart asynchronously without getting a result via Depending on your use case, it may be possible to have other solutions. For example you can spawn an isolate and let your C code run on that thread, it can then use our synchronous callbacks into Dart on that thread. |
Without additional information we're not able to resolve this issue. Feel free to add more info or respond to any questions above and we can reopen the case. Thanks for your contribution! |
In my flutter application, I frequently call dart code in a c++ thread. When the UI refreshes frequently, I observed that
Dart_PostCObject_DL
has a latency of more than 20ms, even 100ms. But I didn't send very big data, only 4 integer values.The text was updated successfully, but these errors were encountered: