Excessive time for simple build

The IDE uses arduino-cli as its back-end. It is the same-same.

cli has a --jobs option: "Max number of parallel compiles. If set to 0 (the default) the number of available CPUs cores will be used." So how many gcc or whatever can you have running at the same. On a quad-core, you can do a --clean compile, and use htop or Task Manager to occasionally see four cores pinned and four items at 90%+ percent each. You can then set that to 1 and see watch the compile times double. Not near the theoretical 4:1, but worth it. But again, this is only for actually compiling.

I looked into that useCachedLibrariesResolution flag I mentioned earlier, and it's related to a hidden --skip-libraries-discovery option: "Skip libraries discovery. This flag is provided only for use in language server and other, very specific, use cases. Do not use for normal compiles". So I of course tried it and while it (as expected) reduced the "Detecting Libraries" time to zero, it looks like all the linking failed. So that will require more investigation.

In the meantime, I figured some basic instrumenting would provide clarity, and obviate manually tapping the stopwatch on my phone. Here's the "one line change" fully cached rebuild

prepare                 0.868
detecting libraries     5.281
function prototypes     0.922
compiling sketch        1.454
compiling libraries     0.223
compiling core          0.225
linking                 4.951
final                   0.616
                     --------
Total                  14.540

"prepare" is everything before "Detecting Libraries" and "final" is everything after the linker, like building the image from the binary and partition table. This is with 10 libraries: 4 from just one #include for the board, and 6 I actually asked for. (Arduino.h is not mentioned)

  • "detecting libraries" is just a chunk of time, even though nothing changed; and reported using "cached library dependencies"
  • "generating function prototypes" is not bad, but not cached
  • all the "compiling" was reported using "previously compiled" or "precompiled" files, except for the .ino.cpp
  • "linking" takes a while too

For comparison, a --clean build

prepare                 0.715
detecting libraries    34.874
function prototypes     0.907
compiling sketch        4.664
compiling libraries    24.436
compiling core          5.326
linking                 5.130
final                   0.640
                     --------
Total                  76.691

No real surprises: linking is the same, compiling takes a while, and what is "Detecting Libraries" actually doing? And for --jobs 1, double the compile time, give or take

prepare                 0.805
detecting libraries    34.479
function prototypes     0.918
compiling sketch        8.272
compiling libraries    53.133
compiling core         11.141
linking                 5.004
final                   0.644
                     --------
Total                 114.395

For a simple(st) sketch, setup and loop with no libraries; clean vs cached

                       clean      cache
prepare                 0.737      0.919
detecting libraries     0.310      0.306
function prototypes     0.380      0.362
compiling sketch        0.368      0.369
compiling libraries     0.001      0.001
compiling core          4.622      0.230
linking                 1.326      1.283
final                   0.403      0.340
                     --------   --------
Total                   8.148      3.810

If you want to try this yourself, clone arduino-cli, checkout the tag for the most recent Release Candidate v0.36.0-rc.2, get it running, and apply the attached git patch.
bench_1.patch.zip (1.5 KB)