clangir icon indicating copy to clipboard operation
clangir copied to clipboard

Optimization: semantic inline

Open kelbon opened this issue 1 year ago • 0 comments

Inline is a main optimization for creating zero-overhead abstractions and its working, but not as good as it may

There are huge problem In current C++: its constructors and emplaces are not zero overhead.

Emplace requires prvalue to materialize, it prevents RVO and copy elision

Examples:

  • constructor vs aggregate: https://godbolt.org/z/vMK34vvsn
  • returning value and strange performnce rules about not using std::move in return expression
  • all emplaces produce one useless move for each argumnt, for example .emplace_back in vector may be better with macros: (ironically also better compilation speed, diagnostics and readability of code)
#define  vector_emplace_back(vec, val)                  \
auto* address = vec.__push_back_uninitialized_memory(); \
new (address) val // (yes, there should  be alloc::construct, but it has same problem as emplace)

// usage:
vector<string> v;
vector_emplace_back(v, string("hello")); // no overhead



Compiler should be able to inline such calls and eluminate useless moves

I think Clang IR is a best place to make such optimization

kelbon avatar Feb 05 '24 09:02 kelbon