public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/110945] New: std::basic_string::assign dramatically slower than other means of copying memory
@ 2023-08-08 10:42 janschultke at googlemail dot com
  2023-08-08 10:43 ` [Bug libstdc++/110945] " janschultke at googlemail dot com
                   ` (16 more replies)
  0 siblings, 17 replies; 18+ messages in thread
From: janschultke at googlemail dot com @ 2023-08-08 10:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110945
           Summary: std::basic_string::assign dramatically slower than
                    other means of copying memory
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janschultke at googlemail dot com
  Target Milestone: ---

See https://quick-bench.com/q/bqGjfyd180oOlJhiY_XnURMNKG8

Using the copy constructor performs best, and ends up using std::memcpy
internally. Even using .resize() and std::copy is much faster than .assign(),
because it is subject to more partial loop unrolling.

basic_string::assign:
https://github.com/gcc-mirror/gcc/blob/25c4b1620ebc10fceabd86a34fdbbaf8037e7e82/libstdc%2B%2B-v3/include/bits/basic_string.h#L1713C28-L1713C28

this calls the four-iterator form of .replace():
https://github.com/gcc-mirror/gcc/blob/25c4b1620ebc10fceabd86a34fdbbaf8037e7e82/libstdc%2B%2B-v3/include/bits/basic_string.h#L2378

this calls this form of _M_replace_dispatch(): (I think)
https://github.com/gcc-mirror/gcc/blob/25c4b1620ebc10fceabd86a34fdbbaf8037e7e82/libstdc%2B%2B-v3/include/bits/basic_string.tcc#L430

this calls _M_replace():
https://github.com/gcc-mirror/gcc/blob/25c4b1620ebc10fceabd86a34fdbbaf8037e7e82/libstdc%2B%2B-v3/include/bits/basic_string.tcc#L507

in this case, it should call _S_move():
https://github.com/gcc-mirror/gcc/blob/25c4b1620ebc10fceabd86a34fdbbaf8037e7e82/libstdc%2B%2B-v3/include/bits/basic_string.h#L431

this calls char_traits::move():
https://github.com/gcc-mirror/gcc/blob/25c4b1620ebc10fceabd86a34fdbbaf8037e7e82/libstdc%2B%2B-v3/include/bits/char_traits.h#L223

and that calls __builtin_memcpy()

However, I must have followed this chain of calls incorrectly, because I do not
see a call to memmove in the output assembly, and most of the time is spent
here:

>        nopl   (%rax)
>        movdqa 0x42d8a0(%rdx),%xmm0
> 63.27% movups %xmm0,(%rax,%rdx,1)
> 36.69% add    $0x10,%rdx
> 0.03%  cmp    $0x100000,%rdx

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2023-08-17 20:40 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-08 10:42 [Bug libstdc++/110945] New: std::basic_string::assign dramatically slower than other means of copying memory janschultke at googlemail dot com
2023-08-08 10:43 ` [Bug libstdc++/110945] " janschultke at googlemail dot com
2023-08-08 10:51 ` janschultke at googlemail dot com
2023-08-08 10:55 ` janschultke at googlemail dot com
2023-08-08 11:02 ` redi at gcc dot gnu.org
2023-08-08 11:03 ` redi at gcc dot gnu.org
2023-08-08 11:16 ` redi at gcc dot gnu.org
2023-08-08 11:17 ` redi at gcc dot gnu.org
2023-08-08 11:19 ` janschultke at googlemail dot com
2023-08-08 11:23 ` redi at gcc dot gnu.org
2023-08-08 11:27 ` redi at gcc dot gnu.org
2023-08-08 11:36 ` redi at gcc dot gnu.org
2023-08-08 11:56 ` redi at gcc dot gnu.org
2023-08-08 12:02 ` redi at gcc dot gnu.org
2023-08-08 12:54 ` redi at gcc dot gnu.org
2023-08-08 16:04 ` redi at gcc dot gnu.org
2023-08-17 20:31 ` cvs-commit at gcc dot gnu.org
2023-08-17 20:40 ` redi at gcc dot gnu.org

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).