]> git.sesse.net Git - cubemap/blobdiff - server.h
Send data using sendfile() instead of write(). Seems to be something like 30% lower...
[cubemap] / server.h
index 8d5261f6ca9b42fb84bab11776486cd44a67f125..e9b74315f1b3d5fdd6227d67c2c4160ccf89be58 100644 (file)
--- a/server.h
+++ b/server.h
@@ -81,15 +81,22 @@ struct Stream {
 
        // Serialization/deserialization.
        Stream(const StreamProto &serialized);
-       StreamProto serialize() const;
+       StreamProto serialize();
 
        std::string stream_id;
 
        // The HTTP response header, plus the video stream header (if any).
        std::string header;
 
-       // The stream data itself, stored in a circular buffer.
-       char *data;
+       // The stream data itself, stored in a circular buffer.q
+       //
+       // We store our data in a file, so that we can send the data to the
+       // kernel only once (with write()). We then use sendfile() for each
+       // client, which effectively zero-copies it out of the kernel's buffer
+       // cache. This is significantly more efficient than doing write() from
+       // a userspace memory buffer, since the latter makes the kernel copy
+       // the same data from userspace many times.
+       int data_fd;
 
        // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
        // since the buffer wraps.
@@ -217,7 +224,7 @@ private:
        void process_queued_data();
 
        void add_client(int sock);
-       void add_data(const std::string &stream_id, const char *data, size_t bytes);
+       void add_data(const std::string &stream_id, const char *data, ssize_t bytes);
 };
 
 #endif  // !defined(_SERVER_H)