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

Divide by 256 in _saveConfig() and _loadConfig() #215

Open
user2684 opened this issue Sep 16, 2017 · 1 comment
Open

Divide by 256 in _saveConfig() and _loadConfig() #215

user2684 opened this issue Sep 16, 2017 · 1 comment
Labels
Milestone

Comments

@user2684
Copy link
Contributor

Hi, obviously a lot of work gone into NodeManager thanks, I'm quite new to MySensors but have started working on.a few sensors around my home.

I was just looking through the code (NodeManager.cpp) and an apparent issue jumped out at me (I write C++ for a living on very large boxes but still worry about performance!)
The two functions NodeManager::_saveConfig and NodeManager::_loadConfig are used to save the _sleep_time value to EEPROM. The code splits the long value into 3 parts by dividing by successively 255 . On loading the value is reconstructed by multiplying by 255.
I'm pretty sure that you meant to use 256 to split the long value into three independent bytes to store.
Dividing or multiplying by 255 is potentially expensive whereas dividing or multiplying by 256 is much faster simply requiring shifts.
The normal code for splitting a long value into individual bytes would be something like
uint8_t byte0 = _sleep_time & 0xff; // compiler should generate a simple byte load
uint8_t byte1 = (_sleep_time >> 8 ) & 0xff; // compiler should still generate a simple byte load from the second byte of _sleep_time
uint8_t byte2 = (_sleep_time >> 16) & 0xff; // as before should still be a byte load.
I'm not sure how good the gcc optimiser is for the AVR code but, putting the shift&mask operations directly into the calls of saveSave(...) may avoid having to allocate local variables as in
// save the 3 bits
saveState(EEPROM_SLEEP_SAVED,1);
saveState(EEPROM_SLEEP_1,_sleep_time & 0xff);
saveState(EEPROM_SLEEP_2,(_sleep_time >> 8 ) & 0xff);
saveState(EEPROM_SLEEP_3, (_sleep_time >> 16) & 0xff);

I am not an expert in in the AVR architecture or instruction set so maybe there is a good reason why you are using 255 instead of 256 - it just seems strange!

Regards

@user2684
Copy link
Contributor Author

This would require keeping track of the current version to upgrade the values if already stored, depends on #279 and implementation will be postponed

@user2684 user2684 modified the milestones: v1.7, unassigned Feb 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant