cunarist / rinf

Rust for native business logic, Flutter for flexible and beautiful GUI
MIT License
1.82k stars 64 forks source link

Question about Benchmarking and Performance in rinf #399

Closed ganeshrvel closed 1 month ago

ganeshrvel commented 1 month ago

Hi rinf team,

I stumbled on this project while looking for a way to use Rust in Flutter combination. Thanks for this amazing package. Great going.

I have a few questions though:

a. Do you have any existing benchmarks for rinf?

b. If I have hundreds of thousands of struct data (mostly metadata info) coming as a list from Rust to Dart, will there be a memory copy overhead?

  1. If yes, does rinf introduce overhead in copying the vector to and from Rust?
  2. Are there any benchmarks conducted on this aspect?
  3. How much memory overhead can be expected in such scenarios?

c. Since Dart uses message ports to communicate, is there any noticeable delay or overhead observed?

  1. When sending a communication from Flutter to Rust, a message-port signal request is sent to Rust from Flutter. Does this cause a very brief UI freeze?
  2. If yes, to avoid UI freezing for a few milliseconds, do we need to start the call to Rust using an isolate or is it all done under the hood?
  3. If yes, will starting an isolate add noticeable delay in communication due to the overhead of cold starting an isolate in Dart language?

Thanks

temeddix commented 1 month ago

Hello :)

a. Unfortunately, benchmarks are not present. You can consider the Protobuf serialization performance as a potential UI blocking factor.

b.

  1. A RustSignal and DartSignal both consist of a pair of Protobuf message binary and raw binary. You need to consider Protobuf serialization as a separate overhead factor. While copying raw binary from Dart to Rust, memory copy is unavoidable because Rust has an ownership system that cannot coexist with garbage-collected Dart objects.
Operation and Data Protobuf Overhead Memory Copy Overhead Details
Dart >> Rust (Message) O O Deserializes from heap vector slice
Dart >> Rust (Raw Binary) X O Copies the heap vector slice
Rust >> Dart (Message) O O Deserializes from heap vector slice
Rust >> Dart (Raw Binary) X X Zero-copy ownership transfer
  1. Unfortunately, no.
  2. Memory overhead can be expected when you include raw binary in a signal from Dart to Rust. The time spent on this is for writing the data in the heap and then copying it again as Vec<u8>.

c.

  1. If the Protobuf serialization cost is significant (which is unlikely), then yes, it is possible to experience a brief UI freeze.
  2. Other than message serialization, there's no blocking factor; therefore, a Dart isolate is not needed. Dart and Rust (tokio) always run on different sets of threads, and Rust tasks never block the Dart runtime or UI.
  3. So this answer might not be relevant.