[libcxx] Patch/RFC: Avoid using ::operator new in the default allocator

Benjamin Kramer benny.kra at gmail.com
Wed Sep 25 12:14:21 PDT 2013


Since N3664 was implemented in Clang (r186799) it can't optimize unused pairs of ::operator new and ::operator delete anymore. Calls generated by a new/delete expression are still foldable with the updated wording. This affects optimizing away unnecessary code that would be really nice to get right. For example

#include <vector>

int main() {
  std::vector<int> v;
  v.push_back(1);

  return v[0];
}

This should fold down to "return 1;" with no allocations. The example is of course oversimplified but situations like this easily occur in real world code through inlining.

The proposed patch replaces "::operator new(x)" with "new char[x]" and adds the necessary casts in the allocator class, as suggested by Richard Smith. This is sufficient to constant fold code like my test case again.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: operator_new.patch
Type: application/octet-stream
Size: 1647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130925/5815fed6/attachment.obj>
-------------- next part --------------


- Ben


More information about the cfe-commits mailing list