From 5ac0308af90c2ab9842682d06a720cfab11eb661 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Wed, 10 Apr 2019 10:07:25 -0400 Subject: [PATCH] tools: refactor mkcodecache PR-URL: https://github.com/nodejs/node/pull/27161 Reviewed-By: Joyee Cheung --- tools/code_cache/cache_builder.cc | 53 ++++++++++++------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/tools/code_cache/cache_builder.cc b/tools/code_cache/cache_builder.cc index 9ce4efa3a1ecb0..8d0fb26d7e7cb0 100644 --- a/tools/code_cache/cache_builder.cc +++ b/tools/code_cache/cache_builder.cc @@ -1,12 +1,12 @@ #include "cache_builder.h" +#include "node_native_module.h" +#include "util.h" + #include #include #include #include #include -#include "util.h" - -#include "node_native_module.h" namespace node { namespace native_module { @@ -55,25 +55,19 @@ static std::string GetDefinition(const std::string& id, return ss.str(); } -static std::string GetInitializer(const std::string& id) { +static void GetInitializer(const std::string& id, std::stringstream& ss) { std::string def_name = GetDefName(id); - char buf[256] = {0}; - snprintf(buf, - sizeof(buf), - "code_cache->emplace(\n" - " \"%s\",\n" - " std::make_unique" - "(%s, static_cast(arraysize(%s)), policy)\n" - ");", - id.c_str(), - def_name.c_str(), - def_name.c_str()); - return buf; + ss << " code_cache.emplace(\n"; + ss << " \"" << id << "\",\n"; + ss << " std::make_unique(\n"; + ss << " " << def_name << ",\n"; + ss << " static_cast(arraysize(" << def_name << ")), policy\n"; + ss << " )\n"; + ss << " );"; } static std::string GenerateCodeCache( - std::map data, - std::vector ids, + const std::map& data, bool log_progress) { std::stringstream ss; ss << R"(#include @@ -101,20 +95,19 @@ namespace native_module { } ss << R"(void NativeModuleEnv::InitializeCodeCache() { - NativeModuleCacheMap* code_cache = - NativeModuleLoader::GetInstance()->code_cache(); - if (!code_cache->empty()) { - return; - } + NativeModuleCacheMap& code_cache = + *NativeModuleLoader::GetInstance()->code_cache(); + CHECK(code_cache.empty()); auto policy = v8::ScriptCompiler::CachedData::BufferPolicy::BufferNotOwned; )"; for (const auto& x : data) { - const std::string& id = x.first; - ss << GetInitializer(id) << "\n\n"; + GetInitializer(x.first, ss); + ss << "\n\n"; } - ss << R"(} + ss << R"( +} } // namespace native_module } // namespace node @@ -126,19 +119,15 @@ std::string CodeCacheBuilder::Generate(Local context) { NativeModuleLoader* loader = NativeModuleLoader::GetInstance(); std::vector ids = loader->GetModuleIds(); - std::vector modules; - modules.reserve(ids.size()); - std::map data; - NativeModuleLoader::Result result; for (const auto& id : ids) { // TODO(joyeecheung): we can only compile the modules that can be // required here because the parameters for other types of builtins // are still very flexible. We should look into auto-generating // the paramters from the source somehow. if (loader->CanBeRequired(id.c_str())) { - modules.push_back(id); + NativeModuleLoader::Result result; USE(loader->CompileAsModule(context, id.c_str(), &result)); ScriptCompiler::CachedData* cached_data = loader->GetCodeCache(id.c_str()); @@ -158,7 +147,7 @@ std::string CodeCacheBuilder::Generate(Local context) { if (ret == 0 && strcmp(env_buf, "mkcodecache") == 0) { log_progress = true; } - return GenerateCodeCache(data, modules, log_progress); + return GenerateCodeCache(data, log_progress); } } // namespace native_module