perf: avoid std::map temporaries in WebContents::DevToolsRequestFileSystems() (#46268)

* perf: move the GetDevToolsWebContents() call outside of the loop

* perf: remove std::map temporary in WebContents::DevToolsRequestFileSystems()

* refactor: remove unused GetAddedFileSystemPaths()

* perf: remove std::vector temporary in WebContents::DevToolsRequestFileSystems()
This commit is contained in:
Charles Kerr 2025-03-26 16:26:56 -05:00 committed by GitHub
parent 3212f60d69
commit 3ae285b614
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -682,17 +682,6 @@ PrefService* GetPrefService(content::WebContents* web_contents) {
return GetPrefService(web_contents)->GetDict(prefs::kDevToolsFileSystemPaths);
}
std::map<std::string, std::string> GetAddedFileSystemPaths(
content::WebContents* web_contents) {
std::map<std::string, std::string> 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);
@ -4060,31 +4049,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<FileSystem> 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(