perf: and refactor: Code Scope + Minor Performance Improvement. (#16467)

* We know result's size will be same as app_metrics_'s size so optimize the vector.

Reserving a vector to save on reallocation cost.

* Narrow scopes of variables to avoid error and optimize them.

Made two vectors scope narrower and reserved them because we know the size in advance. This helps save on allocation costs.

* fix spacing
This commit is contained in:
Shahzad Lone 2019-01-25 09:39:32 -05:00 committed by John Kleinschmidt
parent b66b5561ee
commit 74c29fb610
2 changed files with 5 additions and 2 deletions

View file

@ -1154,6 +1154,7 @@ v8::Local<v8::Promise> App::GetFileIcon(const base::FilePath& path,
std::vector<mate::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) { std::vector<mate::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) {
std::vector<mate::Dictionary> result; std::vector<mate::Dictionary> result;
result.reserve(app_metrics_.size());
int processor_count = base::SysInfo::NumberOfProcessors(); int processor_count = base::SysInfo::NumberOfProcessors();
for (const auto& process_metric : app_metrics_) { for (const auto& process_metric : app_metrics_) {

View file

@ -137,11 +137,12 @@ bool DesktopCapturer::ShouldScheduleNextRefresh(DesktopMediaList* list) {
} }
void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) { void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
std::vector<DesktopCapturer::Source> window_sources;
if (capture_window_ && if (capture_window_ &&
list->GetMediaListType() == content::DesktopMediaID::TYPE_WINDOW) { list->GetMediaListType() == content::DesktopMediaID::TYPE_WINDOW) {
capture_window_ = false; capture_window_ = false;
const auto& media_list_sources = list->GetSources(); const auto& media_list_sources = list->GetSources();
std::vector<DesktopCapturer::Source> window_sources;
window_sources.reserve(media_list_sources.size());
for (const auto& media_list_source : media_list_sources) { for (const auto& media_list_source : media_list_sources) {
window_sources.emplace_back(DesktopCapturer::Source{ window_sources.emplace_back(DesktopCapturer::Source{
media_list_source, std::string(), fetch_window_icons_}); media_list_source, std::string(), fetch_window_icons_});
@ -150,11 +151,12 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
std::back_inserter(captured_sources_)); std::back_inserter(captured_sources_));
} }
std::vector<DesktopCapturer::Source> screen_sources;
if (capture_screen_ && if (capture_screen_ &&
list->GetMediaListType() == content::DesktopMediaID::TYPE_SCREEN) { list->GetMediaListType() == content::DesktopMediaID::TYPE_SCREEN) {
capture_screen_ = false; capture_screen_ = false;
const auto& media_list_sources = list->GetSources(); const auto& media_list_sources = list->GetSources();
std::vector<DesktopCapturer::Source> screen_sources;
screen_sources.reserve(media_list_sources.size());
for (const auto& media_list_source : media_list_sources) { for (const auto& media_list_source : media_list_sources) {
screen_sources.emplace_back( screen_sources.emplace_back(
DesktopCapturer::Source{media_list_source, std::string()}); DesktopCapturer::Source{media_list_source, std::string()});