diff options
author | François Laignel <fengalin@free.fr> | 2020-04-30 19:49:32 +0300 |
---|---|---|
committer | François Laignel <fengalin@free.fr> | 2020-05-25 19:31:49 +0300 |
commit | 5c9bbc6818e5059f0c94b0be7c6c170f53e23bbc (patch) | |
tree | ba426a2e543c041315e750f58dcd604822202d97 /generic | |
parent | 1bea2ad27922896c40f95aaca636a479048757f2 (diff) |
threadshare: Task: reduce memory usage
Using dynamic dispatch instead of monomorphization reduces the lib size
and memory use without affecting CPU nor throughput significantly.
Diffstat (limited to 'generic')
-rw-r--r-- | generic/threadshare/src/runtime/task.rs | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/generic/threadshare/src/runtime/task.rs b/generic/threadshare/src/runtime/task.rs index d4d1ae409..ee2eb0ead 100644 --- a/generic/threadshare/src/runtime/task.rs +++ b/generic/threadshare/src/runtime/task.rs @@ -311,7 +311,7 @@ impl Task { // FIXME allow configuration of the channel buffer size, // this determines the contention on the Task. let (transition_tx, transition_rx) = async_mpsc::channel(4); - let state_machine = StateMachine::new(task_impl, transition_rx); + let state_machine = StateMachine::new(Box::new(task_impl), transition_rx); let (transition, _) = Transition::new(TransitionKind::Prepare); inner.state_machine_handle = Some(inner.state_machine_context.spawn(state_machine.run( Arc::clone(&self.0), @@ -515,9 +515,8 @@ impl Task { } } -#[derive(Debug)] -struct StateMachine<Impl> { - task_impl: Impl, +struct StateMachine { + task_impl: Box<dyn TaskImpl>, transition_rx: async_mpsc::Receiver<Transition>, pending_transition: Option<Transition>, } @@ -546,8 +545,10 @@ macro_rules! spawn_hook { }}; } -impl<Impl: TaskImpl> StateMachine<Impl> { - fn new(task_impl: Impl, transition_rx: async_mpsc::Receiver<Transition>) -> Self { +impl StateMachine { + // Use dynamic dispatch for TaskImpl as it reduces memory usage compared to monomorphization + // without inducing any significant performance penalties. + fn new(task_impl: Box<dyn TaskImpl>, transition_rx: async_mpsc::Receiver<Transition>) -> Self { StateMachine { task_impl, transition_rx, |