Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store all message descriptor parts in program memory on AVR to save SRAM #947

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

simon-wh
Copy link

@simon-wh simon-wh commented Mar 12, 2024

We were having some issues with our builds targeting AVR based MCUs as we only have 8KB SRAM available and the *_msg and *_submsg_info constants were filling up our SRAM a decent amount. So I did some digging and expanded the PROGMEM support in nanopb to extend to the *_msg and *_submsg_info parts instead of just *_field_info.

This has resulted in very significant savings to our builds. Averaging a 11% reduction in our total static SRAM allocations.

In my testing, this appears to work correctly in our usage of nanopb. However, we do not make any use of extension fields and the code for that is a bit trickier. I did some digging and I hope that it should also be working normally as well.

This is not necessarily the limit to savings that can be made. When working on this I noticed that due to the {msg}_DEFAULT defines being strings, they also end up in SRAM allocations on AVR, although they are a bit more subtle as anonymous strings don't end up with symbols, so you have to inspect deeper to see them in an objdump. I wanted to implement the optimisation for those as well, but it was a bit trickier to get going and the savings we would get were far smaller overall.

simon-wh and others added 5 commits March 7, 2024 12:27
TODO:
- Check extension fields and callbacks are valid
- Try and get the _DEFAULT values (which end up as strings) into PROGMEM as well
@simon-wh simon-wh changed the title Store all field descriptor parts in program memory on AVR to save SRAM Store all message descriptor parts in program memory on AVR to save SRAM Mar 12, 2024
@PetteriAimonen
Copy link
Member

Hmm, I would really want to keep pb_encode.c and pb_decode.c free of this AVR PROGMEM stuff. It is error prone and hampers reading the code.

@simon-wh
Copy link
Author

I can understand that, the AVR PROGMEM stuff is overall quite tricky to understand and definitely hurts the readability. It also makes maintenance a bit trickier as you potentially have to take it into account when making changes.

It's somewhat up to you at the end of the day, I'm not opposed to us just using our own patches on top for our builds if you don't want to take this into the mainline code

@PetteriAimonen
Copy link
Member

Yeah, I'll have to think about it. It would be great if there was a way to contain it to pb_common.c, but I'm not sure if that is possible.

The code itself seems functional, scons PLATFORM=AVR fails for C++ but otherwise passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants