From 6e820bf79947ede8d1c883eb30fa3c2d72bd2d7a Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Tue, 7 Feb 2023 00:35:17 -0500
Subject: [PATCH] Reduce findChild calls during snapshot by 28000

---
 indra/newview/llfloatersnapshot.cpp | 47 ++++++++++++++++-------------
 indra/newview/llfloatersnapshot.h   |  5 +++
 indra/newview/llpanelsnapshot.cpp   | 35 ++++++++++-----------
 indra/newview/llpanelsnapshot.h     |  3 ++
 4 files changed, 50 insertions(+), 40 deletions(-)

diff --git a/indra/newview/llfloatersnapshot.cpp b/indra/newview/llfloatersnapshot.cpp
index 49f15540fe1..40374ac65f8 100644
--- a/indra/newview/llfloatersnapshot.cpp
+++ b/indra/newview/llfloatersnapshot.cpp
@@ -92,13 +92,13 @@ LLSnapshotModel::ESnapshotFormat LLFloaterSnapshot::Impl::getImageFormat(LLFloat
 LLSpinCtrl* LLFloaterSnapshot::Impl::getWidthSpinner(LLFloaterSnapshotBase* floater)
 {
 	LLPanelSnapshot* active_panel = getActivePanel(floater);
-	return active_panel ? active_panel->getWidthSpinner() : floater->getChild<LLSpinCtrl>("snapshot_width");
+	return active_panel ? active_panel->getWidthSpinner() : nullptr;
 }
 
 LLSpinCtrl* LLFloaterSnapshot::Impl::getHeightSpinner(LLFloaterSnapshotBase* floater)
 {
 	LLPanelSnapshot* active_panel = getActivePanel(floater);
-	return active_panel ? active_panel->getHeightSpinner() : floater->getChild<LLSpinCtrl>("snapshot_height");
+	return active_panel ? active_panel->getHeightSpinner() : nullptr;
 }
 
 void LLFloaterSnapshot::Impl::enableAspectRatioCheckbox(LLFloaterSnapshotBase* floater, BOOL enable)
@@ -134,7 +134,7 @@ LLSnapshotLivePreview* LLFloaterSnapshotBase::ImplBase::getPreviewView()
 LLSnapshotModel::ESnapshotLayerType LLFloaterSnapshot::Impl::getLayerType(LLFloaterSnapshotBase* floater)
 {
 	LLSnapshotModel::ESnapshotLayerType type = LLSnapshotModel::SNAPSHOT_TYPE_COLOR;
-	LLSD value = floater->getChild<LLUICtrl>("layer_types")->getValue();
+	LLSD value = floater->getLayerTypesCombo()->getValue();
 	const std::string id = value.asString();
 	if (id == "colors")
 		type = LLSnapshotModel::SNAPSHOT_TYPE_COLOR;
@@ -270,7 +270,7 @@ void LLFloaterSnapshot::Impl::updateControls(LLFloaterSnapshotBase* floater)
 	LLSnapshotModel::ESnapshotLayerType layer_type = getLayerType(floater);
 
 	floater->getChild<LLComboBox>("local_format_combo")->selectNthItem(gSavedSettings.getS32("SnapshotFormat"));
-	floater->getChildView("layer_types")->setEnabled(shot_type == LLSnapshotModel::SNAPSHOT_LOCAL);
+	floater->getLayerTypesCombo()->setEnabled(shot_type == LLSnapshotModel::SNAPSHOT_LOCAL);
 
 	LLPanelSnapshot* active_panel = getActivePanel(floater);
 	if (active_panel)
@@ -360,17 +360,17 @@ void LLFloaterSnapshot::Impl::updateControls(LLFloaterSnapshotBase* floater)
 	{
 	  case LLSnapshotModel::SNAPSHOT_WEB:
 		layer_type = LLSnapshotModel::SNAPSHOT_TYPE_COLOR;
-		floater->getChild<LLUICtrl>("layer_types")->setValue("colors");
+		floater->getLayerTypesCombo()->setValue("colors");
 		setResolution(floater, "profile_size_combo");
 		break;
 	  case LLSnapshotModel::SNAPSHOT_POSTCARD:
 		layer_type = LLSnapshotModel::SNAPSHOT_TYPE_COLOR;
-		floater->getChild<LLUICtrl>("layer_types")->setValue("colors");
+		floater->getLayerTypesCombo()->setValue("colors");
 		setResolution(floater, "postcard_size_combo");
 		break;
 	  case LLSnapshotModel::SNAPSHOT_TEXTURE:
 		layer_type = LLSnapshotModel::SNAPSHOT_TYPE_COLOR;
-		floater->getChild<LLUICtrl>("layer_types")->setValue("colors");
+		floater->getLayerTypesCombo()->setValue("colors");
 		setResolution(floater, "texture_size_combo");
 		break;
 	  case  LLSnapshotModel::SNAPSHOT_LOCAL:
@@ -741,20 +741,24 @@ void LLFloaterSnapshot::Impl::updateResolution(LLUICtrl* ctrl, void* data, BOOL
 
 		previewp->getSize(width, height);
 
-		// We use the height spinner here because we come here via the aspect ratio
-		// checkbox as well and we want height always changing to width by default.
-		// If we use the width spinner we would change width according to height by
-		// default, that is not what we want.
-		updateSpinners(view, previewp, width, height, !getHeightSpinner(view)->isDirty()); // may change width and height
-		
-		if(getWidthSpinner(view)->getValue().asInteger() != width || getHeightSpinner(view)->getValue().asInteger() != height)
+		auto panel = getActivePanel(view);
+		if (panel)
 		{
-			getWidthSpinner(view)->setValue(width);
-			getHeightSpinner(view)->setValue(height);
-			if (getActiveSnapshotType(view) == LLSnapshotModel::SNAPSHOT_TEXTURE)
+			// We use the height spinner here because we come here via the aspect ratio
+			// checkbox as well and we want height always changing to width by default.
+			// If we use the width spinner we would change width according to height by
+			// default, that is not what we want.
+			updateSpinners(view, previewp, width, height, !getHeightSpinner(view)->isDirty()); // may change width and height
+
+			if (getWidthSpinner(view)->getValue().asInteger() != width || getHeightSpinner(view)->getValue().asInteger() != height)
 			{
-				getWidthSpinner(view)->setIncrement(width >> 1);
-				getHeightSpinner(view)->setIncrement(height >> 1);
+				getWidthSpinner(view)->setValue(width);
+				getHeightSpinner(view)->setValue(height);
+				if (getActiveSnapshotType(view) == LLSnapshotModel::SNAPSHOT_TEXTURE)
+				{
+					getWidthSpinner(view)->setIncrement(width >> 1);
+					getHeightSpinner(view)->setIncrement(height >> 1);
+				}
 			}
 		}
 
@@ -991,8 +995,9 @@ BOOL LLFloaterSnapshot::postBuild()
 	((Impl*)impl)->setAspectRatioCheckboxValue(this, gSavedSettings.getBOOL("KeepAspectForSnapshot"));
 
 	childSetCommitCallback("layer_types", Impl::onCommitLayerTypes, this);
-	getChild<LLUICtrl>("layer_types")->setValue("colors");
-	getChildView("layer_types")->setEnabled(FALSE);
+	mLayerTypesCombo = getChild<LLUICtrl>("layer_types");
+	mLayerTypesCombo->setValue("colors");
+	mLayerTypesCombo->setEnabled(FALSE);
 
 	mFreezeFrameCheck = getChild<LLUICtrl>("freeze_frame_check");
 	mFreezeFrameCheck->setValue(gSavedSettings.getBOOL("UseFreezeFrame"));
diff --git a/indra/newview/llfloatersnapshot.h b/indra/newview/llfloatersnapshot.h
index 541931cfd5c..2a47ccc32df 100644
--- a/indra/newview/llfloatersnapshot.h
+++ b/indra/newview/llfloatersnapshot.h
@@ -65,6 +65,8 @@ class LLFloaterSnapshotBase : public LLFloater
 	void setFailureLabelPanelVisible(bool value) { if (mFailureLblPanel) mFailureLblPanel->setVisible(value); }
 	void inventorySaveFailed();
 
+	LLUICtrl* getLayerTypesCombo() { return mLayerTypesCombo; }
+
 	class ImplBase;
 	friend class ImplBase;
 	ImplBase* impl;
@@ -73,6 +75,7 @@ class LLFloaterSnapshotBase : public LLFloater
 	LLUICtrl* mThumbnailPlaceholder;
 	LLUICtrl *mRefreshBtn, *mRefreshLabel;
 	LLUICtrl *mSucceessLblPanel, *mFailureLblPanel;
+	LLUICtrl* mLayerTypesCombo = nullptr;
 	LLUICtrl* mFreezeFrameCheck = nullptr;
 	LLTextBox* m360Label = nullptr;
 };
@@ -173,6 +176,7 @@ class LLFloaterSnapshot final : public LLFloaterSnapshotBase
 
 	class Impl;
 	friend class Impl;
+	friend class LLFloaterSnapshotBase::ImplBase;
 };
 
 ///----------------------------------------------------------------------------
@@ -181,6 +185,7 @@ class LLFloaterSnapshot final : public LLFloaterSnapshotBase
 
 class LLFloaterSnapshot::Impl final : public LLFloaterSnapshotBase::ImplBase
 {
+	friend class LLFloaterSnapshotBase;
 	LOG_CLASS(LLFloaterSnapshot::Impl);
 public:
 	Impl(LLFloaterSnapshotBase* floater)
diff --git a/indra/newview/llpanelsnapshot.cpp b/indra/newview/llpanelsnapshot.cpp
index c8e9ed20140..897b65d5178 100644
--- a/indra/newview/llpanelsnapshot.cpp
+++ b/indra/newview/llpanelsnapshot.cpp
@@ -65,11 +65,13 @@ BOOL LLPanelSnapshot::postBuild()
 	getChild<LLUICtrl>(getImageSizeComboName())->setCommitCallback(boost::bind(&LLPanelSnapshot::onResolutionComboCommit, this, _1));
     if (!getWidthSpinnerName().empty())
     {
-        getChild<LLUICtrl>(getWidthSpinnerName())->setCommitCallback(boost::bind(&LLPanelSnapshot::onCustomResolutionCommit, this));
+		mWidthSpinner = getChild<LLSpinCtrl>(getWidthSpinnerName());
+		mWidthSpinner->setCommitCallback(boost::bind(&LLPanelSnapshot::onCustomResolutionCommit, this));
     }
     if (!getHeightSpinnerName().empty())
     {
-        getChild<LLUICtrl>(getHeightSpinnerName())->setCommitCallback(boost::bind(&LLPanelSnapshot::onCustomResolutionCommit, this));
+        mHeightSpinner = getChild<LLSpinCtrl>(getHeightSpinnerName());
+		mHeightSpinner->setCommitCallback(boost::bind(&LLPanelSnapshot::onCustomResolutionCommit, this));
     }
     if (!getAspectRatioCBName().empty())
     {
@@ -112,25 +114,25 @@ void LLPanelSnapshot::enableControls(BOOL enable)
 LLSpinCtrl* LLPanelSnapshot::getWidthSpinner()
 {
     llassert(!getWidthSpinnerName().empty());
-	return getChild<LLSpinCtrl>(getWidthSpinnerName());
+	return mWidthSpinner;
 }
 
 LLSpinCtrl* LLPanelSnapshot::getHeightSpinner()
 {
     llassert(!getHeightSpinnerName().empty());
-	return getChild<LLSpinCtrl>(getHeightSpinnerName());
+	return mHeightSpinner;
 }
 
 S32 LLPanelSnapshot::getTypedPreviewWidth() const
 {
     llassert(!getWidthSpinnerName().empty());
-	return getChild<LLUICtrl>(getWidthSpinnerName())->getValue().asInteger();
+	return mWidthSpinner->getValue().asInteger();
 }
 
 S32 LLPanelSnapshot::getTypedPreviewHeight() const
 {
     llassert(!getHeightSpinnerName().empty());
-    return getChild<LLUICtrl>(getHeightSpinnerName())->getValue().asInteger();
+    return mHeightSpinner->getValue().asInteger();
 }
 
 void LLPanelSnapshot::enableAspectRatioCheckbox(BOOL enable)
@@ -201,28 +203,23 @@ void LLPanelSnapshot::cancel()
 void LLPanelSnapshot::onCustomResolutionCommit()
 {
 	LLSD info;
-    std::string widthSpinnerName = getWidthSpinnerName();
-    std::string heightSpinnerName = getHeightSpinnerName();
-    llassert(!widthSpinnerName.empty() && !heightSpinnerName.empty());
-    LLSpinCtrl *widthSpinner = getChild<LLSpinCtrl>(widthSpinnerName);
-    LLSpinCtrl *heightSpinner = getChild<LLSpinCtrl>(heightSpinnerName);
 	if (getName() == "panel_snapshot_inventory")
 	{
-		S32 width = widthSpinner->getValue().asInteger();
+		S32 width = mWidthSpinner->getValue().asInteger();
 		width = power_of_two(width, MAX_TEXTURE_SIZE);
 		info["w"] = width;
-		widthSpinner->setIncrement(width >> 1);
-		widthSpinner->forceSetValue(width);
-		S32 height =  heightSpinner->getValue().asInteger();
+		mWidthSpinner->setIncrement(width >> 1);
+		mWidthSpinner->forceSetValue(width);
+		S32 height =  mHeightSpinner->getValue().asInteger();
 		height = power_of_two(height, MAX_TEXTURE_SIZE);
-		heightSpinner->setIncrement(height >> 1);
-		heightSpinner->forceSetValue(height);
+		mHeightSpinner->setIncrement(height >> 1);
+		mHeightSpinner->forceSetValue(height);
 		info["h"] = height;
 	}
 	else
 	{
-		info["w"] = widthSpinner->getValue().asInteger();
-		info["h"] = heightSpinner->getValue().asInteger();
+		info["w"] = mWidthSpinner->getValue().asInteger();
+		info["h"] = mHeightSpinner->getValue().asInteger();
 	}
     getParentByType<LLFloater>()->notify(LLSD().with("custom-res-change", info));
 }
diff --git a/indra/newview/llpanelsnapshot.h b/indra/newview/llpanelsnapshot.h
index 55273797cc0..d7a0bfb1b76 100644
--- a/indra/newview/llpanelsnapshot.h
+++ b/indra/newview/llpanelsnapshot.h
@@ -74,6 +74,9 @@ class LLPanelSnapshot: public LLPanel
 	void onKeepAspectRatioCommit(LLUICtrl* ctrl);
 
 	LLFloaterSnapshotBase* mSnapshotFloater;
+
+	LLSpinCtrl* mWidthSpinner = nullptr;
+	LLSpinCtrl*	mHeightSpinner = nullptr;
 };
 
 #endif // LL_LLPANELSNAPSHOT_H
-- 
GitLab