From: Steinar H. Gunderson Date: Sat, 7 Apr 2018 09:14:51 +0000 (+0200) Subject: Replace map with unordered_map nearly everywhere, for speed. X-Git-Tag: 1.4.0~22 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=58dd753c464d917dc446e2cbb4c01fd750d4eb87 Replace map with unordered_map nearly everywhere, for speed. --- diff --git a/config.cpp b/config.cpp index 5a1fdd9..21e17c9 100644 --- a/config.cpp +++ b/config.cpp @@ -7,9 +7,9 @@ #include #include #include -#include #include #include +#include #include #include "tlse.h" @@ -26,7 +26,7 @@ using namespace std; struct ConfigLine { string keyword; vector arguments; - map parameters; + unordered_map parameters; }; namespace { @@ -211,7 +211,7 @@ bool load_file_to_string(const string &filename, size_t max_size, string *conten return true; } -bool parse_tls_parameters(const map ¶meters, AcceptorConfig *acceptor) +bool parse_tls_parameters(const unordered_map ¶meters, AcceptorConfig *acceptor) { bool has_cert = false, has_key = false; diff --git a/httpinput.cpp b/httpinput.cpp index b969596..7967c76 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -249,7 +249,7 @@ bool HTTPInput::parse_response(const string &request) return false; } - multimap parameters = extract_headers(lines, url); + unordered_multimap parameters = extract_headers(lines, url); // Remove “Content-encoding: metacube”. // TODO: Make case-insensitive. diff --git a/main.cpp b/main.cpp index fc36861..7da6650 100644 --- a/main.cpp +++ b/main.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -445,7 +446,7 @@ start: serialize_start.tv_nsec = loaded_state.serialize_start_usec() * 1000ull; // Deserialize the streams. - map stream_headers_for_url; // See below. + unordered_map stream_headers_for_url; // See below. for (const StreamProto &stream : loaded_state.streams()) { if (all_urls.count(stream.url()) == 0) { // Delete the stream backlogs. diff --git a/parse.cpp b/parse.cpp index f4c78fc..bec742b 100644 --- a/parse.cpp +++ b/parse.cpp @@ -56,9 +56,9 @@ vector split_lines(const string &str) return ret; } -multimap extract_headers(const vector &lines, const string &log_context) +unordered_multimap extract_headers(const vector &lines, const string &log_context) { - multimap parameters; + unordered_multimap parameters; for (size_t i = 1; i < lines.size(); ++i) { size_t split = lines[i].find(":"); if (split == string::npos) { diff --git a/parse.h b/parse.h index a56b1f4..1924cc0 100644 --- a/parse.h +++ b/parse.h @@ -4,8 +4,8 @@ // Various routines that deal with parsing; both HTTP requests and more generic text. #include -#include #include +#include #include // Split a line on whitespace, e.g. "foo bar baz" -> {"foo", "bar", "baz"}. @@ -16,7 +16,7 @@ std::vector split_lines(const std::string &str); // Extract HTTP headers from a request or response. Ignores the first line, // where the verb or the return code is. -std::multimap extract_headers( +std::unordered_multimap extract_headers( const std::vector &lines, const std::string &log_context); // Add the new data to an existing string, looking for \r\n\r\n diff --git a/server.cpp b/server.cpp index a7f579c..be583e4 100644 --- a/server.cpp +++ b/server.cpp @@ -301,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::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; } @@ -910,12 +910,12 @@ int Server::parse_request(Client *client) // Parse the headers, for logging purposes. // TODO: Case-insensitivity. - multimap headers = extract_headers(lines, client->remote_addr); - multimap::const_iterator referer_it = headers.find("Referer"); + unordered_multimap 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::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; } @@ -985,25 +985,25 @@ int Server::parse_request(Client *client) client->close_after_response = true; client->http_11 = false; } else { - multimap::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::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::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::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 { @@ -1166,7 +1166,7 @@ void Server::construct_hls_playlist(Client *client) void Server::construct_204(Client *client) { - map::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; diff --git a/server.h b/server.h index da20764..4ec908b 100644 --- a/server.h +++ b/server.h @@ -6,11 +6,11 @@ #include #include #include -#include #include #include #include #include +#include #include #include "tlse.h" @@ -106,13 +106,13 @@ private: std::vector> streams; // Map from URL to index into . - std::map stream_url_map, stream_hls_url_map; + std::unordered_map stream_url_map, stream_hls_url_map; // Map from URL to CORS Allow-Origin header (or empty string). - std::map ping_url_map; + std::unordered_map ping_url_map; // Map from file descriptor to client. - std::map clients; + std::unordered_map clients; // A list of all clients, ordered by the time they connected (first element), // and their file descriptor (second element). It is ordered by connection time @@ -132,7 +132,7 @@ private: epoll_event events[EPOLL_MAX_EVENTS]; // For each TLS-enabled acceptor, our private server context for its key pair. - std::map tls_server_contexts; + std::unordered_map tls_server_contexts; // The actual worker thread. virtual void do_work();