Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

I figured out how to hard set compiler optimization. What do you think? #7842

XanCraft21 started this conversation in General
Discussion options

Hello, i wanted to share something i did with the esp32 drivers. I found a very old article on the official arduino forum after a google search telling how to force set compiler optimization on avr boards, can be found in platform .txt. I applied this same method to the esp32 drivers and got it working. Although my code takes slightly more program space, it runs better now. This small hack can be done on any boards package that does not have the settings in the tools menu but can be changed in the text file.

I wanted to ask for other people’s opinions on doing this optimization hack. What do you think? Is it worth sacrificing code space for more speed? Will it be good to use with commercial products?

You must be logged in to vote

Replies: 2 comments · 3 replies

Comment options

Opinion: code space is already at a premium on this platform. I am curious though.

  1. What "runs better" about your code?
  2. Can you share how you changed the optimization level?
You must be logged in to vote
3 replies
@XanCraft21
Comment options

  1. Mostly basic calculations and a whole lot of them at once. The best example i can give right now is a header only graphics renderer on codeberg called Small3DLib by DrummyFish. Codeberg is like github but not as good. This render seems to be extremely efficient.

The optimization hack works better on smaller models or scaled down models where minimal pixels are drawn to a display, which can reach near full display refresh on a small spi display.

This next bit of info is really useless, but just for an example of the display, I used a 1.5 inch 128x128 amoled display with ssd135 driver. Ssd135 is a fake chinese clone of ssd1351 with the same commands but only reaches half the max spi clock speed the chip can handle, which is 20 mhz instead of 40 mhz, but minus 1 hz since arduino inits the code in a strange way, 20 mhz = 19999999, This value is also affected when using DMA capable libraries like TFT_eSPI. Official Ssd1351 displays (commonly 1.27 inch 128x96 since not many clones are made for this size) that use the same generic breakout board are also limited to 20 mhz because of a noisy low efficiency booster on the back, which only shows stability issues if the bus runs efficiently enough near or at 40 mhz. At 20 mhz the display wether it’s fake or real can reach 46 to 53 fps depending on the software and libraries used. Even though with the highest optimization level the 3d renderer mentioned above runs better with smaller or scaled down models and can badly crawl down to 10 fps with a scaled up model. This optimization hack is rather limited truthfully.

  1. This is how i changed the optimization level. I looked up a google search on how to force modify compiler optimization for avr boards and found a very old arduino forum post since someone else asked the same question months before. I went into my app data folder (on windows, it can only be found by app search using “%appdata%”) , then i navigated to the series of folders appdata>local>arduino15>packages>arduino>avr, going to hardware until i find platform .txt. I think the third folder is arduino17 when using the new 2.0 version, but i’m using the legacy version 1.8.19. Using my preferred text editor i opened the file and scrolled down until i found the compiler flags, there it has the optimization flags set to -Os by default. I set it to -O3, which i had to do for 2 to 3 lines.

I did this exact same thing to the esp32 folder under platform .txt.

In general the hack works for any board type that already has a flag there, but the hack is not necessary if there already is an optimization setting in the tools tab of arduino. Note that changing this value in the text file will affect all boards in the package since I believe most packages only have one platform .txt file. I found at least one board on the entire earth that was already set to -O2. I have at least 2 boards that don’t have this flag and i’m not sure if i should add it there or not.

I hope this helped and i hope you and anyone else can make their codes run more efficiently, with slightly extra code space used of course.

EDIT: in the ESP32 folder there is a special part when you scroll down for experimental optimization. It is also the same for the Arduino DUE boards.

@mikkeaa
Comment options

@mrengineer7777 , there's no "premiuim" level for code optimization, there's only 3 different levels:
-speed
-size
-debug
opinions are off course always personal findings...

@GusIsanda
Comment options

Thanks, thanks. Thanks!

Comment options

There is no setting best for all. "-Os" is a good standard choice.
Details explained https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.