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

HomeSpan 1.9.0 take more flash than 1.8.0 #785

Open
dzungpv opened this issue Feb 24, 2024 · 15 comments
Open

HomeSpan 1.9.0 take more flash than 1.8.0 #785

dzungpv opened this issue Feb 24, 2024 · 15 comments

Comments

@dzungpv
Copy link

dzungpv commented Feb 24, 2024

I am testing 1.9.0 with one of my project before (20%) free, and with 1.9.0 it only free 6% (0x1e0000 ~2MB app partition).

@dzungpv
Copy link
Author

dzungpv commented Feb 29, 2024

For further check you can try ESP IDF tool: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/performance/size.html

I user idf.py size to check the binary size, but you can use more check. Arduino have the same feature when it finish build. You can check same sample project with Arduino IDE with sample 1.8 and same sample project with 1.9 version.

The project I am using use Arduino as IDF component and build it using ESP IDF, not Arduino, you can check more about it with my PR here: #717, you can also try using my change to make it building with ESP IDF with 1.8 version and same sample project with 1.9 version. I know that you are not familiar with ESP IDF but it is the professional tool. You can still using all your beloved Arduino libs and feature in ESP IDF with more advance feature. Ping me if you want to setup the test project and got any problem.

@dzungpv
Copy link
Author

dzungpv commented Feb 29, 2024

I am not fully read all the code but in version 1.9.0 you are Enable C++ exceptions, this alone add many code to the final binary. A code using exception: https://github.com/HomeSpan/HomeSpan/blob/42e174e3cdbf2c943de5185e8c11d1eea861497b/src/PSRAM.h#L48C65-L48C73

@HomeSpan
Copy link
Owner

Thanks. I tried removing there noexcept from all lines in the code but compile size is the same under the Arduino IDE. I also removed throwing exceptions entirely but still same size. I'm wondering if it is the use of a custom allocator, or perhaps the inclusion of streams. I'll look into it, though please feel free to experiment as well and let me know if you identify a primary driver of the increase that can be eliminated.

@frankonski
Copy link
Contributor

I bet it is c++ Streams. I noticed this when I was trying to buffer Serial to a different output, and also replacing Strings, or std::string to stringbuffers.

@HomeSpan
Copy link
Owner

HomeSpan commented Mar 4, 2024

I have confirmed that the size increase of the flash binary is completely unrelated to the use of the custom allocators (regardless of whether C++ exceptions are used).

As @frankonski correctly surmised, the significant increase in size of the codebase we see in 1.9.0 is solely due to the use of the streams library. Simply adding #include <iostreams> to the top of a HomeSpan 1.8.0 sketch adds about 200K to the binary.

In fact, if you create a basic ESP32 "Hello World" sketch (nothing to do with HomeSpan) and compile both with and without adding #include <iostreams> to the sketch, this causes a huge increase in program size even if you do not make use of anything the stream library.

Since the inclusion of streams in 1.9.0 is a critical innovation that allows for much more scalable use of RAM, I think the increase in size is more than warranted.

For base ESP32 chips that have 4MB of Flash, RAM has always been a more limiting factor than sketch size since there is plenty of Flash if you use 1.9MB OTA partitions (or eliminate OTA altogether).

That said, if anyone knows a way of including whatever subset of iostreams is needed to implement the changes in 1.9.0, that could possibly help reduce the overall footprint a bit.

@frankonski
Copy link
Contributor

The use of stream allows much better RAM usage, at the expense of more flash usage.
This goes beyond my current knowledge, but instead of including sstream, and using std::ostream and std::streambuf, could the use of std::basic_ostream and std::basic_streambuf prove to use less flash?

@HomeSpan
Copy link
Owner

HomeSpan commented Mar 4, 2024

Since I'm not using input streams, I'll see if including ostream instead of iostream shrinks the binary.

@frankonski
Copy link
Contributor

<sstream> is StringStream. I have a feeling if you change <sstream> to including <streambuf> may be even better..

@HomeSpan
Copy link
Owner

HomeSpan commented Mar 5, 2024

I've reviewed the stream classes I used in HomeSpan and think they are already as small as possible. The key is NOT to include <iostream> unless you need cin andcout, which is not needed in HomeSpan. But since I'm only including <sstream>, the compiler does not instantiate cin or cout and only brings in the functions I explicitly use. See here for details.

@dzungpv
Copy link
Author

dzungpv commented Mar 5, 2024

@HomeSpan Because when refactor codes you are not mention in every commit, it is very hard to know what cause the problem, the only way now is analyses the final binary. If I have spare time I will setup two test project with 1.8.0 and 1.9.0 and compare it using ESP IDF tool.

But what I said before, in the ESP IDF by default they are not enable exception handle for++: https://docs.espressif.com/projects/esp-idf/en/release-v4.4/esp32/api-guides/error-handling.html?#c-exceptions, ESP32 Arduino 2.x using ESP IDF 4.4. I do not believe add more code to handle exception will not take any size in the final binary. I must enable the CONFIG_COMPILER_CXX_EXCEPTIONS option to build 1.9.0.
You can try clean the cache, reboot your machine when test it.

@HomeSpan
Copy link
Owner

HomeSpan commented Mar 5, 2024

Note that all of the individual commits can be seen in the release-specific branches. Please see "release-1.9.0" for details of each commit.

Under the Arduino-ESP32 library, there is no change in the size of the code when I added the new malloc code that handles exceptions. You can instead see a significant jump in code size in commit f3e9860 when I first started testing the use of streams.

If I'm understanding you correctly, in order to compile 1.9.0 under the IDF you need to enable CXX_EXCEPTIONS. However, Espressif compiled the Arduino-ESP32 board manager with CXX_EXCEPTIONS already enabled, so there is no change to the code size under the Arduino-ESP32 library when adding exception handling to the code. One of the limitations with using the Arduino-ESP32 library is that you are stuck with the decisions that Espressif has made with regard to all the sdkconfig choices. The only way to change this is to recompile your own version of the Arduino-ESP32 library from the IDF itself (which you may be doing if you are using the IDF).

One thing I noted on the link you indicated above is that Espressif says adding exceptions only increases the size of the file by a few kB, but we are seeing a jump of about 200 kB, which is consistent with adding streams.

Once I have a chance to start working with the IDF directly I will be able to test more thoroughly, but right now all testing is done only with the pre-compiled Arduino-ESP32 library since nearly all HomeSpan users make use of that library from either within the Arduino IDE, Platform I/O, or Visual Studio. Note that even if I use the IDF directly, I will only be able to deploy code that everyone can generically use if I set the sdkconfig parameters to exactly match those that Espressif chooses in their version of the Arduino-ESP32 pre-compiled library. I can of course deploy a custom library but that needs to be target-specific, and I would need to create different versions for different chips and configurations.

With regard to the addition of exceptions to the custom allocators, I pulled that code snippet from the web - it seems to be widely used, and it included exception handling. There is likely no problem if you remove the exception handling from the code if it solves the issue when compiling under the IDF.

One other question --- are you pushing up against the 2MB OTA partition limit? If so, how? A typical HomeSpan sketch is only about 1.4MB, and that includes all the underlying WiFi libraries, Streams, etc. What else is in your program that takes up another 600MB?

Long term my goal is to make HomeSpan independent of the Arduino-ESP32 library, but still fully compatible with it. In other words, I'd like to migrate all the functions to only those that are native to the IDF so HomeSpan can be used either with or without the Arduino-ESP32 library, though as noted above, I need to make sure I don't create any conflicts with the pre-compiled versions. This may be a very long-term project...

@dzungpv
Copy link
Author

dzungpv commented Mar 5, 2024

In my project I use some lib like mqtt, async webserver, sensor driver, heatpump driver so it now 1.85MB. As I understand you want to make your lib chip independant? Mean it can run on Ti, Esp, X86, STM32 ... chip?

But if we are on the ESP32 their Arduino lib is base on ESP IDF and custom Rtos.

I know some lib is chip independant like bacnet-stack but the codebase very complex.

About the ESP IDF I can use their https://github.com/espressif/esp-homekit-sdk with some wraper for Arduino, but it is not easy to use like your lib, but it is small size and very fast.

@dzungpv
Copy link
Author

dzungpv commented Mar 11, 2024

@HomeSpan Today I build my project with some lib disable to test with HomeSpan 1.8.0 and 1.9.0 with command:
idf.py size-components and compare result.

  • RAM usage: 133531 vs 138271, reduce 4740
  • Flash size: 1384947 vs 1685843, increase 300896, it is 300KB
  • With 1.9.0 additional lib is use: libstdc++.a this is the biggest size: 177841, and additional code add to other lib
  • Enable Exception alone add 111KB to my project with 1.8.0, so if you disable exception in release, it will reduce size.

This is detail size for 1.9.0:

Total sizes:
Used stat D/IRAM:  133531 bytes (  54885 remain, 70.9% used)
      .data size:   14668 bytes
      .bss  size:   31320 bytes
      .text size:   86516 bytes
   .vectors size:    1027 bytes
Used Flash size : 1583632 bytes
      .text     : 1231467 bytes
      .rodata   :  351653 bytes
Total image size: 1685843 bytes (.bin may be padded larger)
Per-archive contributions to ELF file:
            Archive File DRAM .data & 0.bss .rtc_noinit IRAM0 .text & 0.vectors ram_st_total Flash .text & .rodata & .rodata_noload & .appdesc flash_total
               libmain.a         57    2975           0           0           0         3032      158685     68612                0          0      227354
             libstdc++.a        152    4328           0           0           0         4480      116660     61013                0          0      177825
               liblwip.a         25    4210           0           0           0         4235      106058     15800                0          0      121883
                  libc.a         80      42           0           0           0          122      111903      9723                0          0      121706
           libnet80211.a        898    6901           0        4742           0        12541       95629      2980            16691          0      120940
          liblibsodium.a         56       8           0           0           0           64       72394     33205                0          0      105655
           libHomeSpan.a       2321    1066           0           0           0         3387       58935     31581                0        256       93093
      libarduino-esp32.a        143    2716           0           0           0         2859       55179     28249                0          0       83571
         libmbedcrypto.a        120      88           0          74           0          282       68954     10315                0          0       79463
                 libpp.a       2424    1361           0       15535           0        19320       33104      1370             3147          0       55580
     libwpa_supplicant.a         12    1436           0           0           0         1448       45461      1303                0          0       46776
            libmbedtls.a         60     320           0           0           0          380       29093      7098                0          0       36251
                libphy.a       2568     605           0        8632           0        11805       19195         0                0          0       30395
               libmdns.a          0    1898           0           0           0         1898       28331       221                0          0       28552
        libPsychicHttp.a          0      20           0           0           0           20       17716      9832                0          0       27548
             libspiffs.a          0      12           0           0           0           12       22770       418                0          0       23188
             libdriver.a        136      79           0        1586           0         1801       20303       729                0          0       22754
           libfreertos.a       1564     988           0       14753         424        17729         157      4242                0          0       21140
          libnvs_flash.a          0      24           0           0           0           24       15709      5397                0          0       21106
             libnghttp.a          0       0           0           0           0            0       15835      3320                0          0       19155
                libhal.a        190       0           0        7473           0         7663        9358       504                0          0       17525
     libesp_hw_support.a        266      35          16        6234           0         6551        9031       745                0          0       16292
          libspi_flash.a       1680     416           0        8889           0        10985        2074       760                0          0       13403
         libesp_system.a        160     169           0        2465           0         2794        6109      3308                0          0       12042
               libheap.a          4       8           0        5028           0         5040        2839      3552                0          0       11423
    libesp_http_server.a          0       0           0           0           0            0        9593      1119                0          0       10712
                libvfs.a        272      60           0           0           0          332        9129       664                0          0       10065
               libmqtt.a          4       0           0           0           0            4        9932        95                0          0       10031
    libesp_http_client.a          0       0           0           0           0            0        7701       901                0          0        8602
           libmbedx509.a          0       0           0           0           0            0        8231       118                0          0        8349
         libesp_common.a          0       0           0           0           0            0          53      7759                0          0        7812
        libesp_ringbuf.a          0       0           0        4503           0         4503           0      2415                0          0        6918
          libesp_netif.a         12      24           0           0           0           36        6222       452                0          0        6686
                libgcc.a          8      12           0         104           0          124        5547       872                0          0        6531
      libtcp_transport.a          0       0           0           0           0            0        5491       257                0          0        5748
            libesp-tls.a          0       4           0           0           0            4        4775       172                0          0        4947
 libbootloader_support.a          0       4           0         634           0          638        3709       398                0          0        4741
           libesp_wifi.a        488      31           0         383           0          902        3500       195                0          0        4566
          libesp_event.a          0       4           0           0           0            4        3515       251                0          0        3766
    libESP32MQTTClient.a          0       0           0           0           0            0        2874       772                0          0        3646
             libnewlib.a        237     440           0        1506           0         2183        1361       340                0          0        3444
              libefuse.a         36       4           0           0           0           40        1530      1528                0          0        3094
          libesp_timer.a         32      28           0        1241           0         1301        1134       627                0          0        3034
         libapp_update.a          1      20           0         194           0          215        1970       284                0        256        2705
  libwifi_provisioning.a          4       8           0           0           0           12        2206       291                0          0        2501
                libsoc.a          0       0           0         278           0          278           0      1740                0          0        2018
            libesp_phy.a         16      13           0         209           0          238        1385       331                0          0        1941
      libesp_https_ota.a          0       0           0           0           0            0        1650        40                0          0        1690
libespressif__esp_encryp          0       0           0           0           0            0        1639        19                0          0        1658
        libsmartconfig.a          2     266           0           0           0          268        1019       520                0          0        1541
            libpthread.a         16      12           0         231           0          259         971       302                0          0        1520
             libespnow.a          0      68           0           0           0           68         258      1192                0          0        1450
                libcxx.a          8      16           0           0           0           24         796       549                0          0        1353
              libAHTxx.a          0       0           0           0           0            0         969       304                0          0        1273
                liblog.a          4     276           0         274           0          554         835       141                0          0        1254
                  libm.a          0       0           0           0           0            0        1024         0                0          0        1024
             libxtensa.a        512       0           0          73           0          585         114        35                0          0         734
             libxt_hal.a          0       0           0         333           0          333           0        32                0          0         365
               libcore.a          0      33           0           0           0           33         226         5               43          0         274
      libtcpip_adapter.a          0      17           0           0           0           17         220         0                0          0         220
            libesp_rom.a          0       0           0         125           0          125           0         0                0          0         125
               libjson.a         12       0           0           0           0           12          77         0                0          0          89
          libUrlEncode.a          0       0           0           0           0            0           0        84                0          0          84
          libprotocomm.a          0       0           0           0           0            0          66         0                0          0          66
                   (exe)          0       0           0           0           3            3           3        12                0          0          18
            libesp_eth.a          4       0           0           0           0            4           0        10                0          0          14
             libesp_pm.a          0       0           0           0           0            0           8         0                0          0           8
            libcoexist.a          0       0           0           0           0            0           0         0                0          0           0
               libmesh.a          0       0           0           0           0            0           0         0                0          0           0
         libprotobuf-c.a          0       0           0           0           0            0           0   

1.8.0 No exception:

Total sizes:
Used stat D/IRAM:  138271 bytes (  50145 remain, 73.4% used)
      .data size:   14516 bytes
      .bss  size:   36400 bytes
      .text size:   86328 bytes
   .vectors size:    1027 bytes
Used Flash size : 1283076 bytes
      .text     : 1036499 bytes
      .rodata   :  246065 bytes
Total image size: 1384947 bytes (.bin may be padded larger)
Per-archive contributions to ELF file:
            Archive File DRAM .data & 0.bss .rtc_noinit IRAM0 .text & 0.vectors ram_st_total Flash .text & .rodata & .rodata_noload & .appdesc flash_total
               libmain.a         57    2975           0           0           0         3032      122690     52268                0          0      175015
               liblwip.a         25    4213           0           0           0         4238      105890     15800                0          0      121715
           libnet80211.a        898    6901           0        4742           0        12541       95603      2988            16691          0      120922
          liblibsodium.a         56       8           0           0           0           64       72394     33205                0          0      105655
                  libc.a         80      42           0           0           0          122       91726      8441                0          0      100247
         libmbedcrypto.a        120      88           0          74           0          282       68143     10315                0          0       78652
           libHomeSpan.a       2320   10542           0           0           0        12862       51088     24316                0        256       77980
      libarduino-esp32.a        143    2716           0           0           0         2859       52402     12657                0          0       65202
                 libpp.a       2424    1361           0       15535           0        19320       33044      1370             3147          0       55520
     libwpa_supplicant.a         12    1436           0           0           0         1448       45321      1303                0          0       46636
            libmbedtls.a         60     320           0           0           0          380       28629      7098                0          0       35787
                libphy.a       2568     605           0        8632           0        11805       19195         0                0          0       30395
               libmdns.a          0    1898           0           0           0         1898       28335       221                0          0       28556
             libspiffs.a          0      12           0           0           0           12       22778       418                0          0       23196
             libdriver.a        136      79           0        1586           0         1801       20303       729                0          0       22754
           libfreertos.a       1564     988           0       14702         424        17678         153      4242                0          0       21085
             libnghttp.a          0       0           0           0           0            0       15827      3320                0          0       19147
                libhal.a        190       0           0        7473           0         7663        9358       504                0          0       17525
     libesp_hw_support.a        266      35          16        6234           0         6551        8959       745                0          0       16220
        libPsychicHttp.a          0      20           0           0           0           20       13847      2369                0          0       16216
             libstdc++.a          8      72           0           0           0           80        4119     11583                0          0       15710
          libnvs_flash.a          0      24           0           0           0           24       14231       480                0          0       14711
          libspi_flash.a       1680     416           0        8889           0        10985        2074       760                0          0       13403
         libesp_system.a        160     129           0        2440           0         2729        6081      3308                0          0       11989
               libheap.a          4       8           0        5028           0         5040        2843      3552                0          0       11427
    libesp_http_server.a          0       0           0           0           0            0        9489      1119                0          0       10608
               libmqtt.a          4       0           0           0           0            4        9980        95                0          0       10079
                libvfs.a        272      60           0           0           0          332        9129       664                0          0       10065
    libesp_http_client.a          0       0           0           0           0            0        7701       901                0          0        8602
           libmbedx509.a          0       0           0           0           0            0        8191       118                0          0        8309
         libesp_common.a          0       0           0           0           0            0          53      7759                0          0        7812
        libesp_ringbuf.a          0       0           0        4503           0         4503           0      2415                0          0        6918
          libesp_netif.a         12      24           0           0           0           36        6190       452                0          0        6654
      libtcp_transport.a          0       0           0           0           0            0        5330       257                0          0        5587
            libesp-tls.a          0       4           0           0           0            4        4628       172                0          0        4800
 libbootloader_support.a          0       4           0         634           0          638        3717       398                0          0        4749
           libesp_wifi.a        488      31           0         383           0          902        3365       195                0          0        4431
          libesp_event.a          0       4           0           0           0            4        3507       251                0          0        3758
             libnewlib.a        237     440           0        1506           0         2183        1361       340                0          0        3444
              libefuse.a         36       4           0           0           0           40        1526      1528                0          0        3090
          libesp_timer.a         32      28           0        1241           0         1301        1134       627                0          0        3034
         libapp_update.a          1      20           0         194           0          215        1970       284                0        256        2705
  libwifi_provisioning.a          4       8           0           0           0           12        2206       291                0          0        2501
    libESP32MQTTClient.a          0       0           0           0           0            0        2182         0                0          0        2182
                libsoc.a          0       0           0         278           0          278           0      1740                0          0        2018
            libesp_phy.a         16      13           0         209           0          238        1317       331                0          0        1873
libespressif__esp_encryp          0       0           0           0           0            0        1691        19                0          0        1710
      libesp_https_ota.a          0       0           0           0           0            0        1566        40                0          0        1606
        libsmartconfig.a          2     266           0           0           0          268        1019       520                0          0        1541
            libpthread.a         16      12           0         231           0          259         880       302                0          0        1429
                liblog.a          4     276           0         274           0          554         835       141                0          0        1254
                libcxx.a          8      16           0           0           0           24         844       389                0          0        1241
                  libm.a          0       0           0           0           0            0        1016         0                0          0        1016
              libAHTxx.a          0       0           0           0           0            0         953         0                0          0         953
             libxtensa.a        512       0           0          73           0          585         114        35                0          0         734
             libxt_hal.a          0       0           0         333           0          333           0        32                0          0         365
                libgcc.a          0       0           0           0           0            0         121       160                0          0         281
               libcore.a          0      33           0           0           0           33         226         5               43          0         274
      libtcpip_adapter.a          0      17           0           0           0           17         220         0                0          0         220
            libesp_rom.a          0       0           0         125           0          125           0         0                0          0         125
               libjson.a         12       0           0           0           0           12          77         0                0          0          89
          libprotocomm.a          0       0           0           0           0            0          66         0                0          0          66
                   (exe)          0       0           0           0           3            3           3        12                0          0          18
            libesp_eth.a          4       0           0           0           0            4           0        10                0          0          14
             libesp_pm.a          0       0           0           0           0            0           8         0                0          0           8
          libUrlEncode.a          0       0           0           0           0            0           0         0                0          0           0
            libcoexist.a          0       0           0           0           0            0           0         0                0          0           0
             libespnow.a          0       0           0           0           0            0           0         0                0          0           0
               libmesh.a          0       0           0           0           0            0           0         0                0          0           0
         libprotobuf-c.a          0       0           0           0           0            0           0         0                0          0           0

1.8.0 with Exception enable:

Total sizes:
Used stat D/IRAM:  138467 bytes (  49949 remain, 73.5% used)
      .data size:   14524 bytes
      .bss  size:   36456 bytes
      .text size:   86460 bytes
   .vectors size:    1027 bytes
Used Flash size : 1394276 bytes
      .text     : 1094351 bytes
      .rodata   :  299413 bytes
Total image size: 1496287 bytes (.bin may be padded larger)
Per-archive contributions to ELF file:
            Archive File DRAM .data & 0.bss .rtc_noinit IRAM0 .text & 0.vectors ram_st_total Flash .text & .rodata & .rodata_noload & .appdesc flash_total
               libmain.a         57    2975           0           0           0         3032      159673     69296                0          0      229026
               liblwip.a         25    4213           0           0           0         4238      105678     15800                0          0      121503
           libnet80211.a        898    6901           0        4742           0        12541       95619      2988            16691          0      120938
          liblibsodium.a         56       8           0           0           0           64       72394     33205                0          0      105655
                  libc.a         80      42           0           0           0          122       91738      8441                0          0      100259
           libHomeSpan.a       2320   10542           0           0           0        12862       56664     30486                0        256       89726
      libarduino-esp32.a        143    2716           0           0           0         2859       55191     28249                0          0       83583
         libmbedcrypto.a        120      88           0          74           0          282       68215     10315                0          0       78724
                 libpp.a       2424    1361           0       15535           0        19320       33040      1370             3147          0       55516
     libwpa_supplicant.a         12    1436           0           0           0         1448       45481      1303                0          0       46796
            libmbedtls.a         60     320           0           0           0          380       28629      7098                0          0       35787
                libphy.a       2568     605           0        8632           0        11805       19195         0                0          0       30395
               libmdns.a          0    1898           0           0           0         1898       28331       221                0          0       28552
        libPsychicHttp.a          0      20           0           0           0           20       17728      9832                0          0       27560
             libspiffs.a          0      12           0           0           0           12       22762       418                0          0       23180
             libdriver.a        136      79           0        1586           0         1801       20303       729                0          0       22754
           libfreertos.a       1564     988           0       14702         424        17678         153      4242                0          0       21085
          libnvs_flash.a          0      24           0           0           0           24       15013      5155                0          0       20168
             libnghttp.a          0       0           0           0           0            0       15823      3320                0          0       19143
             libstdc++.a          8      72           0           0           0           80        5857     11967                0          0       17832
                libhal.a        190       0           0        7473           0         7663        9362       504                0          0       17529
     libesp_hw_support.a        266      35          16        6234           0         6551        8959       745                0          0       16220
          libspi_flash.a       1680     416           0        8889           0        10985        2074       760                0          0       13403
         libesp_system.a        160     169           0        2465           0         2794        6109      3308                0          0       12042
               libheap.a          4       8           0        5028           0         5040        2839      3552                0          0       11423
    libesp_http_server.a          0       0           0           0           0            0        9481      1119                0          0       10600
                libvfs.a        272      60           0           0           0          332        9129       664                0          0       10065
               libmqtt.a          4       0           0           0           0            4        9936        95                0          0       10035
    libesp_http_client.a          0       0           0           0           0            0        7701       901                0          0        8602
           libmbedx509.a          0       0           0           0           0            0        8227       118                0          0        8345
         libesp_common.a          0       0           0           0           0            0          53      7759                0          0        7812
        libesp_ringbuf.a          0       0           0        4503           0         4503           0      2415                0          0        6918
          libesp_netif.a         12      24           0           0           0           36        6218       452                0          0        6682
                libgcc.a          8      12           0         104           0          124        5543       872                0          0        6527
      libtcp_transport.a          0       0           0           0           0            0        5318       257                0          0        5575
            libesp-tls.a          0       4           0           0           0            4        4600       172                0          0        4772
 libbootloader_support.a          0       4           0         634           0          638        3709       398                0          0        4741
           libesp_wifi.a        488      31           0         383           0          902        3476       195                0          0        4542
          libesp_event.a          0       4           0           0           0            4        3507       251                0          0        3758
    libESP32MQTTClient.a          0       0           0           0           0            0        2890       772                0          0        3662
             libnewlib.a        237     440           0        1506           0         2183        1361       340                0          0        3444
              libefuse.a         36       4           0           0           0           40        1526      1528                0          0        3090
          libesp_timer.a         32      28           0        1241           0         1301        1130       627                0          0        3030
         libapp_update.a          1      20           0         194           0          215        1970       284                0        256        2705
  libwifi_provisioning.a          4       8           0           0           0           12        2206       291                0          0        2501
                libsoc.a          0       0           0         278           0          278           0      1740                0          0        2018
            libesp_phy.a         16      13           0         209           0          238        1317       331                0          0        1873
libespressif__esp_encryp          0       0           0           0           0            0        1643        19                0          0        1662
      libesp_https_ota.a          0       0           0           0           0            0        1566        40                0          0        1606
        libsmartconfig.a          2     266           0           0           0          268        1019       520                0          0        1541
            libpthread.a         16      12           0         231           0          259         887       302                0          0        1436
                libcxx.a          8      16           0           0           0           24         796       549                0          0        1353
              libAHTxx.a          0       0           0           0           0            0         969       304                0          0        1273
                liblog.a          4     276           0         274           0          554         835       141                0          0        1254
                  libm.a          0       0           0           0           0            0        1012         0                0          0        1012
             libxtensa.a        512       0           0          73           0          585         114        35                0          0         734
             libxt_hal.a          0       0           0         333           0          333           0        32                0          0         365
               libcore.a          0      33           0           0           0           33         226         5               43          0         274
      libtcpip_adapter.a          0      17           0           0           0           17         220         0                0          0         220
            libesp_rom.a          0       0           0         125           0          125           0         0                0          0         125
               libjson.a         12       0           0           0           0           12          77         0                0          0          89
          libUrlEncode.a          0       0           0           0           0            0           0        84                0          0          84
          libprotocomm.a          0       0           0           0           0            0          66         0                0          0          66
                   (exe)          0       0           0           0           3            3           3        12                0          0          18
            libesp_eth.a          4       0           0           0           0            4           0        10                0          0          14
             libesp_pm.a          0       0           0           0           0            0           8         0                0          0           8
            libcoexist.a          0       0           0           0           0            0           0         0                0          0           0
             libespnow.a          0       0           0           0           0            0           0         0                0          0           0
               libmesh.a          0       0           0           0           0            0           0         0                0          0           0
         libprotobuf-c.a          0       0           0           0           0            0           0         0                0          0           0

There are same problem with the libstdc++ here: https://esp32.com/viewtopic.php?t=3741 seem that PSRAM code will use this lib and the iostream too.

@HomeSpan
Copy link
Owner

Thanks - the link at the end regarding libstdc++ is quite informative. One thing to note is that the compile process under the Arduino IDE is not readily configurable, and Espressif invokes -fexceptions. That means that under the Arudino IDE there is no difference in the size of the binary between using exceptions in a sketch and not using exceptions.

For example, the following code produces a binary of 261,169 bytes (under the Arduino IDE using Arduino-ESP32 2.0.14):

#include <vector>

void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.printf("\nReady\n\n");

 std::vector<int> myvector;

  for (int i=1; i<=5; i++)
    myvector.push_back(i);
  
  for (auto it = myvector.begin() ; it != myvector.end(); ++it)
    Serial.printf("%d ",(*it));
  Serial.printf("\n"); 
}

///////////////////

void loop(){
}

Adding in the the custom allocator code with exceptions produces a binary size of 261,109 bytes (ironically, 60 bytes smaller):

#include <vector>

template <class T>
struct Mallocator {
  typedef T value_type;
  Mallocator() = default;
  template <class U> constexpr Mallocator(const Mallocator<U>&) noexcept {}
  [[nodiscard]] T* allocate(std::size_t n) {
    if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc();
    if(auto p = static_cast<T*>(malloc(n*sizeof(T)))) return p;
    throw std::bad_alloc();
  }
  void deallocate(T* p, std::size_t) noexcept { std::free(p); }
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) { return true; }
template <class T, class U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&) { return false; }

void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.printf("\nReady\n\n");

//  std::vector<int> myvector;
  std::vector<int, Mallocator<int>> myvector;

  for (int i=1; i<=5; i++)
    myvector.push_back(i);
  
  for (auto it = myvector.begin() ; it != myvector.end(); ++it)
    Serial.printf("%d ",(*it));
  Serial.printf("\n"); 
}

///////////////////

void loop(){
}

This result is independent of adding and using <iostream>. Either way it adds another 241K.

With the full IDF you likely have more flexibility and can compile with -fno-exceptions, which should automatically convert the exceptions in code to if/else statements (https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html).

@dzungpv
Copy link
Author

dzungpv commented Mar 12, 2024

Thanks - the link at the end regarding libstdc++ is quite informative. One thing to note is that the compile process under the Arduino IDE is not readily configurable, and Espressif invokes -fexceptions. That means that under the Arudino IDE there is no difference in the size of the binary between using exceptions in a sketch and not using exceptions.

For example, the following code produces a binary of 261,169 bytes (under the Arduino IDE using Arduino-ESP32 2.0.14):

#include <vector>

void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.printf("\nReady\n\n");

 std::vector<int> myvector;

  for (int i=1; i<=5; i++)
    myvector.push_back(i);
  
  for (auto it = myvector.begin() ; it != myvector.end(); ++it)
    Serial.printf("%d ",(*it));
  Serial.printf("\n"); 
}

///////////////////

void loop(){
}

Adding in the the custom allocator code with exceptions produces a binary size of 261,109 bytes (ironically, 60 bytes smaller):

#include <vector>

template <class T>
struct Mallocator {
  typedef T value_type;
  Mallocator() = default;
  template <class U> constexpr Mallocator(const Mallocator<U>&) noexcept {}
  [[nodiscard]] T* allocate(std::size_t n) {
    if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc();
    if(auto p = static_cast<T*>(malloc(n*sizeof(T)))) return p;
    throw std::bad_alloc();
  }
  void deallocate(T* p, std::size_t) noexcept { std::free(p); }
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) { return true; }
template <class T, class U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&) { return false; }

void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.printf("\nReady\n\n");

//  std::vector<int> myvector;
  std::vector<int, Mallocator<int>> myvector;

  for (int i=1; i<=5; i++)
    myvector.push_back(i);
  
  for (auto it = myvector.begin() ; it != myvector.end(); ++it)
    Serial.printf("%d ",(*it));
  Serial.printf("\n"); 
}

///////////////////

void loop(){
}

This result is independent of adding and using <iostream>. Either way it adds another 241K.

With the full IDF you likely have more flexibility and can compile with -fno-exceptions, which should automatically convert the exceptions in code to if/else statements (https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html).

Yes Arduino they enable by default but in version 1.8.0 you are not using Exception, so it it not require.
And they enable all the feature, not care about size. You can see the config here: https://github.com/espressif/arduino-esp32/blob/44da992b774f76777bb2e931dd76cfcf12b9fe70/tools/sdk/esp32/sdkconfig#L275
In 1.9.0 if Exception not enable, could not build the lib.
Arduino hides many api under their IDE, it is easy to use and friendly for newbie. But some feature you could not do with it, like secure boot and flash encryption for ESP32, that why I use Arduino as IDF Component, I can still use Arduino and its libs with ESP IDF, with more control.
If you could rewrite the PSRAM code malloc with out using exception or with derective #ifdef It could save me 111KB

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

3 participants