This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
[g++] make_pair inlining failure?
- From: John Doe <m8rph at hotmail dot com>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Mon, 28 Nov 2011 13:48:02 +0000
- Subject: [g++] make_pair inlining failure?
Hi!
Currently I am participating in a project where a large number of
objects are inserted into a std::map. Recently someone replaced all calls of
map_instance.insert( std::make_pair(key, value) );
with
map_instance.insert( MapType::value_type(key, value) );
claiming it would be faster.
I called it a pseudo optimization because I assumed g++ was able to
inline std::make_pair with optimizations enabled. I tried to prove it
using a simple example (see below) and comparing the resulting
executables. When using empty constructors the executables were
identical. They were different when having non-empty constructors.
Now I have two questions:
1. Is the g++ recognizing that std::make_pair can and *should* be inlined?
2. Why do the executables differ (especially if 1. is true)?
Note that I refrained from printing something. I was not sure whether
optimizing would have changed the "observable behavior" in this case (as
defined in the C++ standard).
Version:
$ g++ --version
g++ (Gentoo 4.4.5 p1.3, pie-0.4.5) 4.4.5
Comparison:
g++ -O3 -s -finline-limit=1000000 code-0.cpp -o a.out-0
hexdump -C a.out-0 > 0.txt
# repeat with other file
diff 0.txt 1.txt | less
Code:
#include <iostream>
#include <map>
#include <cstring>
using namespace std;
struct A
{
static const size_t SIZE = 1024 * 1024;
A() : x_(new size_t[SIZE])
{
memset(x_, 0, SIZE * sizeof(size_t));
}
A(const A& rhs)
: x_(new size_t[SIZE])
{
memcpy(x_, rhs.x_, SIZE * sizeof(size_t));
}
~A() { delete [] x_; }
size_t* x_;
};
int main()
{
std::map<int, A> test;
// non-optimized case
test.insert( std::make_pair(1,A()) );
// optimized case
//test.insert( std::map<int, A>::value_type(1,A()) );
}