Uh oh!
There was an error while loading. Please reload this page.
[Perf optimization] Use boost::container::small_vector - #3432
Conversation
This appears to be quite beneficial (cppcheck gets twice as fast on some files).
pfultz2
commented
Aug 30, 2021
I wonder if |
pfultz2
commented
Aug 30, 2021
Actually this may be of interest: |
danmar
commented
Aug 31, 2021
sweet!! 👍
yes. I'd rather not add a boost dependency. |
Ken-Patrick
commented
Aug 31, 2021
I thought so. ;-) |
Ken-Patrick
commented
Aug 31, 2021
Looking a bit more into this, it seems getTokenArgumentFunction is called many many times, hence the impact I saw from the cost of std::vector (in the call of getArguments in getTokenArgumentFunction) I don't know if std::vector will still be a little bottleneck after that. |
danmar
commented
Aug 31, 2021
👍 |
Ken-Patrick
commented
Aug 31, 2021
Done in #3435 |
pfultz2
commented
Aug 31, 2021
There are actually a lot of other places where we return vectors of size 1 or 2(such as |
Ken-Patrick
commented
Aug 31, 2021
Probably. I played a bit with stack_alloc, it is not very convenient for that, at least a lot less than SmallVector (from llvm or boost): you can't return simply return the vector<short_alloc> because you also need to return the arena, and it is non copyable. So you have to do one of the following:
|
pfultz2
commented
Sep 1, 2021
I see, I was thinking the arena was stored in the allocator. I did find this simple https://github.com/KonanM/small_vector/blob/master/include/small_vector/small_vector.h I think it requires C++17, but it looks like it could be easily adapted to C++11. |
pfultz2
commented
Sep 10, 2021
I actually found this library: https://github.com/alandefreitas/small Its header-only(so it should be easy to copy into our external directory). However, this requires C++17. We could create type aliases that fallback on STL containers when using older compilers if we want to use such a library. |
pfultz2
commented
Sep 10, 2021
Also, caffe2 uses a copy of LLVM's SmallVector class but removes dependencies on extra LLVM constructs: https://github.com/pytorch/pytorch/blob/master/c10/util/SmallVector.h We would just need to define |
danmar
commented
Sep 11, 2021
for information, I could allow something like: I want that it works to use old compilers but if some optimisation or "side feature" is lost that is fine. |
I propose adding at least the alias and use it for cases where the container is slow so it is implicitly documented. I also suggest using a separate header file for this instead of adding it to an existing one so it is only includes in places where it is actually needed. In CMake we could make Boost optional so it is only used when found and add it via a define as suggested by Danial. This would not add a hard dependency and we would not need to change any other build systems for now (as they might go away in the future anyways). Later we can replace it with a smaller and hard dependency. This would be a very small change. Also please profile the code before and after so we only use it in places it is actually needed. |
danmar
commented
Jan 17, 2022
not a hard dependency => sounds good to me. |
firewave
commented
Feb 4, 2022
#3799 adds the SmallVector alias with optional I have a follow-up which switches the most obvious offender over to it. Feel free to adjust other instances afterwards. Please profile before and afterwards so we don't switch over unnecessary usage. |
firewave
commented
Mar 20, 2022
Now that I have another one prepared but that needs some cleanups which cause performance regressions which I need to investigate (and possibly report to the compilers) first. Will be a few days until I have both completely ready. |
I posted a draft for another usage of |
firewave
commented
Jun 2, 2022
I did an intermediate improvement by pre-sizing the
|
FYI #3919 is close to being ready for review. |
This appears to be quite beneficial (cppcheck gets twice as fast on
some files).
This is a first proposal, it is not supposed to be merged as it is now (it is probably missing some dependencies in the CI, among other things).
I used boost::container::small_vector, but maybe there are other solutions here, especially if we don't want to add a dependency to boost: copying llvm/ADT/SmallVector.h ?, something else ?