Skip to content

Commit

Permalink
perf: use flat_set, flat_map for small, trivially-moved containers (#…
Browse files Browse the repository at this point in the history
…40817)

* refactor: use base::flat_map in ElectronMenuModel

* refactor: use base::flat_map in BuildSubmenuFromModel()

* refactor: use base::flat_map in GetDialogsMap()

* refactor: use base::flat_map in DesktopCapturer

* refactor: use base::flat_map, flat_set in ElectronBrowserClient

* refactor: use base::flat_map in ProxyingURLLoaderFactory

* refactor: use base::flat_map in MapToCommonId()

* refactor: use base::flat_map for g_map_id

* refactor: use base::flat_map for ViewsDelegate::AppbarAutohideEdgeMap

* refactor: use base::flat_map for App::app_metrics_

* refactor: use base::flat_map for PowerSaveBlocker::wake_lock_types_

* refactor: use base::flat_map for NativeImage::hicons_

* refactor: use base::flat_map for MenuViews::menu_runners_

* refactor: use base::flat_map for WebViewManager::web_contents_embedder_map_

* refactor: use base::flat_map for InspectableWebContents::extensions_api_

* refactor: use base::flat_set for libnotify GetServerCapabilities()

* refactor: use base::flat_set for InspectableWebContents::loaders_

* refactor: use base::flat_set for ElectronRendererClient::environments_

refactor: use base::flat_set for ElectronRendererClient::injected_frames_

* refactor: use base::flat_set for WebWorkerObserver::environments_
  • Loading branch information
ckerr committed Jan 5, 2024
1 parent 5086071 commit 22970f5
Show file tree
Hide file tree
Showing 23 changed files with 83 additions and 87 deletions.
7 changes: 3 additions & 4 deletions shell/browser/api/electron_api_app.h
Expand Up @@ -5,11 +5,11 @@
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_H_

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/task/cancelable_task_tracker.h"
#include "chrome/browser/icon_manager.h"
#include "chrome/browser/process_singleton.h"
Expand Down Expand Up @@ -259,9 +259,8 @@ class App : public ElectronBrowserClient::Delegate,

base::FilePath app_path_;

using ProcessMetricMap =
std::map<int, std::unique_ptr<electron::ProcessMetric>>;
ProcessMetricMap app_metrics_;
// pid -> electron::ProcessMetric
base::flat_map<int, std::unique_ptr<electron::ProcessMetric>> app_metrics_;

bool disable_hw_acceleration_ = false;
bool disable_domain_blocking_for_3DAPIs_ = false;
Expand Down
15 changes: 7 additions & 8 deletions shell/browser/api/electron_api_desktop_capturer.cc
Expand Up @@ -4,11 +4,11 @@

#include "shell/browser/api/electron_api_desktop_capturer.h"

#include <map>
#include <memory>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
Expand Down Expand Up @@ -42,11 +42,11 @@

#if BUILDFLAG(IS_LINUX)
// Private function in ui/base/x/x11_display_util.cc
std::map<x11::RandR::Output, int> GetMonitors(
base::flat_map<x11::RandR::Output, int> GetMonitors(
std::pair<uint32_t, uint32_t> version,
x11::RandR* randr,
x11::Window window) {
std::map<x11::RandR::Output, int> output_to_monitor;
base::flat_map<x11::RandR::Output, int> output_to_monitor;
if (version >= std::pair<uint32_t, uint32_t>{1, 5}) {
if (auto reply = randr->GetMonitors({window}).Sync()) {
for (size_t monitor = 0; monitor < reply->monitors.size(); monitor++) {
Expand Down Expand Up @@ -76,20 +76,20 @@ std::vector<uint8_t> GetEDIDProperty(x11::RandR* randr,
// Find the mapping from monitor name atom to the display identifier
// that the screen API uses. Based on the logic in BuildDisplaysFromXRandRInfo
// in ui/base/x/x11_display_util.cc
std::map<int32_t, uint32_t> MonitorAtomIdToDisplayId() {
base::flat_map<int32_t, uint32_t> MonitorAtomIdToDisplayId() {
auto* connection = x11::Connection::Get();
auto& randr = connection->randr();
auto x_root_window = ui::GetX11RootWindow();

std::map<int32_t, uint32_t> monitor_atom_to_display;
base::flat_map<int32_t, uint32_t> monitor_atom_to_display;

auto resources = randr.GetScreenResourcesCurrent({x_root_window}).Sync();
if (!resources) {
LOG(ERROR) << "XRandR returned no displays; don't know how to map ids";
return monitor_atom_to_display;
}

std::map<x11::RandR::Output, int> output_to_monitor =
const auto output_to_monitor =
GetMonitors(connection->randr_version(), &randr, x_root_window);
auto monitors_reply = randr.GetMonitors({x_root_window}).Sync();

Expand Down Expand Up @@ -385,8 +385,7 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
// display name atom and the display id is either the EDID or the
// loop index when that display was found (see
// BuildDisplaysFromXRandRInfo in ui/base/x/x11_display_util.cc)
std::map<int32_t, uint32_t> monitor_atom_to_display_id =
MonitorAtomIdToDisplayId();
const auto monitor_atom_to_display_id = MonitorAtomIdToDisplayId();
for (auto& source : screen_sources) {
auto display_id_iter =
monitor_atom_to_display_id.find(source.media_list_source.id.id);
Expand Down
28 changes: 15 additions & 13 deletions shell/browser/api/electron_api_menu_views.cc
Expand Up @@ -52,24 +52,26 @@ void MenuViews::PopupAt(BaseWindow* window,
auto close_callback = base::AdaptCallbackForRepeating(
base::BindOnce(&MenuViews::OnClosed, weak_factory_.GetWeakPtr(),
window_id, std::move(callback_with_ref)));
menu_runners_[window_id] =
auto& runner = menu_runners_[window_id] =
std::make_unique<MenuRunner>(model(), flags, std::move(close_callback));
menu_runners_[window_id]->RunMenuAt(
native_window->widget(), nullptr, gfx::Rect(location, gfx::Size()),
views::MenuAnchorPosition::kTopLeft, source_type);
runner->RunMenuAt(native_window->widget(), nullptr,
gfx::Rect{location, gfx::Size{}},
views::MenuAnchorPosition::kTopLeft, source_type);
}

void MenuViews::ClosePopupAt(int32_t window_id) {
auto runner = menu_runners_.find(window_id);
if (runner != menu_runners_.end()) {
if (auto iter = menu_runners_.find(window_id); iter != menu_runners_.end()) {
// Close the runner for the window.
runner->second->Cancel();
} else if (window_id == -1) {
// Or just close all opened runners.
for (auto it = menu_runners_.begin(); it != menu_runners_.end();) {
// The iterator is invalidated after the call.
(it++)->second->Cancel();
}
iter->second->Cancel();
return;
}

if (window_id == -1) {
// When -1 is passed in, close all opened runners.
// Note: `Cancel()` invalidaes iters, so move() to a temp before looping
auto tmp = std::move(menu_runners_);
for (auto& [id, runner] : tmp)
runner->Cancel();
}
}

Expand Down
4 changes: 2 additions & 2 deletions shell/browser/api/electron_api_menu_views.h
Expand Up @@ -5,9 +5,9 @@
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_MENU_VIEWS_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_MENU_VIEWS_H_

#include <map>
#include <memory>

#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "shell/browser/api/electron_api_menu.h"
#include "ui/display/screen.h"
Expand All @@ -33,7 +33,7 @@ class MenuViews : public Menu {
void OnClosed(int32_t window_id, base::OnceClosure callback);

// window ID -> open context menu
std::map<int32_t, std::unique_ptr<views::MenuRunner>> menu_runners_;
base::flat_map<int32_t, std::unique_ptr<views::MenuRunner>> menu_runners_;

base::WeakPtrFactory<MenuViews> weak_factory_{this};
};
Expand Down
6 changes: 2 additions & 4 deletions shell/browser/api/electron_api_power_save_blocker.h
Expand Up @@ -5,8 +5,7 @@
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_POWER_SAVE_BLOCKER_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_POWER_SAVE_BLOCKER_H_

#include <map>

#include "base/containers/flat_map.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
Expand Down Expand Up @@ -48,8 +47,7 @@ class PowerSaveBlocker : public gin::Wrappable<PowerSaveBlocker> {
bool is_wake_lock_active_ = false;

// Map from id to the corresponding blocker type for each request.
using WakeLockTypeMap = std::map<int, device::mojom::WakeLockType>;
WakeLockTypeMap wake_lock_types_;
base::flat_map<int, device::mojom::WakeLockType> wake_lock_types_;

mojo::Remote<device::mojom::WakeLock> wake_lock_;
};
Expand Down
4 changes: 2 additions & 2 deletions shell/browser/api/electron_api_system_preferences_mac.mm
Expand Up @@ -4,7 +4,6 @@

#include "shell/browser/api/electron_api_system_preferences.h"

#include <map>
#include <string>
#include <utility>

Expand All @@ -14,6 +13,7 @@
#import <Security/Security.h>

#include "base/apple/scoped_cftyperef.h"
#include "base/containers/flat_map.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
Expand Down Expand Up @@ -81,7 +81,7 @@ static bool FromV8(v8::Isolate* isolate,
int g_next_id = 0;

// The map to convert |id| to |int|.
std::map<int, id> g_id_map;
base::flat_map<int, id> g_id_map;

AVMediaType ParseMediaType(const std::string& media_type) {
if (media_type == "camera") {
Expand Down
8 changes: 4 additions & 4 deletions shell/browser/electron_browser_client.h
Expand Up @@ -5,12 +5,12 @@
#ifndef ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_CLIENT_H_
#define ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_CLIENT_H_

#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/synchronization/lock.h"
Expand Down Expand Up @@ -313,9 +313,9 @@ class ElectronBrowserClient : public content::ContentBrowserClient,
bool IsRendererSubFrame(int process_id) const;

// pending_render_process => web contents.
std::map<int, content::WebContents*> pending_processes_;
base::flat_map<int, content::WebContents*> pending_processes_;

std::set<int> renderer_is_subframe_;
base::flat_set<int> renderer_is_subframe_;

std::unique_ptr<PlatformNotificationService> notification_service_;
std::unique_ptr<NotificationPresenter> notification_presenter_;
Expand Down
6 changes: 3 additions & 3 deletions shell/browser/net/proxying_url_loader_factory.h
Expand Up @@ -6,12 +6,12 @@
#define ELECTRON_SHELL_BROWSER_NET_PROXYING_URL_LOADER_FACTORY_H_

#include <cstdint>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
Expand Down Expand Up @@ -274,11 +274,11 @@ class ProxyingURLLoaderFactory

// Mapping from our own internally generated request ID to an
// InProgressRequest instance.
std::map<uint64_t, std::unique_ptr<InProgressRequest>> requests_;
base::flat_map<uint64_t, std::unique_ptr<InProgressRequest>> requests_;

// A mapping from the network stack's notion of request ID to our own
// internally generated request ID for the same request.
std::map<int32_t, uint64_t> network_request_id_to_web_request_id_;
base::flat_map<int32_t, uint64_t> network_request_id_to_web_request_id_;

std::vector<std::string> ignore_connections_limit_domains_;
};
Expand Down
8 changes: 4 additions & 4 deletions shell/browser/notifications/linux/libnotify_notification.cc
Expand Up @@ -4,9 +4,9 @@

#include "shell/browser/notifications/linux/libnotify_notification.h"

#include <set>
#include <string>

#include "base/containers/flat_set.h"
#include "base/files/file_enumerator.h"
#include "base/functional/bind.h"
#include "base/logging.h"
Expand All @@ -24,8 +24,8 @@ namespace {

LibNotifyLoader libnotify_loader_;

const std::set<std::string>& GetServerCapabilities() {
static std::set<std::string> caps;
const base::flat_set<std::string>& GetServerCapabilities() {
static base::flat_set<std::string> caps;
if (caps.empty()) {
auto* capabilities = libnotify_loader_.notify_get_server_caps();
for (auto* l = capabilities; l != nullptr; l = l->next)
Expand All @@ -36,7 +36,7 @@ const std::set<std::string>& GetServerCapabilities() {
}

bool HasCapability(const std::string& capability) {
return GetServerCapabilities().count(capability) != 0;
return GetServerCapabilities().contains(capability);
}

bool NotifierSupportsActions() {
Expand Down
8 changes: 4 additions & 4 deletions shell/browser/ui/electron_menu_model.h
Expand Up @@ -5,10 +5,10 @@
#ifndef ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
#define ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_

#include <map>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
Expand Down Expand Up @@ -119,9 +119,9 @@ class ElectronMenuModel : public ui::SimpleMenuModel {
absl::optional<SharingItem> sharing_item_;
#endif

std::map<int, std::u16string> toolTips_; // command id -> tooltip
std::map<int, std::u16string> roles_; // command id -> role
std::map<int, std::u16string> sublabels_; // command id -> sublabel
base::flat_map<int, std::u16string> toolTips_; // command id -> tooltip
base::flat_map<int, std::u16string> roles_; // command id -> role
base::flat_map<int, std::u16string> sublabels_; // command id -> sublabel
base::ObserverList<Observer> observers_;

base::WeakPtrFactory<ElectronMenuModel> weak_factory_{this};
Expand Down
4 changes: 2 additions & 2 deletions shell/browser/ui/gtk/menu_util.cc
Expand Up @@ -7,9 +7,9 @@
#include <gdk/gdk.h>
#include <gtk/gtk.h>

#include <map>
#include <string>

#include "base/containers/flat_map.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "shell/browser/ui/gtk_util.h"
Expand Down Expand Up @@ -150,7 +150,7 @@ void BuildSubmenuFromModel(ui::MenuModel* model,
MenuActivatedCallback item_activated_cb,
bool* block_activation,
std::vector<ScopedGSignal>* signals) {
std::map<int, GtkWidget*> radio_groups;
base::flat_map<int, GtkWidget*> radio_groups;
GtkWidget* menu_item = nullptr;
for (size_t i = 0; i < model->GetItemCount(); ++i) {
std::string label = ui::ConvertAcceleratorsFromWindowsStyle(
Expand Down
7 changes: 3 additions & 4 deletions shell/browser/ui/inspectable_web_contents.cc
Expand Up @@ -203,11 +203,10 @@ class InspectableWebContents::NetworkResourceLoader
URLLoaderFactoryHolder url_loader_factory,
DispatchCallback callback,
base::TimeDelta retry_delay = base::TimeDelta()) {
auto resource_loader =
bindings->loaders_.insert(
std::make_unique<InspectableWebContents::NetworkResourceLoader>(
stream_id, bindings, resource_request, traffic_annotation,
std::move(url_loader_factory), std::move(callback), retry_delay);
bindings->loaders_.insert(std::move(resource_loader));
std::move(url_loader_factory), std::move(callback), retry_delay));
}

NetworkResourceLoader(
Expand Down Expand Up @@ -308,7 +307,7 @@ class InspectableWebContents::NetworkResourceLoader
std::move(callback_).Run(&response);
}

bindings_->loaders_.erase(bindings_->loaders_.find(this));
bindings_->loaders_.erase(this);
}

void OnRetry(base::OnceClosure start_retry) override {}
Expand Down
11 changes: 6 additions & 5 deletions shell/browser/ui/inspectable_web_contents.h
Expand Up @@ -6,11 +6,11 @@
#ifndef ELECTRON_SHELL_BROWSER_UI_INSPECTABLE_WEB_CONTENTS_H_
#define ELECTRON_SHELL_BROWSER_UI_INSPECTABLE_WEB_CONTENTS_H_

#include <map>
#include <memory>
#include <set>
#include <string>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/containers/span.h"
#include "base/containers/unique_ptr_adapters.h"
#include "base/memory/raw_ptr.h"
Expand Down Expand Up @@ -241,11 +241,12 @@ class InspectableWebContents
embedder_message_dispatcher_;

class NetworkResourceLoader;
std::set<std::unique_ptr<NetworkResourceLoader>, base::UniquePtrComparator>
base::flat_set<std::unique_ptr<NetworkResourceLoader>,
base::UniquePtrComparator>
loaders_;

using ExtensionsAPIs = std::map<std::string, std::string>;
ExtensionsAPIs extensions_api_;
// origin -> script
base::flat_map<std::string, std::string> extensions_api_;

// Contains the set of synced settings.
// The DevTools frontend *must* call `Register` for each setting prior to
Expand Down
7 changes: 3 additions & 4 deletions shell/browser/ui/message_box_gtk.cc
Expand Up @@ -4,9 +4,8 @@

#include "shell/browser/ui/message_box.h"

#include <map>

#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
Expand Down Expand Up @@ -43,8 +42,8 @@ MessageBoxSettings::~MessageBoxSettings() = default;
namespace {

// <ID, messageBox> map
std::map<int, GtkWidget*>& GetDialogsMap() {
static base::NoDestructor<std::map<int, GtkWidget*>> dialogs;
base::flat_map<int, GtkWidget*>& GetDialogsMap() {
static base::NoDestructor<base::flat_map<int, GtkWidget*>> dialogs;
return *dialogs;
}

Expand Down

0 comments on commit 22970f5

Please sign in to comment.