improve performance of span_iterator w/ clang (#1166)
Some checks failed
CI_Android / Android (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (macos-latest) (push) Has been cancelled
cmake_find_package / Build ${{ matrix.os }} (ubuntu-latest) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 10) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 11) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 12) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 13, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 14, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 15, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 13, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 14, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 15, 23) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 14, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 17, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 20, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 23, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 14, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 17, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 20, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 23, 14.3.1) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled

* improve performance of span_iterator w/ clang

Issue: #1165

Before this PR, the range-for loop was ~3300x slower. After this PR, it
is ~1.005x slower

The clang optimizer is very good at optimizing `current != end`, so
we changed to this idiom. This moves the Expects assertion into the
constructor instead of on the hot-path which is called whenever either
operator++ or operator* is called.

Note: The codegen for the assertion is still a missed optimization,
but less worrisome as it only happens once per iterator.

Note: benchmarks on M1 Macbook Pro w/ Apple Clang 16.0.0
This commit is contained in:
Carson Radtke 2024-11-12 15:41:21 -06:00 committed by GitHub
parent 87f9d76886
commit f8ec309118
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -140,7 +140,9 @@ namespace details
constexpr span_iterator(pointer begin, pointer end, pointer current)
: begin_(begin), end_(end), current_(current)
{}
{
Expects(begin_ <= current_ && current <= end_);
}
constexpr operator span_iterator<const Type>() const noexcept
{
@ -149,21 +151,18 @@ namespace details
constexpr reference operator*() const noexcept
{
Expects(begin_ && end_);
Expects(begin_ <= current_ && current_ < end_);
Expects(current_ != end_);
return *current_;
}
constexpr pointer operator->() const noexcept
{
Expects(begin_ && end_);
Expects(begin_ <= current_ && current_ < end_);
Expects(current_ != end_);
return current_;
}
constexpr span_iterator& operator++() noexcept
{
Expects(begin_ && current_ && end_);
Expects(current_ < end_);
Expects(current_ != end_);
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
@ -180,8 +179,7 @@ namespace details
constexpr span_iterator& operator--() noexcept
{
Expects(begin_ && end_);
Expects(begin_ < current_);
Expects(begin_ != current_);
--current_;
return *this;
}