commaai / opendbc

a Python API for your car
MIT License
1.92k stars 1.11k forks source link

build: speedup #1180

Open sshane opened 1 month ago

sshane commented 1 month ago

@deanlee interesting in checking out why some objects take so long to build?

time g++ -v -ftime-report -o opendbc/can/dbc.os -c -std=c++1z -DDBC_FILE_PATH='"/home/batman/threepilot/opendbc_repo/opendbc/dbc"' -g -fPIC -O2 -Wunused -Werror -Wshadow -Wno-vla-cxx-extension -Wno-unknown-warning-option -fPIC -I. -I/usr/lib/include -I/home/batman/.pyenv/versions/3.11.4/include/python3.11 opendbc/can/dbc.cc

real    0m10.028s
user    0m7.034s
sys     0m2.953s
Time variable                                   usr           sys          wall               GGC
 phase parsing                      :   0.91 ( 30%)   0.95 ( 52%)   1.86 ( 38%)  158925 kB ( 38%)
 phase lang. deferred               :   0.47 ( 15%)   0.29 ( 16%)   0.76 ( 16%)   82164 kB ( 20%)
 phase opt and generate             :   1.61 ( 52%)   0.57 ( 31%)   2.18 ( 44%)  171455 kB ( 41%)
 |overload resolution               :   0.41 ( 13%)   0.25 ( 14%)   0.59 ( 12%)   57574 kB ( 14%)
 template instantiation             :   0.71 ( 23%)   0.50 ( 28%)   1.25 ( 26%)  129017 kB ( 31%)
deanlee commented 1 month ago

I’ll look into the build performance issue later.

deanlee commented 1 month ago

Due to the inefficiency of std::regex, it often takes over 90% of compile time (e.g., 10 seconds on my laptop). Many discussions online highlight that std::regex is slow both to compile and at runtime. Aside from switching to a more efficient regex library, there are few good solutions.

sshane commented 1 month ago

Why does removing -g speed it up significantly?

sshane commented 1 month ago

Also, this only takes 3s with all the regex:

batman@workstation-shane:~/openpilot/opendbc_repo$ time g++ dbc_test.cc -g -O2

real    0m3.445s
user    0m3.187s
sys     0m0.246s

It appears just adding an empty regex in the real dbc.cc causes a 2x slowdown, and subsequent patterns take almost no extra compile time.

deanlee commented 1 month ago

Why does removing -g speed it up significantly?

std::regex generates a large amount of complex template code, leading to template bloat, which causes the compiler to take more time to process. This is especially true when the -g flag is enabled to generate debug information, as the compiler needs to produce detailed debug data for all the expanded templates.

deanlee commented 1 month ago

RE2 looks like a good alternative to std::regex, it's has minimal headers and relatively small lib compare to other reg lib. https://github.com/google/re2