`<algorithm>`: `ranges::min`, `ranges::max` and `ranges::minmax` evaluate each element twice
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 55/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- cpp
- Lĩnh vực
- performance
Hướng nghiên cứu
Bắt đầu bằng cách xác định các triển khai của ranges::min, ranges::max và ranges::minmax cùng các bài kiểm thử của chúng, sau đó chạy trường hợp dòng lệnh repro.cpp được cung cấp để đo số lần gọi phép biến đổi. Công việc được xem là hoàn tất khi mỗi phần tử biến đổi được đánh giá đúng một lần đối với các overload này, đồng thời vẫn đáp ứng độ phức tạp so sánh và phép chiếu đã nêu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
std::ranges::min/max/minmax over a views::transform range invoke the transform roughly twice per element. libstdc++ invokes it once, and libc++'s count depends on the data. Since these overloads return range_value_t<R> by value and are constrained on indirectly_copyable_storable, one evaluation per element appears to be permitted.
Command-line test case
// cl /EHsc /std:c++latest /O2 repro.cpp
#include <algorithm>
#include <cstdio>
#include <ranges>
#include <vector>
int calls = 0;
struct Entry { double v; double get() const { ++calls; return v; } };
int main() {
std::vector<Entry> data;
for (int i = 1; i <= 8; ++i) data.push_back(Entry{static_cast<double>(i)});
calls = 0;
(void) std::ranges::min(data | std::views::transform(&Entry::get));
std::printf("ranges::min : %d\n", calls); // 15
calls = 0;
double m = 1e300;
for (const auto& e : data) m = (std::min)(m, e.get());
std::printf("hand loop : %d\n", calls); // 8
}
Observed (N = 8, so 8 is one call per element and 15 is 2N−1):
| MS STL 19.51 | libstdc++ | libc++ | |
|---|---|---|---|
| ranges::min over transform_view | 15 | 8 | <=15 |
| ranges::max over transform_view | 15 | 8 | <=15 |
| ranges::minmax over transform_view | 22 | 8 | 22 |
| ranges::fold_left over transform_view | 8 | 8 | 8 |
| hand-written loop | 8 | 8 | 8 |
Expected behavior
One evaluation per element, as libstdc++ achieves.
STL version
Microsoft Visual Studio 18 (Insiders), toolset 14.51.36231, compiler 19.51.36252, x64.
Additional context
[alg.min.max]/7 constrains comparisons and applications of the projection:
Complexity: Exactly
ranges::distance(r) - 1comparisons and twice as many applications of the projection, if any.
Here the projection is identity, and I could not find any wording in [alg.min.max] or [algorithms.requirements] that constrains how many times *i may be evaluated. An implementation that stores range_value_t<R> rather than tracking an iterator would satisfy the stated complexity exactly while halving the dereferences. minmax seems to be even more permissive with its "at most" wording.
Note that [alg.min.max]/27 has similar wording on the min_element variant, except that "if any" is suspiciously missing:
Complexity: Exactly
max(last - first - 1,0)comparisons and twice as many projections.
I guess the pedantic reading of this gives the reason why min_element cannot have a similar optimisation (along with minor performance and design quirks on caching reference alongside of the iterator). This is deliberately not part of this report.
Measured impact
I was trying to write a blog post advocating for C++20 ranges, and ran into unexpected performance issues when using costly transform.
With a non-trivial transform (std::cos(angle) * length), MSVC 19.51 /O2: 2.09× a hand-written loop at N = 131072, and 4.77× at N = 2048. MSVC is not able to optimise out the redundancy at /O2 /Ob3 /GL /arch:AVX2 /LTCG or with /fp:fast.
If the maintainers consider this a worthwhile change, I would be happy to prepare a pull request for std::max/min/minmax with identity projection.
- Ngôn ngữ chính
- C++
- Star
- 11.2k
- Fork
- 1.7k
- Merge trung bình
- 4 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 14
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/STL
-
LWG
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 2 ngày
-
generator LWG
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 2 ngày
-
LWG
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 2 ngày
-
`<__msvc_math.hpp>`: `_USE_MATH_DEFINES` is ignored after the first `<math.h>` include under `/Zc:cmath`Có thể đã có người làm @tiagomacarios đã nhận 10 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của microsoft/STL
Issue tương tự
-
bug data-types
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 71/100
ClickHouse/clickhouse-odbc#680 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
ionut-mihalache/aqua#31 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-kind
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
villagesql/villagesql-server#1702 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
microsoft/vcpkg-tool#2191 · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày