From 6195d747ce33d40cd85057f3a572be73e882ada9 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 24 Aug 2018 23:11:21 +0900 Subject: [PATCH] nghttpx: Share mruby context if it is compiled from same file --- src/shrpx_worker.cc | 14 ++++++++++++-- src/shrpx_worker.h | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index 8fa5ba0e..89c55e95 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -181,6 +181,10 @@ void Worker::replace_downstream_config( std::vector>(groups.size()); std::map addr_groups_indexer; + // TODO It is a bit less efficient because + // mruby::create_mruby_context returns std::unique_ptr and we cannot + // use std::make_shared. + std::map> shared_mruby_ctxs; for (size_t i = 0; i < groups.size(); ++i) { auto &src = groups[i]; @@ -190,8 +194,14 @@ void Worker::replace_downstream_config( dst->pattern = ImmutableString{std::begin(src.pattern), std::end(src.pattern)}; #ifdef HAVE_MRUBY - dst->mruby_ctx = mruby::create_mruby_context(src.mruby_file); - assert(dst->mruby_ctx); + auto mruby_ctx_it = shared_mruby_ctxs.find(src.mruby_file); + if (mruby_ctx_it == std::end(shared_mruby_ctxs)) { + dst->mruby_ctx = mruby::create_mruby_context(src.mruby_file); + assert(dst->mruby_ctx); + shared_mruby_ctxs.emplace(src.mruby_file, dst->mruby_ctx); + } else { + dst->mruby_ctx = (*mruby_ctx_it).second; + } #endif // HAVE_MRUBY auto shared_addr = std::make_shared(); diff --git a/src/shrpx_worker.h b/src/shrpx_worker.h index 2515166b..e6deaa58 100644 --- a/src/shrpx_worker.h +++ b/src/shrpx_worker.h @@ -194,7 +194,7 @@ struct DownstreamAddrGroup { ImmutableString pattern; std::shared_ptr shared_addr; #ifdef HAVE_MRUBY - std::unique_ptr mruby_ctx; + std::shared_ptr mruby_ctx; #endif // HAVE_MRUBY // true if this group is no longer used for new request. If this is // true, the connection made using one of address in shared_addr