public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/112682] New: More efficient std::basic_string move construction
@ 2023-11-23 13:49 antoshkka at gmail dot com
  0 siblings, 0 replies; only message in thread
From: antoshkka at gmail dot com @ 2023-11-23 13:49 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112682
           Summary: More efficient std::basic_string move construction
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

A few places in bits/basic_string.h have the following code:

```
        if (__str._M_is_local())
          {
            _M_init_local_buf();
            traits_type::copy(_M_local_buf, __str._M_local_buf,
                              __str.length() + 1);
          }
```

Despite the knowledge, that `__str.length()` is not greater than 15 the
compiler emits (and inlines) a memcpy call. That results in a quite big set of
instructions https://godbolt.org/z/j35MMfxzq

Replacing `__str.length() + 1` with `_S_local_capacity + 1` explicitly forces
the compiler to copy the whole `__str._M_local_buf`. As a result the assembly
becomes almost 5 times shorter and without any function calls or multiple
conditional jumps https://godbolt.org/z/bfq8bxra9


P.S.: not sure, if it is allowed to copy uninitialized data via
traits_type::copy and would the sanitizers be happy with such copy attempt.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-23 13:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-23 13:49 [Bug libstdc++/112682] New: More efficient std::basic_string move construction antoshkka at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).