Skip to content

Commit

Permalink
Refactor CommandView::to_owned
Browse files Browse the repository at this point in the history
This patch replaces Vec::from_slice with Vec::extend_from_slice in
CommandView::to_owned.  This reduces the stack usage on lpc55 to one
third (!), from ca. 23 kB to ca. 8 kB with S = 7609.
  • Loading branch information
robin-nitrokey committed Oct 25, 2023
1 parent 8b0d071 commit 33f0401
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,12 @@ impl<'a> CommandView<'a> {
p1,
p2,
le,
data,
data: data_slice,
extended,
} = self;
Ok(Command {
// We use this way to construct the command instead of Data::from_slice as that would
// triple stack usage on the lpc55.
let mut command = Command {
// header
class,
instruction,
Expand All @@ -579,9 +581,14 @@ impl<'a> CommandView<'a> {
// maximum expected response length
le,
// payload
data: Data::from_slice(data).map_err(|_| FromSliceError::TooLong)?,
data: Data::new(),
extended,
})
};
command
.data
.extend_from_slice(data_slice)
.map_err(|_| FromSliceError::TooLong)?;
Ok(command)
}
}

Expand Down

0 comments on commit 33f0401

Please sign in to comment.