eidheim / Simple-WebSocket-Server

A very simple, fast, multithreaded, platform independent WebSocket (WS) and WebSocket Secure (WSS) server and client library implemented using C++11, Boost.Asio and OpenSSL. Created to be an easy way to make WebSocket endpoints in C++.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

why not using std::move in SendData?

fndisme opened this issue · comments

class SendData {
public:
SendData(std::shared_ptr header_stream, std::shared_ptr message_stream,
const std::function<void(const boost::system::error_code)> &callback) :
# header_stream(std::move(header_stream)), message_stream(std::move(message_stream)), callback(callback) {}
std::shared_ptr header_stream;
std::shared_ptr message_stream;
std::function<void(const boost::system::error_code)> callback;
};
I think using std::move has better performance? some post using lambda maybe using std::move bind some variable maybe more better?

Thank you, the header_stream could actually be std::unique_ptr instead of std::shared_ptr, and move would then be needed. Although, if move is used on message_stream, one would not be able to reuse the SendStream object when calling SocketServer::send, if that is what the user of the library wants.

I think it's safe for user to call send with move. for example
we has std::shared_ptr<message_stream> stream;
I call send(stream); // we can use move in send to build SendData
it's safe to call stream->
I call send(std::move(stream)); // we can use move in send to build SendData
it's undefined to call stream-> // but we know it's not ok

SimpleWebsocketServer is very easy to use. I use it in my company application, so I wish it runs more faster and stronger.
Thinks

You are right, it should be safe to use std::move in this case. One concern I have though, is that performing a move is not thread safe, while copying a shared_ptr is thread safe. However, we could do this:

diff --git a/server_ws.hpp b/server_ws.hpp
index d7e4854..4f0c554 100644
--- a/server_ws.hpp
+++ b/server_ws.hpp
@@ -56,7 +56,7 @@ namespace SimpleWeb {

             class SendData {
             public:
-                SendData(std::shared_ptr<SendStream> header_stream, std::shared_ptr<SendStream> message_stream,
+                SendData(const std::shared_ptr<SendStream> &header_stream, const std::shared_ptr<SendStream> &message_stream,
                         const std::function<void(const boost::system::error_code)> &callback) :
                         header_stream(header_stream), message_stream(message_stream), callback(callback) {}
                 std::shared_ptr<SendStream> header_stream;

What do you think?

When I think about it, all the functions that have shared_ptr as parameters should be changed to const & parameters to avoid unnecessary shared_ptr copies (http://stackoverflow.com/questions/8385457/should-i-pass-a-shared-ptr-by-reference).

Yes, I agree with you, You are right. use const shared_ptr& is the best one.

O(∩_∩)O, i can safe close this issue.