]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Replace map with unordered_map nearly everywhere, for speed.
[cubemap] / server.cpp
index e9bc654eb1a3cbe4d667f3bafef6119aabec01fa..be583e490d5223358d5aef68c8b826654b06e51d 100644 (file)
@@ -1,6 +1,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <pthread.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -300,7 +301,7 @@ void Server::add_client_from_serialized(const ClientProto &client, const vector<
 
 int Server::lookup_stream_by_url(const string &url) const
 {
-       map<string, int>::const_iterator stream_url_it = stream_url_map.find(url);
+       const auto stream_url_it = stream_url_map.find(url);
        if (stream_url_it == stream_url_map.end()) {
                return -1;
        }
@@ -909,12 +910,12 @@ int Server::parse_request(Client *client)
 
        // Parse the headers, for logging purposes.
        // TODO: Case-insensitivity.
-       multimap<string, string> headers = extract_headers(lines, client->remote_addr);
-       multimap<string, string>::const_iterator referer_it = headers.find("Referer");
+       unordered_multimap<string, string> headers = extract_headers(lines, client->remote_addr);
+       const auto referer_it = headers.find("Referer");
        if (referer_it != headers.end()) {
                client->referer = referer_it->second;
        }
-       multimap<string, string>::const_iterator user_agent_it = headers.find("User-Agent");
+       const auto user_agent_it = headers.find("User-Agent");
        if (user_agent_it != headers.end()) {
                client->user_agent = user_agent_it->second;
        }
@@ -984,25 +985,25 @@ int Server::parse_request(Client *client)
                client->close_after_response = true;
                client->http_11 = false;
        } else {
-               multimap<string, string>::const_iterator connection_it = headers.find("Connection");
+               const auto connection_it = headers.find("Connection");
                if (connection_it != headers.end() && connection_it->second == "close") {
                        client->close_after_response = true;
                }
        }
 
-       map<string, int>::const_iterator stream_url_map_it = stream_url_map.find(url);
+       const auto stream_url_map_it = stream_url_map.find(url);
        if (stream_url_map_it != stream_url_map.end()) {
                // Serve a regular stream..
                client->stream = streams[stream_url_map_it->second].get();
                client->serving_hls_playlist = false;
        } else {
-               map<string, int>::const_iterator stream_hls_url_map_it = stream_hls_url_map.find(url);
+               const auto stream_hls_url_map_it = stream_hls_url_map.find(url);
                if (stream_hls_url_map_it != stream_hls_url_map.end()) {
                        // Serve HLS playlist.
                        client->stream = streams[stream_hls_url_map_it->second].get();
                        client->serving_hls_playlist = true;
                } else {
-                       map<string, string>::const_iterator ping_url_map_it = ping_url_map.find(url);
+                       const auto ping_url_map_it = ping_url_map.find(url);
                        if (ping_url_map_it == ping_url_map.end()) {
                                return 404;  // Not found.
                        } else {
@@ -1165,7 +1166,7 @@ void Server::construct_hls_playlist(Client *client)
 
 void Server::construct_204(Client *client)
 {
-       map<string, string>::const_iterator ping_url_map_it = ping_url_map.find(client->url);
+       const auto ping_url_map_it = ping_url_map.find(client->url);
        assert(ping_url_map_it != ping_url_map.end());
 
        string response;
@@ -1247,6 +1248,14 @@ bool Server::more_requests(Client *client)
        // Log to access_log.
        access_log->write(client->get_stats());
 
+       // Flush pending data; does not cancel out TCP_CORK (since that still takes priority),
+       // but does a one-off flush.
+       int one = 1;
+       if (setsockopt(client->sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one)) == -1) {
+               log_perror("setsockopt(TCP_NODELAY)");
+               // Can still continue.
+       }
+
        // Switch states and reset the parsers. We don't reset statistics.
        client->state = Client::READING_REQUEST;
        client->url.clear();