-
Notifications
You must be signed in to change notification settings - Fork 50
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
feat(mpz-common): multi-threaded executor #136
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍, some comments
let task_count = self.queue.len(); | ||
|
||
let mut lanes: Vec<_> = (0..lane_count) | ||
.map(|_| Vec::with_capacity((task_count / lane_count) + (task_count % lane_count))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.map(|_| Vec::with_capacity((task_count / lane_count) + (task_count % lane_count))) | |
.map(|_| Vec::with_capacity((task_count / lane_count) + 1)) |
One more should be enough, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be + (task_count % lane_count != 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gw
|
||
assert_eq!(results_b, vec![0, 1]); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test to make sure things run concurrently
use std::time::{Duration, Instant};
// Need to enable tokio time feature
// Tests that the tasks run concurrently.
#[tokio::test]
async fn test_mt_executor_concurrent() {
let ((mux_a, fut_a), (mux_b, fut_b)) = test_yamux_pair_framed(1024, Bincode);
tokio::spawn(async move {
futures::try_join!(fut_a.into_future(), fut_b.into_future()).unwrap();
});
let mut exec_a = MTExecutor::new(mux_a, 8);
let mut exec_b = MTExecutor::new(mux_b, 8);
let (mut ctx_a, mut ctx_b) =
futures::try_join!(exec_a.new_thread(), exec_b.new_thread()).unwrap();
let mut queue_a = ctx_a.queue().await.unwrap();
let mut queue_b = ctx_b.queue().await.unwrap();
let start = Instant::now();
let timeout = Duration::from_millis(100);
for i in 0..8 {
queue_a.push(move |ctx| {
let i = i;
let timeout = timeout;
Box::pin(async move {
tokio::time::sleep(timeout).await;
ctx.io_mut().send(i as u8).await.unwrap();
})
});
queue_b.push(|ctx| Box::pin(async { ctx.io_mut().expect_next::<u8>().await.unwrap() }));
}
let (_, results_b) = futures::try_join!(queue_a.wait(), queue_b.wait()).unwrap();
let elapsed = Instant::now().duration_since(start);
// The overall latency should be approximately that of a single task.
assert!(elapsed < timeout + Duration::from_millis(50));
assert_eq!(results_b, vec![0, 1, 2, 3, 4, 5, 6, 7]);
}
let task_count = self.queue.len(); | ||
|
||
let mut lanes: Vec<_> = (0..lane_count) | ||
.map(|_| Vec::with_capacity((task_count / lane_count) + (task_count % lane_count))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be + (task_count % lane_count != 0)
I'm going to just remove this queue stuff and stick with the other changes. Will proceed with merge as scope will be a subset. Will open another PR if/when we need this functionality |
Sorry for changing scope after review. I also identified a bug which I've fixed. |
* feat(mpz-common): mt executor * separate context from executor * full mt implementation with queues * remove unused dep * re-enable lints * remove queue, fix bug * bump tlsn-utils * use TestFramedMux * update test
* feat(mpz-common): mt executor * separate context from executor * full mt implementation with queues * remove unused dep * re-enable lints * remove queue, fix bug * bump tlsn-utils * use TestFramedMux * update test
This PR implements a multi-threaded executor which utilizes a multiplexed connection to support forking.
It is currently limited to executing 2 futures concurrently, but we can expand this as needed.EDIT: I've gone all the way and added full forking support which can be accessed via the
queue
method onContext
. This can be used to distribute an arbitrary number of tasks across a configurable number of logical threads.