[all-commits] [llvm/llvm-project] 84ae8c: [libc++] `std::ranges::advance`: avoid unneeded bo...

Jan Kokemüller via All-commits all-commits at lists.llvm.org
Tue Apr 2 16:09:48 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 84ae8cb4af9abafe9f45e69744607aadb38d649a
      https://github.com/llvm/llvm-project/commit/84ae8cb4af9abafe9f45e69744607aadb38d649a
  Author: Jan Kokemüller <jan.kokemueller at gmail.com>
  Date:   2024-04-02 (Tue, 02 Apr 2024)

  Changed paths:
    M libcxx/include/__iterator/advance.h
    M libcxx/test/std/iterators/iterator.primitives/range.iter.ops/range.iter.ops.advance/iterator_count_sentinel.pass.cpp
    M libcxx/test/support/test_iterators.h

  Log Message:
  -----------
  [libc++] `std::ranges::advance`: avoid unneeded bounds checks when advancing iterator (#84126)

Currently, the bounds check in `std::ranges::advance(it, n, s)` is done
_before_ `n` is checked. This results in one extra, unneeded bounds
check.

Thus, `std::ranges::advance(it, 1, s)` currently is _not_ simply
equivalent to:

```c++
if (it != s) {
    ++it;
}
```

This difference in behavior matters when the check involves some
"expensive" logic. For example, the `==` operator of
`std::istreambuf_iterator` may actually have to read the underlying
`streambuf`.

Swapping around the checks in the `while` results in the expected
behavior.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list