Skip to content

Heap optimizations

Oleksandr Koval requested to merge OleksandrKvl/cmake:heap-optimizations into master

This is a result of this thread.

Brief overview and considerations.

  1. In cmState, store BuiltinCommands and ScriptedCommands as a std::shared_ptr<std::function> to avoid copy in GetCommandByExactName() calls. Now care should be taken not to change what GetCommandByExactName() returns, also commands' operator() should be const, i.e. not change inner state because now it's preserved between calls. I have not found any problems but additional review is still required.

  2. Making cmListFileFunction a std::shared_ptr<cmListFileFunctionImpl>. It avoids a lot of copy operations especially in FunctionBlockers. Same as above, if someone really needs copy, now it should be done explicitly. I just created type-alias and changed all places to use opeator->(). Also, it might be better to leave cmListFileFunction as it was, create cmListFileFunctionPtr alias and use it everywhere instead of cmListFileFunction.

3rd and 4th commits optimize obviously useless copy operations that were top consumers in heaptrack report when I've fixed previous ones. Here's a benchmark of different join methods.

Also, consider making cmState::BuiltinCommands and cmState::ScriptedCommands a std::unordered_map. I can't see any reason why it should be a std::map, their order is not used anywhere. I also made a benchmark for it. You can see that querying a std::map is faster only for a very small(<10) set. Currently, CMake has ~111 built-in commands and even empty run results in 70+ scripted commands. If it's approved I can add it to this MR as well.

Merge request reports