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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Why do you care how many times an allocator is copied? They should be cheap
(essentially free) to copy.

A far more interesting test would look at how many bytes are allocated for
string concatenation:

#include <iostream>
#include <string>

int n_ = 0;
int b_ = 0;

void alloc_cnt() {
    std::cout<< "Allocators: " << n_ << ", bytes: " << b_ << '\n';
}

template <class T>
struct myAlloc_ : std:: allocator<T> {
  myAlloc_() { }
  myAlloc_(myAlloc_&& r) { ++n_; }
  myAlloc_(const myAlloc_& r) { ++n_; }
  myAlloc_& operator=(const myAlloc_& other) { return *this; }

  T* allocate(std::size_t n) {
    T* ptr = std::allocator<T>::allocate(n);
    b_ += n * sizeof(T);
    return ptr;
  }
 template <typename U>
 struct rebind { typedef myAlloc_<U> other; };
};
using mystring = std:: basic_string<char, std:: char_traits<char>,
myAlloc_<char>>;


int main()
{
  mystring f_ = "abc00000000000000000", b_ = "def", c_ = "xyz";
  alloc_cnt ();

  auto a = f_ + "mno" + b_;
  alloc_cnt ();

  auto c = c_ + f_ + b_;
  alloc_cnt ();

  auto d = c_ + (f_ + b_);
  alloc_cnt ();

  return 0;
}

With GCC 12:

Allocators: 3, bytes: 21
Allocators: 6, bytes: 83
Allocators: 9, bytes: 114
Allocators: 12, bytes: 176

With GCC 13:

Allocators: 3, bytes: 21
Allocators: 7, bytes: 52
Allocators: 11, bytes: 83
Allocators: 15, bytes: 114


The program uses less memory to achieve the same result. This was changed by
r13-3814-gc93baa93df2d45 and is actually important.

The number of copies of allocators is a meaningless figure.

Reply via email to