Bug 32907 - Inefficient operator== in std::string
Summary: Inefficient operator== in std::string
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: 4.3.0
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-26 19:12 UTC by Chris Jefferson
Modified: 2022-06-16 13:15 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-07-26 21:51:02


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Jefferson 2007-07-26 19:12:40 UTC
This is picked up from http://gcc.gnu.org/ml/gcc/2007-07/msg00681.html , apologises if it has already been dealt with.

Both std::string and std::list do not compare lengths before comparing elements in operator==. In std::string this increases the chances of quitting early and produces a small improvement even for equal length strings. For std::list this produces a much larger gain. I believe the operator== for list should simply be replaced implementation used for all the other containers:

{ return (__x.size() == __y.size()
              && std::equal(__x.begin(), __x.end(), __y.begin())); }

Which is efficent as list has a O(1) size.
Comment 1 Paolo Carlini 2007-07-26 19:35:09 UTC
Well, in our current, C++03, implementation, definitely list::size is O(N). The issue is thorny, as you know well. For C++0x, Howard has a proposal related to the additional splice overload, I'm not sure which is the progress on that...
Comment 2 Chris Jefferson 2007-07-26 19:41:25 UTC
Ah, woops, many apologises. Too long since I've looked at list::size, I forgot which way around libstdc++ differed from the rest of the world :)
Comment 3 paolo@gcc.gnu.org 2007-07-27 17:25:17 UTC
Subject: Bug 32907

Author: paolo
Date: Fri Jul 27 17:25:04 2007
New Revision: 126988

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=126988
Log:
2007-07-27  Paolo Carlini  <pcarlini@suse.de>

	PR libstdc++/32907
	* include/bits/basic_string.h (operator==(const basic_string<_CharT>&,
	const basic_string<_CharT>&)): Add.
	(operator!=): Forward to operator==.
	* include/ext/vstring.h(operator==(const __versa_string<_CharT,
	std::char_traits<_CharT>, std::allocator<_CharT>, _Base>&,
	const __versa_string<_CharT, std::char_traits<_CharT>,
	std::allocator<_CharT>, _Base>&)): Add.
	(operator!=): Forward to operator==.

	* include/ext/sso_string_base.h (_M_compare): Remove.

Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/basic_string.h
    trunk/libstdc++-v3/include/ext/sso_string_base.h
    trunk/libstdc++-v3/include/ext/vstring.h

Comment 4 Paolo Carlini 2007-07-27 17:25:57 UTC
Fixed for 4.3.0.