From f6301714dbabd1a97407d1e482b920143f7e13c3 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 26 Mar 2017 21:14:34 +0900 Subject: [PATCH] nghttpx: Avoid copy of std::mt19937 which is huge --- src/shrpx.cc | 3 +-- src/shrpx_connection_handler.cc | 8 ++------ src/shrpx_worker.cc | 5 ++--- src/shrpx_worker.h | 3 +-- src/shrpx_worker_process.cc | 3 +-- src/util.cc | 5 ++++- src/util.h | 4 ++-- 7 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 8d192acf..ccbdf6bf 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -2908,8 +2908,7 @@ int process_options(Config *config, auto iov = make_byte_ref(config->balloc, SHRPX_OBFUSCATED_NODE_LENGTH + 2); auto p = iov.base; *p++ = '_'; - std::random_device rd; - auto gen = util::make_mt19937(rd); + auto gen = util::make_mt19937(); p = util::random_alpha_digit(p, p + SHRPX_OBFUSCATED_NODE_LENGTH, gen); *p = '\0'; fwdconf.by_obfuscated = StringRef{iov.base, p}; diff --git a/src/shrpx_connection_handler.cc b/src/shrpx_connection_handler.cc index 29d67ca0..0de4e4c3 100644 --- a/src/shrpx_connection_handler.cc +++ b/src/shrpx_connection_handler.cc @@ -232,11 +232,9 @@ int ConnectionHandler::create_single_worker() { all_ssl_ctx_.push_back(session_cache_ssl_ctx); } - std::random_device rd; - single_worker_ = make_unique( loop_, sv_ssl_ctx, cl_ssl_ctx, session_cache_ssl_ctx, cert_tree_.get(), - ticket_keys_, this, config->conn.downstream, util::make_mt19937(rd)); + ticket_keys_, this, config->conn.downstream); #ifdef HAVE_MRUBY if (single_worker_->create_mruby_context() != 0) { return -1; @@ -278,8 +276,6 @@ int ConnectionHandler::create_worker_thread(size_t num) { ++num; } - std::random_device rd; - for (size_t i = 0; i < num; ++i) { auto loop = ev_loop_new(config->ev_loop_flags); @@ -295,7 +291,7 @@ int ConnectionHandler::create_worker_thread(size_t num) { } auto worker = make_unique( loop, sv_ssl_ctx, cl_ssl_ctx, session_cache_ssl_ctx, cert_tree_.get(), - ticket_keys_, this, config->conn.downstream, util::make_mt19937(rd)); + ticket_keys_, this, config->conn.downstream); #ifdef HAVE_MRUBY if (worker->create_mruby_context() != 0) { return -1; diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index 74b30c5a..87ae96e4 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -114,9 +114,8 @@ Worker::Worker(struct ev_loop *loop, SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx, ssl::CertLookupTree *cert_tree, const std::shared_ptr &ticket_keys, ConnectionHandler *conn_handler, - std::shared_ptr downstreamconf, - std::mt19937 randgen) - : randgen_(std::move(randgen)), + std::shared_ptr downstreamconf) + : randgen_(util::make_mt19937()), worker_stat_{}, dns_tracker_(loop), loop_(loop), diff --git a/src/shrpx_worker.h b/src/shrpx_worker.h index 8c94cdc2..9b77eb5b 100644 --- a/src/shrpx_worker.h +++ b/src/shrpx_worker.h @@ -223,8 +223,7 @@ public: ssl::CertLookupTree *cert_tree, const std::shared_ptr &ticket_keys, ConnectionHandler *conn_handler, - std::shared_ptr downstreamconf, - std::mt19937 randgen); + std::shared_ptr downstreamconf); ~Worker(); void run_async(); void wait(); diff --git a/src/shrpx_worker_process.cc b/src/shrpx_worker_process.cc index 2e1aa708..aa335318 100644 --- a/src/shrpx_worker_process.cc +++ b/src/shrpx_worker_process.cc @@ -412,8 +412,7 @@ int worker_process_event_loop(WorkerProcessConfig *wpconf) { auto loop = EV_DEFAULT; - std::random_device rd; - auto gen = util::make_mt19937(rd); + auto gen = util::make_mt19937(); ConnectionHandler conn_handler(loop, gen); diff --git a/src/util.cc b/src/util.cc index e31af042..0adb6727 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1452,7 +1452,10 @@ StringRef extract_host(const StringRef &hostport) { return StringRef{std::begin(hostport), p}; } -std::mt19937 make_mt19937(std::random_device &rd) { return std::mt19937(rd()); } +std::mt19937 make_mt19937() { + std::random_device rd; + return std::mt19937(rd()); +} } // namespace util diff --git a/src/util.h b/src/util.h index 123fd5df..49d0c4b1 100644 --- a/src/util.h +++ b/src/util.h @@ -744,8 +744,8 @@ int sha256(uint8_t *buf, const StringRef &s); // NULL-terminated. StringRef extract_host(const StringRef &hostport); -// Returns new std::mt19937 object, seeded by |rd|. -std::mt19937 make_mt19937(std::random_device &rd); +// Returns new std::mt19937 object. +std::mt19937 make_mt19937(); } // namespace util