Issue |
90785
|
Summary |
[libc++] Code generation for bounds checks seem unoptimal
|
Labels |
libc++,
performance,
hardening
|
Assignees |
|
Reporter |
ldionne
|
In `std::vector`, we currently do
```c++
template <class _Tp, class _Allocator>
reference vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < size(), "vector[] index out of bounds");
return this->__begin_[__n];
}
```
However, this requires computing `end - begin` and comparing it to `__n`. Since we already need to compute `__begin + __n` (since we do `__begin[__n]`), we could probably express the condition differently to improve code generation:
```c++
template <class _Tp, class _Allocator>
reference vector<_Tp, _Allocator>::operator[](size_type __n) _NOEXCEPT {
auto* __p = vec.__begin_ + __n;
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < vec.__end_, "vector[] index out of bounds");
return *__p;
}
```
This seems to generate better code indeed, see https://godbolt.org/z/dzPvMcdfE.
However, I wonder if there's some kind of tradeoff where this would be less easily optimized by the compiler. I just don't have a good intuition for which one is going to be better in the general case.
CC @fhahn @var-const
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs