From bce144aaf362c3c711cf4c14f9f40a4af5637b31 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 6 Aug 2024 19:23:51 -0500 Subject: [PATCH] perf: avoid redundant calls to GetView() (#43230) * perf: avoid double-calls to GetView() There are a lot of places where we call the virtual method GetView() twice in succession: the first to check if the view exists, and the second to use. This PR holds the view in a temp variable instead, e.g.: if (auto* view = foo->GetView()) view->DoSomething(); Co-authored-by: Charles Kerr * perf: avoid discarded GetView() call Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/browser/api/frame_subscriber.cc | 5 ++-- .../osr/osr_render_widget_host_view.cc | 6 ++-- shell/browser/osr/osr_web_contents_view.cc | 30 ++++++++----------- shell/browser/ui/autofill_popup.cc | 7 ++--- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/shell/browser/api/frame_subscriber.cc b/shell/browser/api/frame_subscriber.cc index cdb65c7667..55521e6fed 100644 --- a/shell/browser/api/frame_subscriber.cc +++ b/shell/browser/api/frame_subscriber.cc @@ -37,13 +37,14 @@ void FrameSubscriber::AttachToHost(content::RenderWidgetHost* host) { // The view can be null if the renderer process has crashed. // (https://crbug.com/847363) - if (!host_->GetView()) + auto* rwhv = host_->GetView(); + if (!rwhv) return; // Create and configure the video capturer. gfx::Size size = GetRenderViewSize(); DCHECK(!size.IsEmpty()); - video_capturer_ = host_->GetView()->CreateVideoCapturer(); + video_capturer_ = rwhv->CreateVideoCapturer(); video_capturer_->SetResolutionConstraints(size, size, true); video_capturer_->SetAutoThrottlingEnabled(false); video_capturer_->SetMinSizeChangePeriod(base::TimeDelta()); diff --git a/shell/browser/osr/osr_render_widget_host_view.cc b/shell/browser/osr/osr_render_widget_host_view.cc index afe4c5ee56..6f4da505e3 100644 --- a/shell/browser/osr/osr_render_widget_host_view.cc +++ b/shell/browser/osr/osr_render_widget_host_view.cc @@ -562,10 +562,8 @@ OffScreenRenderWidgetHostView::CreateViewForWidget( content::RenderWidgetHost* render_widget_host, content::RenderWidgetHost* embedder_render_widget_host, content::WebContentsView* web_contents_view) { - if (render_widget_host->GetView()) { - return static_cast( - render_widget_host->GetView()); - } + if (auto* rwhv = render_widget_host->GetView()) + return static_cast(rwhv); OffScreenRenderWidgetHostView* embedder_host_view = nullptr; if (embedder_render_widget_host) { diff --git a/shell/browser/osr/osr_web_contents_view.cc b/shell/browser/osr/osr_web_contents_view.cc index 059f27bbf4..e856d3260a 100644 --- a/shell/browser/osr/osr_web_contents_view.cc +++ b/shell/browser/osr/osr_web_contents_view.cc @@ -33,8 +33,8 @@ void OffScreenWebContentsView::SetWebContents( content::WebContents* web_contents) { web_contents_ = web_contents; - if (GetView()) - GetView()->InstallTransparency(); + if (auto* view = GetView()) + view->InstallTransparency(); } void OffScreenWebContentsView::SetNativeWindow(NativeWindow* window) { @@ -51,8 +51,8 @@ void OffScreenWebContentsView::SetNativeWindow(NativeWindow* window) { void OffScreenWebContentsView::OnWindowResize() { // In offscreen mode call RenderWidgetHostView's SetSize explicitly - if (GetView()) - GetView()->SetSize(GetSize()); + if (auto* view = GetView()) + view->SetSize(GetSize()); } void OffScreenWebContentsView::OnWindowClosed() { @@ -109,7 +109,9 @@ void OffScreenWebContentsView::TransferDragSecurityInfo(WebContentsView* view) { } gfx::Rect OffScreenWebContentsView::GetViewBounds() const { - return GetView() ? GetView()->GetViewBounds() : gfx::Rect(); + if (auto* view = GetView()) + return view->GetViewBounds(); + return {}; } void OffScreenWebContentsView::CreateView(gfx::NativeView context) {} @@ -117,10 +119,8 @@ void OffScreenWebContentsView::CreateView(gfx::NativeView context) {} content::RenderWidgetHostViewBase* OffScreenWebContentsView::CreateViewForWidget( content::RenderWidgetHost* render_widget_host) { - if (render_widget_host->GetView()) { - return static_cast( - render_widget_host->GetView()); - } + if (auto* rwhv = render_widget_host->GetView()) + return static_cast(rwhv); return new OffScreenRenderWidgetHostView( transparent_, painting_, GetFrameRate(), callback_, render_widget_host, @@ -146,8 +146,8 @@ OffScreenWebContentsView::CreateViewForChildWidget( void OffScreenWebContentsView::SetPageTitle(const std::u16string& title) {} void OffScreenWebContentsView::RenderViewReady() { - if (GetView()) - GetView()->InstallTransparency(); + if (auto* view = GetView()) + view->InstallTransparency(); } void OffScreenWebContentsView::RenderViewHostChanged( @@ -183,11 +183,9 @@ void OffScreenWebContentsView::UpdateDragOperation( bool document_is_handling_drag) {} void OffScreenWebContentsView::SetPainting(bool painting) { - auto* view = GetView(); painting_ = painting; - if (view != nullptr) { + if (auto* view = GetView()) view->SetPainting(painting); - } } bool OffScreenWebContentsView::IsPainting() const { @@ -197,11 +195,9 @@ bool OffScreenWebContentsView::IsPainting() const { } void OffScreenWebContentsView::SetFrameRate(int frame_rate) { - auto* view = GetView(); frame_rate_ = frame_rate; - if (view != nullptr) { + if (auto* view = GetView()) view->SetFrameRate(frame_rate); - } } int OffScreenWebContentsView::GetFrameRate() const { diff --git a/shell/browser/ui/autofill_popup.cc b/shell/browser/ui/autofill_popup.cc index 5b051098fb..a9780dc876 100644 --- a/shell/browser/ui/autofill_popup.cc +++ b/shell/browser/ui/autofill_popup.cc @@ -192,11 +192,8 @@ void AutofillPopup::CreateView(content::RenderFrameHost* frame_host, view_ = new AutofillPopupView(this, parent->GetWidget()); if (offscreen) { - auto* rwhv = frame_host->GetView(); - if (embedder_frame_host != nullptr) { - rwhv = embedder_frame_host->GetView(); - } - + auto* rwhv = embedder_frame_host ? embedder_frame_host->GetView() + : frame_host->GetView(); auto* osr_rwhv = static_cast(rwhv); view_->view_proxy_ = std::make_unique(view_); osr_rwhv->AddViewProxy(view_->view_proxy_.get());