db5fa0837e
Inserting a pair<Key, Value> into a map<Key, Value> will allocate a new node and construct a pair<const Key, Value> in the node, then check if the Key is already present in the map. That is because pair<Key, Value> is not the same type as the map's value_type. But it only differs in the const-qualification on the Key, and so we should be able to do the lookup directly, without allocating a new node. This avoids allocating and then deallocating a node for the case where the key is already found and nothing gets inserted. We can take this optimization further and lookup the key directly for a pair<Key, X>, pair<const Key, X>, pair<Key&, X> etc. for any X. A strict reading of the standard says we can only do this when we know the allocator won't do anything funky with the value when constructing a pair<const Key, Value> from a slightly different type. Inserting that type only requires the value_type to be Cpp17EmplaceInsertable into the container, and that doesn't have any requirement that the value is unchanged (unlike Cpp17CopyInsertable and Cpp17MoveInsertable). For that reason, the optimization is only done for maps using std::allocator. A similar optimization can be done for map.emplace(key, value) where the first argument is similar to the key_type and so can be looked up without allocating a new node and constructing a key_type. Finally, both of the insert and emplace cases can use the same optimization when key_type is a scalar type and some other scalar is being passed as the insert/emplace argument. Converting from one scalar type to another won't have surprising value-altering behaviour, and has no side effects (unlike e.g. constructing a std::string from a const char* argument, which might allocate). We don't need to do this for std::multimap, because we always insert the new node even if the key is already present. So there's no benefit to doing the lookup before allocating the new node. libstdc++-v3/ChangeLog: PR libstdc++/92300 * include/bits/stl_map.h (insert(Pair&&), emplace(Args&&...)): Check whether the arguments can be looked up directly without constructing a temporary node first. * include/bits/stl_pair.h (__is_pair): Move to here, from ... * include/bits/uses_allocator_args.h (__is_pair): ... here. * testsuite/23_containers/map/modifiers/emplace/92300.cc: New test. * testsuite/23_containers/map/modifiers/insert/92300.cc: New test. |
||
---|---|---|
c++tools | ||
config | ||
contrib | ||
fixincludes | ||
gcc | ||
gnattools | ||
gotools | ||
include | ||
INSTALL | ||
intl | ||
libada | ||
libatomic | ||
libbacktrace | ||
libcc1 | ||
libcody | ||
libcpp | ||
libdecnumber | ||
libffi | ||
libgcc | ||
libgfortran | ||
libgo | ||
libgomp | ||
libiberty | ||
libitm | ||
libobjc | ||
liboffloadmic | ||
libphobos | ||
libquadmath | ||
libsanitizer | ||
libssp | ||
libstdc++-v3 | ||
libvtv | ||
lto-plugin | ||
maintainer-scripts | ||
zlib | ||
.dir-locals.el | ||
.gitattributes | ||
.gitignore | ||
ABOUT-NLS | ||
ar-lib | ||
ChangeLog | ||
ChangeLog.jit | ||
ChangeLog.tree-ssa | ||
compile | ||
config-ml.in | ||
config.guess | ||
config.rpath | ||
config.sub | ||
configure | ||
configure.ac | ||
COPYING | ||
COPYING3 | ||
COPYING3.LIB | ||
COPYING.LIB | ||
COPYING.RUNTIME | ||
depcomp | ||
install-sh | ||
libtool-ldflags | ||
libtool.m4 | ||
lt~obsolete.m4 | ||
ltgcc.m4 | ||
ltmain.sh | ||
ltoptions.m4 | ||
ltsugar.m4 | ||
ltversion.m4 | ||
MAINTAINERS | ||
Makefile.def | ||
Makefile.in | ||
Makefile.tpl | ||
missing | ||
mkdep | ||
mkinstalldirs | ||
move-if-change | ||
multilib.am | ||
README | ||
symlink-tree | ||
test-driver | ||
ylwrap |
This directory contains the GNU Compiler Collection (GCC). The GNU Compiler Collection is free software. See the files whose names start with COPYING for copying permission. The manuals, and some of the runtime libraries, are under different terms; see the individual source files for details. The directory INSTALL contains copies of the installation information as HTML and plain text. The source of this information is gcc/doc/install.texi. The installation information includes details of what is included in the GCC sources and what files GCC installs. See the file gcc/doc/gcc.texi (together with other files that it includes) for usage and porting information. An online readable version of the manual is in the files gcc/doc/gcc.info*. See http://gcc.gnu.org/bugs/ for how to report bugs usefully. Copyright years on GCC source files may be listed using range notation, e.g., 1987-2012, indicating that every year in the range, inclusive, is a copyrightable year that could otherwise be listed individually.