fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.29k stars 301 forks source link

Slow performance when calling opaque function #2385

Closed Tienisto closed 2 weeks ago

Tienisto commented 2 weeks ago

Is your feature request related to a problem? Please describe. I don't know if it's the nature of SSE or if it's specifically to opaque function calls, but calling opaque functions with binary data is very slow.

Describe the solution you'd like Improve performance of calling opaque functions

Describe alternatives you've considered Maybe use CST (mentioned in https://cjycode.com/flutter_rust_bridge/guides/miscellaneous/codec#comparison), but I could not find an annotation to change that.

Additional context

pub struct Dart2RustStreamSink {
    sender: mpsc::Sender<Vec<u8>>,
}

pub struct Dart2RustStreamReceiver {
    pub(crate) receiver: mpsc::Receiver<Vec<u8>>,
}

pub fn create_stream() -> (Dart2RustStreamSink, Dart2RustStreamReceiver) {
    let (sender, receiver) = mpsc::channel(16 * 1024); // 16KB buffer
    println!("create_stream");
    (
        Dart2RustStreamSink { sender },
        Dart2RustStreamReceiver { receiver },
    )
}

impl Dart2RustStreamSink {
    pub async fn add(&mut self, data: Vec<u8>) -> Result<(), RhttpError> {
        // This method still needs 100ms or more to complete from Dart side, even without calling mpsc

        // self.sender
        //     .send(data)
        //     .await
        //     .map_err(|_| RhttpError::RhttpUnknownError("Failed to send data".to_string()))
        Ok(())
    }

    pub async fn close(&mut self) -> Result<(), RhttpError> {
        self.sender
            .close()
            .await
            .map_err(|_| RhttpError::RhttpUnknownError("Failed to close stream".to_string()))
    }
}
Tienisto commented 2 weeks ago

After some debugging, I found out that the call of an opaque function itself is expensive, regardless of the payload.

Tried to remove the parameter, so no payload is needed. Still it takes very long to call the function.

pub async fn add(&mut self) -> Result<(), RhttpError> {
        // This method still needs 100ms or more to complete from Dart side.
        Ok(())
    }
Tienisto commented 2 weeks ago

My buffer implementation was causing the slowness.

fzyzcjy commented 2 weeks ago

Happy to see it is solved! If there are any further questions just ping me :)

github-actions[bot] commented 5 days ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.