erikzenker / hsm

Finite state machine library based on the boost hana meta programming library. It follows the principles of the boost msm and boost sml libraries, but tries to reduce own complex meta programming code to a minimum.
MIT License
186 stars 20 forks source link

Improve Compiletime #88

Open erikzenker opened 4 years ago

erikzenker commented 4 years ago

The last state machine benchmark run showed that runtime is very good and on par with sml and msm. Compiletime is on par with msm but a factor 20 higher than sml. Compiletime should be investigated further and the goal is to get onto the same level as sml.

Benchmark Hsm Sml Msm Statechart
Simple state machine
10.010 s
0.786 s
5.380 s
1.480 s
Complex state machine
77.120 s
3.130 s
26.820 s
5.190 s
erikzenker commented 4 years ago

Since clang 9 you can analyse compile time very detailed with the cxx flag -ftime-trace. This flag generates a json file which can be loaded into the chrome trace analyser chrome://tracing/. In contrast to templight++ it does not only show template instantiations, but also source code include time and backend optimization.

erikzenker commented 4 years ago

Since hsm is header only, the compiler needs some seconds to process all the includes. The sml library provides a preprocessed header file. The script to create this header file can be found here.

ooxi commented 4 years ago

Just as an inside: I'm quite interested in trying out HSM, however the compile time currently is prohibitive.

fwiw, for preprocessing and amalgamation of headers Quom is quite often used, e.g. by CPRE

erikzenker commented 4 years ago

@ooxi thx for considering hsm. Compile time is currently also my biggest pain point. I will definetly try quom which will save us some seconds.

If you have some more tips to reduce compile time heavily you are welcone. I was experimenting with mp11 for the hot path in dispatch table generation, but its way harder to code then using pure hana.

erikzenker commented 4 years ago

@ooxi I added a preprocessed header as `hsm/hsm_gen.h". Using this header reduces the compile time by 1 second.

ooxi commented 4 years ago

@erikzenker that's great news! Just to make sure I understand you correctly, this reduces the compile time by one second or the compile time is reduced to one second?

erikzenker commented 4 years ago

@erikzenker that's great news! Just to make sure I understand you correctly, this reduces the compile time by one second or the compile time is reduced to one second?

Just by one second 😒

erikzenker commented 3 years ago

While adding the latest features to the library the compile time got even worse. I started to write a gist about my investigations: https://gist.github.com/erikzenker/a318fd59cdb4de87db3d659cf425cc1a#file-clang_compile_time_cookbook-md

erikzenker commented 3 years ago

I attached the current compile time trace which can be opened with chrome chrome://tracing. I was able to reduce the class instantiations and pending class instantiations. But I have no idea how I can reduce the compile time for the frontend code gen and the backend.

hsm
erikzenker commented 3 years ago

I was able to reduce the compile time a lot by replacing the remove_duplicates version which uses hana by a mp11 version. The complex example compiles now in ~30 seconds instead of 77 seconds. It is still 10x slower than sml, but I think I could continue this path of replacing core components with mp11 versions. I will update the compile time benchmark result of the readme soon.

erikzenker commented 3 years ago

There is a compile time regression since the last investigations. See state machine benchmark here: https://github.com/erikzenker/state-machine-benchmark/pull/7/checks?check_run_id=2403853253

erikzenker commented 3 years ago

Looks like the regression was introduced with the release v1.4.2

erikzenker commented 3 years ago

It was 838a20e7cf8c9187b75fe3003c4c597cf4856ed6

erikzenker commented 3 years ago

There is another compile time analyzer tool that I stumbled upon: https://github.com/jrmadsen/compile-time-perf

hawkeyese commented 1 year ago

Regarding compile time: Does it matter how you write the actions/guards? Or is it mainly the number of transitions in the transition table that eat compile time?