Improved placement proxy to avoid temporary objects when constructing from values from the target type.

Added testcase based on Howard Hinnant's "Insert vs. Emplace" article.
This commit is contained in:
Ion Gaztañaga
2014-08-02 21:30:39 +02:00
parent 08b26b5d99
commit 90be67e81f
3 changed files with 668 additions and 0 deletions

View File

@@ -0,0 +1,467 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Howard Hinnant 2014.
// (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
//
// This testcase is based on H. Hinnant's article "Insert vs. Emplace",
// (http://github.com/HowardHinnant/papers/blob/master/insert_vs_emplace.html)
//
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <boost/move/utility.hpp>
#include <boost/container/vector.hpp>
#include <boost/detail/lightweight_test.hpp>
class X
{
int i_;
int* p_;
BOOST_COPYABLE_AND_MOVABLE(X)
public:
struct special
{
unsigned c;
unsigned dt;
unsigned cc;
unsigned ca;
unsigned mc;
unsigned ma;
friend bool operator==(const special &l, const special &r)
{ return l.c == r.c && l.dt == r.dt && l.cc == r.cc && l.ca == r.ca && l.mc == r.mc && l.ma == r.ma; }
};
static special sp;
X(int i, int* p)
: i_(i)
, p_(p)
{
// std::cout << "X(int i, int* p)\n";
sp.c++;
}
~X()
{
// std::cout << "~X()\n";
sp.dt++;
}
X(const X& x)
: i_(x.i_)
, p_(x.p_)
{
// std::cout << "X(const X& x)\n";
sp.cc++;
}
X& operator=(BOOST_COPY_ASSIGN_REF(X) x)
{
i_ = x.i_;
p_ = x.p_;
// std::cout << "X& operator=(const X& x)\n";
sp.ca++;
return *this;
}
X(BOOST_RV_REF(X) x) BOOST_CONTAINER_NOEXCEPT
: i_(x.i_)
, p_(x.p_)
{
// std::cout << "X(X&& x)\n";
sp.mc++;
}
X& operator=(BOOST_RV_REF(X) x) BOOST_CONTAINER_NOEXCEPT
{
i_ = x.i_;
p_ = x.p_;
// std::cout << "X& operator=(X&& x)\n";
sp.ma++;
return *this;
}
};
std::ostream&
operator<<(std::ostream& os, X::special const& sp)
{
os << "Const: " << sp.c << '\n';
os << "Destr: " << sp.dt << '\n';
os << "CopyC: " << sp.cc << '\n';
os << "CopyA: " << sp.ca << '\n';
os << "MoveC: " << sp.mc << '\n';
os << "MoveA: " << sp.ma << '\n';
os << "Total: " << (sp.c + sp.dt + sp.cc + sp.ca + sp.mc + sp.ma) << '\n';
return os;
}
X::special X::sp = X::special();
int
main()
{
X::special insert_results;
X::special emplace_results;
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--insert lvalue no reallocation--\n";
X::sp = X::special();
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace lvalue no reallocation--\n";
X::sp = X::special();
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--insert xvalue no reallocation--\n";
X::sp = X::special();
v.insert(v.begin(), boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace xvalue no reallocation--\n";
X::sp = X::special();
v.emplace(v.begin(), boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--insert rvalue no reallocation--\n";
X::sp = X::special();
v.insert(v.begin(), X(0,0));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace rvalue no reallocation--\n";
X::sp = X::special();
v.emplace(v.begin(), X(0,0));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
//With emulated move semantics an extra copy is unavoidable
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
BOOST_TEST_EQ(insert_results == emplace_results, true);
#endif
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--insert lvalue reallocation--\n";
X::sp = X::special();
v.insert(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace lvalue reallocation--\n";
X::sp = X::special();
v.emplace(v.begin(), x);
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--insert xvalue reallocation--\n";
X::sp = X::special();
v.insert(v.begin(), boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace xvalue reallocation--\n";
X::sp = X::special();
v.emplace(v.begin(), boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--insert rvalue reallocation--\n";
X::sp = X::special();
v.insert(v.begin(), X(0,0));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace rvalue reallocation--\n";
X::sp = X::special();
v.emplace(v.begin(), X(0,0));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
//With emulated move semantics an extra copy is unavoidable
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
BOOST_TEST_EQ(insert_results == emplace_results, true);
#endif
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--push_back lvalue no reallocation--\n";
X::sp = X::special();
v.push_back(x);
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace_back lvalue no reallocation--\n";
X::sp = X::special();
v.emplace_back(x);
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--push_back xvalue no reallocation--\n";
X::sp = X::special();
v.push_back(boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace_back xvalue no reallocation--\n";
X::sp = X::special();
v.emplace_back(boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--push_back rvalue no reallocation--\n";
X::sp = X::special();
v.push_back(X(0,0));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(4);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace_back rvalue no reallocation--\n";
X::sp = X::special();
v.emplace_back(X(0,0));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
//With emulated move semantics an extra copy is unavoidable
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
BOOST_TEST_EQ(insert_results == emplace_results, true);
#endif
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--push_back lvalue reallocation--\n";
X::sp = X::special();
v.push_back(x);
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace_back lvalue reallocation--\n";
X::sp = X::special();
v.emplace_back(x);
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--push_back xvalue reallocation--\n";
X::sp = X::special();
v.push_back(boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
X x(0,0);
std::cout << "--emplace_back xvalue reallocation--\n";
X::sp = X::special();
v.emplace_back(boost::move(x));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
BOOST_TEST_EQ(insert_results == emplace_results, true);
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--push_back rvalue reallocation--\n";
X::sp = X::special();
v.push_back(X(0,0));
std::cout << X::sp;
std::cout << "----\n";
insert_results = X::sp;
}
{
boost::container::vector<X> v;
v.reserve(3);
v.push_back(X(0,0));
v.push_back(X(0,0));
v.push_back(X(0,0));
std::cout << "--emplace_back rvalue reallocation--\n";
X::sp = X::special();
v.emplace_back(X(0,0));
std::cout << X::sp;
std::cout << "----\n";
emplace_results = X::sp;
}
//With emulated move semantics an extra copy is unavoidable
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
BOOST_TEST_EQ(insert_results == emplace_results, true);
#endif
return boost::report_errors();
}

View File

@@ -22,6 +22,7 @@
#include <boost/container/detail/destroyers.hpp>
#include <boost/aligned_storage.hpp>
#include <boost/move/utility.hpp>
#include <boost/container/detail/mpl.hpp>
#include <iterator> //std::iterator_traits
#include <boost/assert.hpp>
#include <boost/detail/no_exceptions_support.hpp>
@@ -269,6 +270,44 @@ struct insert_emplace_proxy
}
};
//Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type
template<class A, class Iterator>
struct insert_emplace_proxy<A, Iterator, typename boost::container::allocator_traits<A>::value_type>
: public insert_move_proxy<A, Iterator>
{
explicit insert_emplace_proxy(typename boost::container::allocator_traits<A>::value_type &&v)
: insert_move_proxy<A, Iterator>(v)
{}
};
template<class A, class Iterator>
struct insert_emplace_proxy<A, Iterator, typename boost::container::allocator_traits<A>::value_type &>
: public insert_copy_proxy<A, Iterator>
{
explicit insert_emplace_proxy(const typename boost::container::allocator_traits<A>::value_type &v)
: insert_copy_proxy<A, Iterator>(v)
{}
};
template<class A, class Iterator>
struct insert_emplace_proxy<A, Iterator, const typename boost::container::allocator_traits<A>::value_type &>
: public insert_copy_proxy<A, Iterator>
{
explicit insert_emplace_proxy(const typename boost::container::allocator_traits<A>::value_type &v)
: insert_copy_proxy<A, Iterator>(v)
{}
};
template<class A, class Iterator>
struct insert_emplace_proxy<A, Iterator, const typename boost::container::allocator_traits<A>::value_type>
: public insert_copy_proxy<A, Iterator>
{
explicit insert_emplace_proxy(const typename boost::container::allocator_traits<A>::value_type &v)
: insert_copy_proxy<A, Iterator>(v)
{}
};
}}} //namespace boost { namespace container { namespace container_detail {
#else //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
@@ -347,6 +386,34 @@ struct BOOST_PP_CAT(insert_emplace_proxy_arg, N)
#define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
#include BOOST_PP_LOCAL_ITERATE()
//Specializations to avoid an unneeded temporary when emplacing from a single argument o type value_type
template<class A, class Iterator>
struct insert_emplace_proxy_arg1<A, Iterator, ::boost::rv<typename boost::container::allocator_traits<A>::value_type> >
: public insert_move_proxy<A, Iterator>
{
explicit insert_emplace_proxy_arg1(typename boost::container::allocator_traits<A>::value_type &v)
: insert_move_proxy<A, Iterator>(v)
{}
};
template<class A, class Iterator>
struct insert_emplace_proxy_arg1<A, Iterator, typename boost::container::allocator_traits<A>::value_type>
: public insert_copy_proxy<A, Iterator>
{
explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits<A>::value_type &v)
: insert_copy_proxy<A, Iterator>(v)
{}
};
/*
template<class A, class Iterator>
struct insert_emplace_proxy_arg1<A, Iterator, const typename boost::container::allocator_traits<A>::value_type>
: public insert_copy_proxy<A, Iterator>
{
explicit insert_emplace_proxy_arg1(const typename boost::container::allocator_traits<A>::value_type &v)
: insert_copy_proxy<A, Iterator>(v)
{}
};
*/
}}} //namespace boost { namespace container { namespace container_detail {
#endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING

View File

@@ -0,0 +1,134 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.10"
Name="bench_insert_vs_emplace"
ProjectGUID="{51EC9223-B8A9-4F6E-EA24-352DFA76C72A}"
Keyword="Win32Proj">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="../../Bin/Win32/Debug"
IntermediateDirectory="Debug/bench_insert_vs_emplace"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../../../.."
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="TRUE"
ExceptionHandling="TRUE"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
TreatWChar_tAsBuiltInType="TRUE"
ForceConformanceInForLoopScope="FALSE"
UsePrecompiledHeader="0"
WarningLevel="4"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="winmm.lib"
OutputFile="$(OutDir)/bench_insert_vs_emplace_d.exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../../../stage/lib"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile="$(OutDir)/bench_insert_vs_emplace.pdb"
SubSystem="1"
TargetMachine="1"
FixedBaseAddress="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="../../Bin/Win32/Release"
IntermediateDirectory="Release/bench_insert_vs_emplace"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../../../.."
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
TreatWChar_tAsBuiltInType="TRUE"
ForceConformanceInForLoopScope="FALSE"
UsePrecompiledHeader="0"
WarningLevel="4"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="0"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="winmm.lib"
OutputFile="$(OutDir)/bench_insert_vs_emplace.exe"
LinkIncremental="1"
AdditionalLibraryDirectories="../../../../stage/lib"
GenerateDebugInformation="TRUE"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4CBB737A-CAC8-16C6-1243-3D2AA35F127D}">
<File
RelativePath="..\..\bench\bench_insert_vs_emplace.cpp">
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>