-
Notifications
You must be signed in to change notification settings - Fork 99
Reduce stack size required for deserialization [Help] #167
Comments
We are working with nRF52840 SoC and freeRTOS as our OS. Pasting the high water mark defination here: |
@pyfisch |
I don't think so if you use serde-cbor. Serde uses recursion to do deserialization and therefore every nested structure adds at least one more stack frame to the stack. As CBOR is really simple you can write your own serializer/deserializer and achieve lower resource usage. In this case I would avoid recursion and store intermediate results on the heap.
No, I don't collect such data for microcontrollers. @wildarch added no-std support to this crate, maybe he can give you better advice on how to use serde on microcontrollers. |
Serde has a little known (and slightly hidden) feature where it can deserialize straight into the target struct. For this you can use Hope this helps 😄 |
@wildarch Thanks for quick response. I really appreciate you helping me out. I have few more questions:
|
I used the existing code from serde_cbor to test this feature but I didn't observe any difference in stack consumption. My cargo dependencies: pub fn deserialize_in_place<'a, T: Deserialize<'a>>(slice: &'a [u8], mut place: T) -> Result<T, Error> |
Unfortunately the code I wrote using this is proprietary and I've since left the company, so I'll have to answer this off the top of my head 😅.
|
@PraneshAnubhav this might be a dumb question -- In docs for
https://docs.rs/serde_cbor/0.10.2/serde_cbor/de/fn.from_slice_with_scratch.html Yet, you are passing as scratch an empty slice
Doesn't that seem like it would be harmful, since you are giving no scratch at all for the impl to use? Did you try either making a heap allocation for the scratch space, or using some global mutable memory perhaps thread local or with a lock if you cannot use the heap at all? I imagine that providing scratch space may reduce stack pressure. |
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: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?
The text was updated successfully, but these errors were encountered: