Using nvs for dynamic variables to reduce code size

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Using nvs for dynamic variables to reduce code size

Postby rwel59 » Sat Mar 02, 2019 7:03 pm

I currently have an app with a large number of settings stored in nvs. A decent amount of code is written to retrieve these settings into structures for access throughout the app as well as update settings when required (which is rarely).

I am trying to determine where I might be able to reduce code size and memory consumption. One thought is to directly access these settings in nvs whenever I use them, which would basically be continuously. Would this be an appropriate strategy?

ESP_Sprite
Posts: 9016
Joined: Thu Nov 26, 2015 4:08 am

Re: Using nvs for dynamic variables to reduce code size

Postby ESP_Sprite » Sun Mar 03, 2019 9:01 am

How about having a global struct in RAM reflecting all these settings? On startup, you can retrieve everything from NVS and dump it into the struct. When a value changes, you can change it in the struct then call a routine that writes back all members that have changed. If the amount of changes is very low and you won't need forward compatibility of the settings, you may even write the entire struct to NVS as a blob.

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: Using nvs for dynamic variables to reduce code size

Postby rwel59 » Sun Mar 03, 2019 1:13 pm

That is essentially when I am doing now - reading everything from nvs and putting into global struct. What I am trying to figure out is whether or not I could potentially bypass the struct, reading variables from nvs whenever they are used.

As I said before, this would mean that I am calling nvs everytime time I access a variable which is pretty much constantly. I'm assuming that this is not as efficient from a performance perspective although no clue by how much. Just looking for some insight.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: Using nvs for dynamic variables to reduce code size

Postby ESP_igrr » Sun Mar 03, 2019 2:00 pm

NVS does maintain a cache which helps find specific key in flash without having to scan through all the items stored in all pages. Once the location of the key-value pair is found using the cache, NVS still needs to retrieve the actual value from flash (value is not stored in cache). Cache lookup time will depend on the number of pages in NVS partition and the number of other key-value pairs stored therein. It's pretty hard to give a single estimate. The best would be to time an nvs_get operation you have in your program (using esp_timer_get_time, for example).

Who is online

Users browsing this forum: Google [Bot] and 121 guests