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

Feature Request / Bug On Some Platforms: Reduce memory footprint by wrapping all C strings in F(...) #106

Closed
EmperorArthur opened this issue Mar 3, 2021 · 5 comments

Comments

@EmperorArthur
Copy link

Hello,

I was recently evaluating your library to replace our custom Arduino HTTP Server implementation. Of all the libraries examined so far, yours is definitely the most polished, and certainly has better routing capabilities than the nested if statements ours uses. Please do not take my suggestions as thinking your library is "bad."

In the process of evaluating your library, I noticed that it uses 1398 bytes of RAM. I believe this is because, despite importing "Arduino.h", the library uses raw C strings for everything. Those strings are always stored in RAM, just sitting there doing nothing but taking up space.

This limits what devices the library can be run on, and/or the amount of data processing that is available when using this library.

Suggested Fix

#include <WString.h> and then use F(<HttpStatus_reasonPhrase>). An alternate approach for some code is to use this repository, with sleight modifications.

@EmperorArthur
Copy link
Author

If you are interested, I should be able to submit a pull request to resolve this issue.

@lasselukkari
Copy link
Owner

lasselukkari commented Mar 4, 2021

Hi.

Most strings that are sent to to client are already using the progmem with the P macro. I have been trying to avoid the dependency to String class. Maybe there is no good reason for that.

Feel free to make a pull request. I will be interesting to see how much additional memory this will save. I will most likely also need to update the CI scripts to support this.

@EmperorArthur
Copy link
Author

Thanks for the response. I used this tool to examine what is being stored in RAM, and took a deep dive in the code.

It looks like most of the usage I was seeing was because of the 128 and 1024 buffers. You may want to document that somewhere. Especially since some of us (me mostly) are used to using things like ArduinoStreamUtils.

However, I believe I can save at least 50 bytes of RAM just by wrapping the C strings in m_processMethod with:

#define FC(string_literal) String(F(string_literal)).c_str()
FC("some string")

I am continuing to investigate, and will send a PR soon.

@EmperorArthur
Copy link
Author

EmperorArthur commented Mar 4, 2021

That macro approach didn't work out as well as I'd like, however standard Arduino F strings were the solution.

PR #109 saves 98 bytes worth of RAM, and actually seems to decrease flash usage by 54 bytes as well. I assume the optimizer is able to remove or combine more code.

Edit:
It's important to note that the Response.set(...) function does not like F() strings. That can cause static RAM bloat from normal user code and is costing a few bytes here. However, that should probably be another issue/PR for after this one is resolved.

@lasselukkari
Copy link
Owner

PR merged.

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

No branches or pull requests

2 participants