From 3023f14bddda07300e7aa05d32d48f1e68611b58 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Wed, 26 Mar 2025 19:25:59 -0500 Subject: [PATCH] perf: avoid `std::map` temporaries in `WebContents::DevToolsRequestFileSystems()` (#46308) * perf: move the GetDevToolsWebContents() call outside of the loop Co-authored-by: Charles Kerr * perf: remove std::map temporary in WebContents::DevToolsRequestFileSystems() Co-authored-by: Charles Kerr * refactor: remove unused GetAddedFileSystemPaths() Co-authored-by: Charles Kerr * perf: remove std::vector temporary in WebContents::DevToolsRequestFileSystems() Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- .../browser/api/electron_api_web_contents.cc | 46 ++++++------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 0ce10289a286..eaca13d3332f 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -682,17 +682,6 @@ PrefService* GetPrefService(content::WebContents* web_contents) { return GetPrefService(web_contents)->GetDict(prefs::kDevToolsFileSystemPaths); } -std::map GetAddedFileSystemPaths( - content::WebContents* web_contents) { - std::map result; - for (auto it : GetAddedFileSystems(web_contents)) { - std::string type = - it.second.is_string() ? it.second.GetString() : std::string(); - result[it.first] = type; - } - return result; -} - bool IsDevToolsFileSystemAdded(content::WebContents* web_contents, const std::string_view file_system_path) { return GetAddedFileSystems(web_contents).contains(file_system_path); @@ -4147,31 +4136,22 @@ void WebContents::DevToolsAppendToFile(const std::string& url, } void WebContents::DevToolsRequestFileSystems() { - auto file_system_paths = GetAddedFileSystemPaths(GetDevToolsWebContents()); - if (file_system_paths.empty()) { - inspectable_web_contents_->CallClientFunction( - "DevToolsAPI", "fileSystemsLoaded", base::Value(base::Value::List())); - return; + const std::string empty_str; + content::WebContents* const dtwc = GetDevToolsWebContents(); + const base::Value::Dict& added_paths = GetAddedFileSystems(dtwc); + + auto filesystems = base::Value::List::with_capacity(added_paths.size()); + for (const auto path_and_type : added_paths) { + const auto& [path, type_val] = path_and_type; + const auto& type = type_val.is_string() ? type_val.GetString() : empty_str; + const std::string file_system_id = + RegisterFileSystem(dtwc, base::FilePath::FromUTF8Unsafe(path)); + filesystems.Append(CreateFileSystemValue( + CreateFileSystemStruct(dtwc, file_system_id, path, type))); } - std::vector file_systems; - for (const auto& file_system_path : file_system_paths) { - base::FilePath path = - base::FilePath::FromUTF8Unsafe(file_system_path.first); - std::string file_system_id = - RegisterFileSystem(GetDevToolsWebContents(), path); - FileSystem file_system = - CreateFileSystemStruct(GetDevToolsWebContents(), file_system_id, - file_system_path.first, file_system_path.second); - file_systems.push_back(file_system); - } - - base::Value::List file_system_value; - for (const auto& file_system : file_systems) - file_system_value.Append(CreateFileSystemValue(file_system)); inspectable_web_contents_->CallClientFunction( - "DevToolsAPI", "fileSystemsLoaded", - base::Value(std::move(file_system_value))); + "DevToolsAPI", "fileSystemsLoaded", base::Value{std::move(filesystems)}); } void WebContents::DevToolsAddFileSystem(