[libcxx-commits] [libcxx] [libc++] Refactor std::{fill_n, count} for vector<bool>::iterator (PR #120305)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 28 08:09:12 PST 2025


winner245 wrote:

> I am pretty sure I remember there was a performance reason why @philnik777 refactored the value into a template parameter when he last looked at this. I'd like him to chime in.

@philnik777, Would you share a bit of your insights on the performance reasons @ldionne mentioned? It would be really helpful to understand your concerns on this.

In the meantime, I've double-checked the current implementation. While it uses the bool parameter as a NTTP, the if condition checks on the bool values are all performed at runtime. I think we may improve the current implementation in the following two directions:

1. **Performance**: If we are concerned about the performance impact due to runtime if, we should leverage the compile-time benefits of NTTP by using `constexpr if` (C++17 and later) or templates with SFINAE (C++14 and earlier). Using NTTP while still performing runtime if still incurs unnecessary overhead.

2. **Clarity**: If we can tolerate the minor performance impact (as is currently implemented), it is more straightforward to use a regular function parameter instead of NTTP. This allows us to simplify the implementation a bit by removing unnecessary if/else statements, as this refactoring does.

Which approach do you find more useful?

https://github.com/llvm/llvm-project/pull/120305


More information about the libcxx-commits mailing list