Reduce stack size required for deserialization [Help] #167
Description
We have a method deserialize which is a wrapper over serde_cbor::de::from_slice_with_scratch
method. The code is written for an MCU environment, hence we are using no_std methods. The method is:
pub fn deserialize<'a, T: Deserialize<'a>>(slice: &'a [u8]) -> Result<T, SerdeError> {
info!("stack HWM before serde call: %d", Task::current().unwrap().get_stack_high_water_mark());
let value = serde_cbor::de::from_slice_with_scratch(&slice, &mut []);
let output = match value {
Ok(_e) => Ok(_e),
Err(_e) => Err(SerdeError::DeserializationFailed)
};
info!("stack HWM after serde call: %d", Task::current().unwrap().get_stack_high_water_mark());
return output;
}
We have added log lines to understand the stack requirement. We receive commands over BLE and deserialize the data. As per current architecture, we do 3 level of deserialization.
The following are the logs of 1st command sent over BLE:-
[15:28:59.842] : [elf::utils::serde_module] stack HWM before serde call: 2852
[15:28:59.844] : [elf::utils::serde_module] stack HWM after serde call: 1562
[15:28:59.845] : [elf::utils::serde_module] stack HWM before serde call: 1562
[15:28:59.847] : [elf::utils::serde_module] stack HWM after serde call: 1470
[15:28:59.850] : [elf::utils::serde_module] stack HWM before serde call: 2905
[15:28:59.851] : [elf::utils::serde_module] stack HWM after serde call: 768
The following are the logs of 2nd command sent over same BLE connection:-
[15:29:21.260] : [elf::utils::serde_module] stack HWM before serde call: 1470
[15:29:21.262] : [elf::utils::serde_module] stack HWM after serde call: 1470
[15:29:21.264] : [elf::utils::serde_module] stack HWM before serde call: 1470
[15:29:21.265] : [elf::utils::serde_module] stack HWM after serde call: 1470
[15:29:21.266] : [elf::utils::serde_module] stack HWM before serde call: 768
[15:29:21.268] : [elf::utils::serde_module] stack HWM after serde call: 768
My query: Is there a way to reduce stack consumption?