diff --git a/CHANGELOG.md b/CHANGELOG.md index 34ea1f9..24d3c39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +- Optimize stack usage of `Dispatch::poll` ## [0.1.1] - 2022-08-22 - adjust to `interchange` API change diff --git a/src/dispatch.rs b/src/dispatch.rs index 7a77fdb..951d3da 100644 --- a/src/dispatch.rs +++ b/src/dispatch.rs @@ -106,14 +106,17 @@ impl<'pipe, 'interrupt> Dispatch<'pipe, 'interrupt> { #[inline(never)] pub fn poll(&mut self, apps: &mut [&mut dyn App<'interrupt>]) -> bool { - let maybe_request = self.responder.take_request(); - if let Some((command, message)) = maybe_request { + // We could call take_request directly, but for some reason this doubles stack usage. + let mut message_buffer = Message::new(); + if let Ok((command, message)) = self.responder.request() { // info_now!("cmd: {}", u8::from(command)); // info_now!("cmd: {:?}", command); - if let Some(app) = Self::find_app(command, apps) { + message_buffer.extend_from_slice(message).unwrap(); + + if let Some(app) = Self::find_app(*command, apps) { // match app.call(command, self.responder.response_mut().unwrap()) { - self.call_app(*app, command, &message); + self.call_app(*app, *command, &message_buffer); } else { self.reply_with_error(Error::InvalidCommand); }