[Bug libstdc++/87106] Group move and destruction of the source, where possible, for speed

glisse at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Oct 26 22:02:00 GMT 2018


--- Comment #9 from Marc Glisse <glisse at gcc dot gnu.org> ---
I was looking into using relocation in std::swap. For a type like deque (if we
ignore the swap overload), using memcpy is really worth it. For a more simple
type like int, using memcpy loses type information, which can hurt other
optimizations, so I'd rather stick to the typed assignments for swap<int>. It
isn't obvious what the right compromise is. Maybe types that are trivially
relocatable but not trivially movable and destructible? Or just non-trivial?
That might work if libstdc++ wasn't stuck with a legacy, non-trivial

For the array swap, I could use a large intermediate buffer on the side (memcpy
really shines on large regions), possibly as large as the full array, but I
would need to cap it to some safe size (hard to define).

By the way, when swapping ranges of a type that does not specialize/overload
swap, it seems wasteful to call swap on each element, because it creates a new
temporary object for each element instead of reusing the same one. It may be
possible to detect if swap would call an overload in a different namespace, but
it seems harder to detect if std::swap was specialized).

More information about the Gcc-bugs mailing list