WeSearch

Std: Is_heap Could Be Faster

Arthur O’Dwyer· ·3 min read · 0 reactions · 0 comments · 10 views
#c++#performance#algorithms#stl#optimization
⚡ TL;DR · AI summary

The article discusses potential inefficiencies in the implementation of std::is_heap in C++ standard libraries, noting that it unnecessarily requires random access iterators despite being implementable with forward traversal. A proposed alternative implementation reduces redundant arithmetic and improves performance in benchmarks. The author highlights that major standard library implementations, including libc++, libstdc++, and MS STL, exhibit similar inefficiencies.

Key facts
Original article
Arthur O’Dwyer · Arthur O’Dwyer
Read full at Arthur O’Dwyer →
Opening excerpt (first ~120 words) tap to expand

std::is_heap could be faster The other day I was noodling around with some libc++ unit-test code that looked roughly like this (Godbolt): template<class A> auto extract_container(A& a) { struct UnwrapAdaptor : A { A::container_type& cc = A::c; }; return UnwrapAdaptor(a).cc; } template<class Adaptor> void test_push_range(bool is_heapified) { int in1[] = {1,3,7}; int in2[] = {2,4,5,6}; int expected[] = {1,3,7,2,4,5,6}; Adaptor a; a.push_range(in1); a.push_range(in2); if (auto c = extract_container(a); is_heapified) { assert(std::ranges::is_heap(c)); assert(std::ranges::is_permutation(c, expected)); } else { assert(std::ranges::equal(c, expected)); } } int main() { test_push_range<std::stack<int>>(false); test_push_range<std::queue<int>>(false);…

Excerpt limited to ~120 words for fair-use compliance. The full article is at Arthur O’Dwyer.

Anonymous · no account needed
Share 𝕏 Facebook Reddit LinkedIn Threads WhatsApp Bluesky Mastodon Email

Discussion

0 comments

More from Arthur O’Dwyer