Skip to content
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

Reduce memory copy on Dart signal #374

Merged
merged 6 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion documentation/docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ Define an async Rust function that runs forever, sending numbers to Dart every s
```rust title="native/hub/src/sample_functions.rs"
...
use crate::messages;
use std::time::Duration;
...
pub async fn stream_amazing_number() {
use messages::tutorial_resource::*;

let mut current_number: i32 = 1;
loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
MyAmazingNumber { current_number }.send_signal_to_dart(); // GENERATED
current_number += 1;
}
Expand Down
14 changes: 7 additions & 7 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,20 +418,20 @@ use std::sync::OnceLock;
use tokio::sync::mpsc::unbounded_channel;

type SignalHandlers = OnceLock<
HashMap<i32, Box<dyn Fn(Vec<u8>, Vec<u8>)
HashMap<i32, Box<dyn Fn(&[u8], &[u8])
-> Result<(), Box<dyn Error>> + Send + Sync>>,
>;
static SIGNAL_HANDLERS: SignalHandlers = OnceLock::new();

pub fn handle_dart_signal(
message_id: i32,
message_bytes: Vec<u8>,
binary: Vec<u8>
message_bytes: &[u8],
binary: &[u8]
) {
let hash_map = SIGNAL_HANDLERS.get_or_init(|| {
let mut new_hash_map = HashMap::<
i32,
Box<dyn Fn(Vec<u8>, Vec<u8>)
Box<dyn Fn(&[u8], &[u8])
-> Result<(), Box<dyn Error>> + Send + Sync>,
>::new();
''';
Expand All @@ -452,14 +452,14 @@ pub fn handle_dart_signal(
rustReceiveScript += '''
new_hash_map.insert(
${markedMessage.id},
Box::new(|message_bytes: Vec<u8>, binary: Vec<u8>| {
Box::new(|message_bytes: &[u8], binary: &[u8]| {
use super::$modulePath$filename::*;
let message = ${normalizePascal(messageName)}::decode(
message_bytes.as_slice()
message_bytes
)?;
let dart_signal = DartSignal {
message,
binary,
binary: binary.to_vec(),
};
let mut guard = ${snakeName.toUpperCase()}_CHANNEL.lock()?;
if guard.is_none() {
Expand Down
11 changes: 6 additions & 5 deletions flutter_ffi_plugin/example/native/hub/src/sample_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::messages;
use crate::tokio;
use rinf::debug_print;
use std::time::Duration;
use tokio::sync::Mutex;

// Using the `cfg` macro enables conditional statement.
Expand Down Expand Up @@ -57,7 +58,7 @@ pub async fn stream_fractal() {
tokio::spawn(async move {
loop {
// Wait for 40 milliseconds on each frame
tokio::time::sleep(std::time::Duration::from_millis(40)).await;
tokio::time::sleep(Duration::from_millis(40)).await;
if sender.capacity() == 0 {
continue;
}
Expand Down Expand Up @@ -120,7 +121,7 @@ pub async fn run_debug_tests() {
return;
}

tokio::time::sleep(std::time::Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
debug_print!("Starting debug tests.");

// Get the current time.
Expand All @@ -138,15 +139,15 @@ pub async fn run_debug_tests() {

// Test `tokio::join!` for futures.
let join_first = async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
debug_print!("First future finished.");
};
let join_second = async {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
tokio::time::sleep(Duration::from_secs(2)).await;
debug_print!("Second future finished.");
};
let join_third = async {
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
tokio::time::sleep(Duration::from_secs(3)).await;
debug_print!("Third future finished.");
};
tokio::join!(join_first, join_second, join_third);
Expand Down
11 changes: 5 additions & 6 deletions rust_crate/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ macro_rules! write_interface {
binary_pointer: *const u8,
binary_size: usize,
) {
let message_bytes =
unsafe { std::slice::from_raw_parts(message_pointer, message_size).to_vec() };
let binary =
unsafe { std::slice::from_raw_parts(binary_pointer, binary_size).to_vec() };
use std::slice::from_raw_parts;
let message_bytes = unsafe { from_raw_parts(message_pointer, message_size) };
let binary = unsafe { from_raw_parts(binary_pointer, binary_size) };
messages::generated::handle_dart_signal(message_id, message_bytes, binary);
}

#[cfg(target_family = "wasm")]
#[wasm_bindgen::prelude::wasm_bindgen]
pub fn send_dart_signal_extern(message_id: i32, message_bytes: &[u8], binary: &[u8]) {
let message_bytes = message_bytes.to_vec();
let binary = binary.to_vec();
let message_bytes = message_bytes;
let binary = binary;
messages::generated::handle_dart_signal(message_id, message_bytes, binary);
}
};
Expand Down