https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94295

            Bug ID: 94295
           Summary: use __builtin_operator_new and
                    __builtin_operator_delete when available
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richard-gccbugzilla at metafoo dot co.uk
  Target Milestone: ---

See https://bugs.llvm.org/show_bug.cgi?id=45287 for some background.

The C++ language rules do not permit optimization (eg, deletion) of direct
calls to 'operator new' and 'operator delete'. libstdc++ uses such calls to
implement std::allocator:

https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/ext/new_allocator.h#L112

As a consequence, allocations performed by libstdc++'s containers are not
optimizable.

Clang provides a pair of builtin functions to work around this issue:
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-operator-new-and-builtin-operator-delete

__builtin_operator_new(args) is equivalent to ::operator new(args) except that
it permits optimizations.
__builtin_operator_delete(args) is equivalent to ::operator delete(args) except
that it permits optimizations.

You can detect support for these builtins with

#ifdef __has_builtin
#if __has_builtin(__builtin_operator_new) >= 201802L
// ...
#endif
#endif

(Note that __has_builtin(...) returned 1 for an older version of the builtins
that didn't support placement forms, and so couldn't be used for aligned
allocation and sized delete. It's probably not worth your time dealing with
those.)

This bug requests that libstdc++ uses these builtins when available.
(Separately, it'd be great if GCC considered supporting them too.)

Reply via email to