public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/62187] New: std::string==const char* could compare sizes first
@ 2014-08-19 13:21 glisse at gcc dot gnu.org
2022-06-14 14:58 ` [Bug libstdc++/62187] " redi at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: glisse at gcc dot gnu.org @ 2014-08-19 13:21 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62187
Bug ID: 62187
Summary: std::string==const char* could compare sizes first
Product: gcc
Version: 5.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: enhancement
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: glisse at gcc dot gnu.org
Hello,
when I compare 2 basic_string with ==, libstdc++ only uses the optimization of
first checking that the sizes are the same (before calling compare) if
__is_char<_CharT> and the traits and allocator are the default ones. I don't
understand why, but assuming there is a good reason, I believe the optimization
should still apply when comparing std::string and const char*.
(this applies to __vstring as well)
This was noticed in PR 62156, where we also see that std::string("foo") does a
memcpy of size 3 then sets the 4th char to '\0', where a single memcpy of size
4 would make sense.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug libstdc++/62187] std::string==const char* could compare sizes first
2014-08-19 13:21 [Bug libstdc++/62187] New: std::string==const char* could compare sizes first glisse at gcc dot gnu.org
@ 2022-06-14 14:58 ` redi at gcc dot gnu.org
2022-06-14 20:19 ` cvs-commit at gcc dot gnu.org
2022-06-14 20:20 ` redi at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2022-06-14 14:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62187
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Assignee|unassigned at gcc dot gnu.org |redi at gcc dot gnu.org
Status|NEW |ASSIGNED
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #5)
> I've also created an LWG issue about this,
Rather than a new issue, this was added to https://wg21.link/lwg2852
The resolution was to confirm that operator== doesn't need to call compare if
it can determine the result another way. That means we can do the length check
unconditionally.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug libstdc++/62187] std::string==const char* could compare sizes first
2014-08-19 13:21 [Bug libstdc++/62187] New: std::string==const char* could compare sizes first glisse at gcc dot gnu.org
2022-06-14 14:58 ` [Bug libstdc++/62187] " redi at gcc dot gnu.org
@ 2022-06-14 20:19 ` cvs-commit at gcc dot gnu.org
2022-06-14 20:20 ` redi at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-14 20:19 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62187
--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:
https://gcc.gnu.org/g:6abe341558abec40c9c44d76e7fb4fb3978e894b
commit r13-1096-g6abe341558abec40c9c44d76e7fb4fb3978e894b
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Tue Jun 14 16:19:32 2022 +0100
libstdc++: Check lengths first in operator== for basic_string [PR62187]
As confirmed by LWG 2852, the calls to traits_type::compare do not need
to be obsvervable, so we can make operator== compare string lengths
first and return immediately for non-equal lengths. This avoids doing a
slow string comparison for "abc...xyz" == "abc...xy". Previously we only
did this optimization for std::char_traits<char>, but we can enable it
unconditionally thanks to LWG 2852.
For comparisons with a const char* we can call traits_type::length right
away to do the same optimization. That strlen call can be folded away
for constant arguments, making it very efficient.
For the pre-C++20 operator== and operator!= overloads we can swap the
order of the arguments to take advantage of the operator== improvements.
libstdc++-v3/ChangeLog:
PR libstdc++/62187
* include/bits/basic_string.h (operator==): Always compare
lengths before checking string contents.
[!__cpp_lib_three_way_comparison] (operator==, operator!=):
Reorder arguments.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug libstdc++/62187] std::string==const char* could compare sizes first
2014-08-19 13:21 [Bug libstdc++/62187] New: std::string==const char* could compare sizes first glisse at gcc dot gnu.org
2022-06-14 14:58 ` [Bug libstdc++/62187] " redi at gcc dot gnu.org
2022-06-14 20:19 ` cvs-commit at gcc dot gnu.org
@ 2022-06-14 20:20 ` redi at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2022-06-14 20:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62187
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|ASSIGNED |RESOLVED
Target Milestone|--- |13.0
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for GCC 13.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-06-14 20:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-19 13:21 [Bug libstdc++/62187] New: std::string==const char* could compare sizes first glisse at gcc dot gnu.org
2022-06-14 14:58 ` [Bug libstdc++/62187] " redi at gcc dot gnu.org
2022-06-14 20:19 ` cvs-commit at gcc dot gnu.org
2022-06-14 20:20 ` 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).