Use unique_ptr instead of shared_ptr in our any implementation, >10% speed improvement

This commit is contained in:
Jason Turner 2012-07-09 16:20:55 -06:00
parent a6924bcc9e
commit f27739cf71

View File

@ -44,7 +44,7 @@ namespace chaiscript {
{ {
virtual void *data() = 0; virtual void *data() = 0;
virtual const std::type_info &type() const = 0; virtual const std::type_info &type() const = 0;
virtual std::shared_ptr<Data> clone() const = 0; virtual std::unique_ptr<Data> clone() const = 0;
}; };
template<typename T> template<typename T>
@ -66,16 +66,16 @@ namespace chaiscript {
return m_type; return m_type;
} }
std::shared_ptr<Data> clone() const std::unique_ptr<Data> clone() const
{ {
return std::shared_ptr<Data>(new Data_Impl<T>(m_data)); return std::unique_ptr<Data>(new Data_Impl<T>(m_data));
} }
const std::type_info &m_type; const std::type_info &m_type;
T m_data; T m_data;
}; };
std::shared_ptr<Data> m_data; std::unique_ptr<Data> m_data;
public: public:
// construct/copy/destruct // construct/copy/destruct
@ -94,7 +94,7 @@ namespace chaiscript {
template<typename ValueType> template<typename ValueType>
Any(const ValueType &t_value) Any(const ValueType &t_value)
{ {
m_data = std::shared_ptr<Data>(new Data_Impl<ValueType>(t_value)); m_data = std::unique_ptr<Data>(new Data_Impl<ValueType>(t_value));
} }
Any & operator=(const Any &t_any) Any & operator=(const Any &t_any)
@ -131,9 +131,7 @@ namespace chaiscript {
// modifiers // modifiers
Any & swap(Any &t_other) Any & swap(Any &t_other)
{ {
std::shared_ptr<Data> data = t_other.m_data; std::swap(m_data, t_other.m_data);
t_other.m_data = m_data;
m_data = data;
return *this; return *this;
} }