Woolfrey / software_robot_library

Custom classes for robot control.
GNU General Public License v3.0
2 stars 1 forks source link

Remove Template classes and reduce compile time #130

Closed Woolfrey closed 1 month ago

Woolfrey commented 1 month ago

Plan

Remove all template classes from RobotLibrary.

Compilation times for other projects using RobotLibrary were observed to take up to 19 seconds to compile. This is due to every class being defined as a template, meaning source code is not pre-compiled to save time:

24.07.11_Template_Build_Time.webm

[A post-hoc note: I didn't have a proper plan set out, so I took a lot of detours, and ran in to a lot of problems trying to make everything work!]

Do:

Observation Thoughts, insights
I experienced a lot of linker errors. Had to remove the inline keyword scattered throughout header and source files. I did this thinking it might speed up code at runtime, but it was not applied consistently, nor tested.
Changing these lines in the root/CMakeLists.txt file: target_link_libraries(${PROJECT_NAME} INTERFACE Control Math Model Trajectory) .. install(TARGETS ${PROJECT_NAME} Control Math Model Trajectory and this in the sublibrary CMakeLists.txt files: install(TARGETS Model EXPORT ModelTargets affects how the library is linked in other projects (i.e. RobotLibrary::RobotLibrary vs RobotLibrary::Model). I was trying to make all the CMakeLists.txt files consistent, but the root CMake has to be carefully changed accordingly.
Using pre-compiled headers hid a lot of problems. I did this thinking it might improve compile times.
The install location meant that other projects could not find the header files. E.g. usr/local/RobotLibrary/Control caused the compiler to fail. Using usr/local/RobotLibrary/ for all sublibraries worked. Sublibraries works fine for RobotLibrary itself, but it causes problems on the user end.
ChatGPT was able to write CMake files quickly. I had to start with simple requests and build up complexity, otherwise there were numerous issues. It's better to use ChatGPT early for writing code while the project is small and simple.

Study

The compile time for RobotLibrary itself is increased:

robot_library_compile_time_before.webm

robot_library_compile_time_after.webm

but on the user-side it is much faster:

compile_time_after.webm

This is better on the user side, since RobotLibrary only needs to be compiled once. Any code that relies on it can be rapidly tested and debugged.

Act:

  1. An external repository for all the test code was made, and validated.
  2. All changes were merged in to master.

Some lessons learnt:

Woolfrey commented 1 month ago

@ssutjipto FYI. I'm closing it since it's all done, but thought it was worth documenting.