
That is because you are most likely using the inefficient creation style, probably like this: shared_ptr<myClass> myPtr(new myClass(arg1,arg2)); Which will also new a struct inside the shared_ptr that holds that pointer and bookkeeping information. If you have read the shared_ptr documentation, the efficient creation style is this: shared_ptr<myClass> myPtr = make_shared<myClass>(arg1,arg2); Which will new both your class and the bookkeeping information in the same place in memory, increasing both the speed of creation (only one new), and speed of accessing (better cache coherency). When creating your objects at the same time (literally) as the shared_ptr, that should fix most of those speed issues.
That's not the point, make_shared can be used only when the object is
created,but what I said is as follows:
class Session
: public boost::enable_shared_from_this<Session> {
public:
static void HandleRead(boost::weak_ptr<Session> session,
const boost::system::error_code& error,
size_t bytes_transferred) {
if (session.expired())
return;
...
}
void Read() {
// this line is very slow,
// because boost::bind(&Session::HandleRead,shared_from_this(), ...)
// causes extra new/delete operations
socket_.async_read_some(boost::asio::buffer(...),
boost::bind(&Session::HandleRead,
shared_from_this(),
_1, _2));
...
}
void Close() {
socket_.close();
}
private:
boost::asio::ip::tcp::socket socket_;
};
std::set