Skip to content

Commit ca2bc9a

Browse files
committed
Performance trick
1 parent 6657223 commit ca2bc9a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/zend/fibers.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ use crate::zend::Function;
77
use std::cell::RefCell;
88
use std::fs::File;
99
use std::future::Future;
10-
use std::io;
10+
use std::io::{self, Write};
1111
use std::os::fd::{RawFd, FromRawFd};
1212
use std::sync::mpsc::{Sender, Receiver, channel};
1313
use std::io::Read;
1414
use lazy_static::lazy_static;
1515
use tokio::runtime::Runtime;
1616
use std::os::fd::AsRawFd;
1717

18+
use super::borrow_unchecked;
19+
1820
lazy_static! {
1921
pub static ref RUNTIME: Runtime = Runtime::new().expect("Could not allocate runtime");
2022
}
@@ -60,23 +62,25 @@ impl EventLoop {
6062
}
6163

6264
pub fn suspend_on<T: Send + 'static, F: Future<Output = T> + Send + 'static>(future: F) -> T {
63-
let future = EVENTLOOP.with_borrow_mut(move |c| {
65+
let (future, get_current_suspension) = EVENTLOOP.with_borrow_mut(move |c| {
6466
let c = c.as_mut().unwrap();
6567
let idx = c.fibers.len() as u64;
6668
c.fibers.insert_at_index(idx, call_user_func!(c.get_current_suspension).unwrap()).unwrap();
6769

6870
let sender = c.sender.clone();
6971
let mut notifier = c.notify_sender.try_clone().unwrap();
7072

71-
RUNTIME.spawn(async move {
73+
(RUNTIME.spawn(async move {
7274
let res = future.await;
7375
sender.send(idx).unwrap();
74-
::std::io::Write::write_all(&mut notifier, &[0]).unwrap();
76+
notifier.write_all(&[0]).unwrap();
7577
res
78+
}), unsafe {
79+
borrow_unchecked(&c.get_current_suspension)
7680
})
7781
});
7882

79-
call_user_func!(Function::from_method("\\Revolt\\EventLoop", "getSuspension")).unwrap().try_call_method("suspend", vec![]).unwrap();
83+
call_user_func!(get_current_suspension).unwrap().try_call_method("suspend", vec![]).unwrap();
8084

8185
return RUNTIME.block_on(future).unwrap();
8286
}

0 commit comments

Comments
 (0)