Understanding compiler behavior (Arduino) - Page 1
Forum section: electronics / beginners

author: dagnum
author_rank: Newbie
timestamp: March 15, 2023, 03:22:08 pm
content: I'm trying to optimize compiled program storage for an Arduino sketch. My specific question is in regards to creating a library for the MS5803 family of pressure sensors. I have borrowed code form Luke Miller on github, but my problem with his libraries is that each variant of the sensor has its own library. I want to be able to run two variants connected to the same Arduino device (already handled the static I2C address in his library). (https://github.com/millerlp/MS5803_05). I'd like to have one library that can handle multiple versions of the sensors, which have different maximum pressures, calibration coefficients, and correction formulas. I have modified the library to take in a variant number in the constructor and using that for calculations, example:Code:switch(_Version){case 14:Offset = (int64_t)sensorCoeffs * 65536 + (sensorCoeffs * (int64_t)dT) / 128;Sensitivity = (int64_t)sensorCoeffs * 32768 + (sensorCoeffs * (int64_t)dT) / 256;break;case 5:Offset = (int64_t)sensorCoeffs * 262144 + (sensorCoeffs * (int64_t)dT) / 32;Sensitivity = (int64_t)sensorCoeffs * 131072 + (sensorCoeffs * (int64_t)dT) / 128;break;case 2:Offset = (int64_t)sensorCoeffs * 131072 + (sensorCoeffs * (int64_t)dT) / 64;Sensitivity = (int64_t)sensorCoeffs * 65536 + (sensorCoeffs * (int64_t)dT) / 128;break;}Ok, so this works great to perform different corrections based on the variant argument in each sensor's constructor. There is a lot of repetition here and I'm running short on program memory, so I'm considering substituting some of the hard-coded constants for variables that change depending on the MS5803 variant. Before doing that, I figured I'd check the potential gains. I've performed some tests and am confused by what the compiler is doing. I tried combinations of same/different variants and a simple case where I remove all the switch statements in place of one of the formulas (this provides incorrect results, but I wanted a baseline for my tests).Tests (value is program storage in bytes):1 variant2 variantsswitch2651626518no switch2651626518As you can see, the difference is just 2 bytes when I have two MS5803 declared with 2 variants compared to one. Most confusingly, deleting the case switching has no effect on program storage. My questions:Why is there no difference when using the case structure with 3 formulas vs. no cases and 1 formulaIs there a better structure for handling variants? Both stylistically and for storage optimization

author: homebrew
author_rank: Frequent Contributor
timestamp: March 15, 2023, 04:09:04 pm
content: 1. What are the compiler flags set to? Optimisation level, etc.2. You can always inspect the assembled files and take a look what optimisation the compiler came up with.

author: Nusa
author_rank: Super Contributor
timestamp: March 15, 2023, 04:23:09 pm
content: I suspect you're seeing the results of compiler optimization of the switch statement and any surrounding code. The compiler likely has noticed the similarity between cases and is already doing something similar to what you had in mind. You can, of course, tell the compiler not to optimize and you should see larger differences between versions.To truly understand what's happening, you'd need to parse the assembled output to see how it's done. Likely to be a painful exercise, especially if you don't know anything about reading/understanding assembly code.

author: ledtester
author_rank: Super Contributor
timestamp: March 15, 2023, 05:12:23 pm
content: If you post the two versions of your code I'll see what I figure out.