From a117e7b31664ec99212844bb3d280d83645ea1d6 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 24 Sep 2017 16:41:37 -0400 Subject: [PATCH] src: use pass by reference in module_wrap.cc This commit changes several pass by value arguments to pass by reference. This change was motivated by the following Coverity reports: ** CID 177292: Performance inefficiencies (PASS_BY_VALUE) /src/module_wrap.cc: 326 in node::loader:::: check_file(node::url::URL, bool, bool)() ** CID 177293: Performance inefficiencies (PASS_BY_VALUE) /src/module_wrap.cc: 352 in node::loader:::: resolve_extensions(node::url::URL, bool)() *** CID 177290: Performance inefficiencies (PASS_BY_VALUE) /src/module_wrap.cc: 368 in node::loader:::: resolve_index(node::url::URL)() *** CID 177289: Performance inefficiencies (PASS_BY_VALUE) /src/module_wrap.cc: 371 in node::loader:::: resolve_main(node::url::URL)() *** CID 177291: Performance inefficiencies (PASS_BY_VALUE) /src/module_wrap.cc: 430 in node::loader:::: resolve_directory(node::url::URL, bool)() --- src/module_wrap.cc | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 73d248c373ea00..a4afde94bacef8 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -323,12 +323,12 @@ struct file_check { bool failed = true; uv_file file = -1; }; -inline const struct file_check check_file(URL search, +inline const struct file_check check_file(URL* search, bool close = false, bool allow_dir = false) { struct file_check ret; uv_fs_t fs_req; - std::string path = search.ToFilePath(); + std::string path = search->ToFilePath(); if (path.empty()) { return ret; } @@ -349,28 +349,29 @@ inline const struct file_check check_file(URL search, if (close) uv_fs_close(nullptr, &fs_req, fd, nullptr); return ret; } -URL resolve_extensions(URL search, bool check_exact = true) { +URL resolve_extensions(URL* search, bool check_exact = true) { if (check_exact) { auto check = check_file(search, true); if (!check.failed) { - return search; + return *search; } } for (auto extension : EXTENSIONS) { - URL guess(search.path() + extension, &search); - auto check = check_file(guess, true); + URL guess(search->path() + extension, search); + auto check = check_file(&guess, true); if (!check.failed) { return guess; } } return URL(""); } -inline URL resolve_index(URL search) { - return resolve_extensions(URL("index", &search), false); +inline URL resolve_index(URL* search) { + URL index("index", search); + return resolve_extensions(&index, false); } -URL resolve_main(URL search) { - URL pkg("package.json", &search); - auto check = check_file(pkg); +URL resolve_main(URL* search) { + URL pkg("package.json", search); + auto check = check_file(&pkg); if (!check.failed) { auto iso = Isolate::GetCurrent(); auto ctx = iso->GetCurrentContext(); @@ -398,7 +399,7 @@ URL resolve_main(URL search) { if (!is_relative_or_absolute_path(main_std)) { main_std.insert(0, "./"); } - return Resolve(main_std, &search); + return Resolve(main_std, search); } return URL(""); } @@ -427,7 +428,7 @@ URL resolve_module(std::string specifier, URL* base) { return URL(""); } -URL resolve_directory(URL search, bool read_pkg_json) { +URL resolve_directory(URL* search, bool read_pkg_json) { if (read_pkg_json) { auto main = resolve_main(search); if (!(main.flags() & URL_FLAGS_FAILED)) return main; @@ -448,12 +449,12 @@ URL Resolve(std::string specifier, URL* base, bool read_pkg_json) { } if (is_relative_or_absolute_path(specifier)) { URL resolved(specifier, base); - auto file = resolve_extensions(resolved); + auto file = resolve_extensions(&resolved); if (!(file.flags() & URL_FLAGS_FAILED)) return file; if (specifier.back() != '/') { resolved = URL(specifier + "/", base); } - return resolve_directory(resolved, read_pkg_json); + return resolve_directory(&resolved, read_pkg_json); } else { return resolve_module(specifier, base); }