Skip to content

Commit 56651d0

Browse files
authored
Merge pull request o3de#17634 from aws-lumberyard-dev/daimini/Prefabs/StopStoringNonFocusedInstanceDomInMemory
Prefabs | Reduce memory consumption on big, deeply nested levels.
2 parents cc117db + 5762253 commit 56651d0

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <AzToolsFramework/Prefab/Instance/InstanceEntityMapperInterface.h>
1919
#include <AzToolsFramework/Prefab/Instance/TemplateInstanceMapperInterface.h>
2020

21+
#include <AzToolsFramework/Prefab/PrefabFocusInterface.h>
22+
2123
namespace AzToolsFramework
2224
{
2325
namespace Prefab
@@ -85,6 +87,8 @@ namespace AzToolsFramework
8587
"It is a requirement for the Prefab Instance class. "
8688
"Check that it is being correctly initialized.");
8789

90+
m_isDomCachingEnabled = s_DomCachingEnabledDefault;
91+
8892
if (parent)
8993
{
9094
AliasPath absoluteInstancePath = m_parent->GetAbsoluteInstanceAliasPath();
@@ -947,8 +951,21 @@ namespace AzToolsFramework
947951

948952
void Instance::SetCachedInstanceDom(PrefabDomValueConstReference instanceDom)
949953
{
950-
m_cachedInstanceDom = PrefabDom(); // force a flush of memory by clearing first.
951-
m_cachedInstanceDom.CopyFrom(instanceDom->get(), m_cachedInstanceDom.GetAllocator());
954+
// force a flush of memory by clearing first if cache isn't empty.
955+
if (!m_cachedInstanceDom.IsNull())
956+
{
957+
m_cachedInstanceDom = PrefabDom();
958+
}
959+
960+
if (m_isDomCachingEnabled)
961+
{
962+
m_cachedInstanceDom.CopyFrom(instanceDom->get(), m_cachedInstanceDom.GetAllocator());
963+
}
964+
}
965+
966+
void Instance::EnableDomCaching(bool enableDomCaching)
967+
{
968+
m_isDomCachingEnabled = enableDomCaching;
952969
}
953970
}
954971
} // namespace AzToolsFramework

Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/Instance/Instance.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ namespace AzToolsFramework
9898
//! @param context reflection context.
9999
static void Reflect(AZ::ReflectContext* context);
100100

101+
//! Whether caching the instance's dom should be enabled by default.
102+
//! Setting can be changed for individual instances using EnableDomCaching.
103+
inline static bool s_DomCachingEnabledDefault = false;
104+
101105
//! Gets template id.
102106
//! @return Template id of the instance.
103107
TemplateId GetTemplateId() const;
@@ -315,10 +319,12 @@ namespace AzToolsFramework
315319
//! @}
316320

317321
//! Getter and setter for cached instance DOM.
322+
//! Note that caching the DOM is disabled by default and needs to be enabled per instance.
318323
//! @{
319324
PrefabDomConstReference GetCachedInstanceDom() const;
320325
PrefabDomReference GetCachedInstanceDom();
321326
void SetCachedInstanceDom(PrefabDomValueConstReference instanceDom);
327+
void EnableDomCaching(bool enableDomCaching);
322328
//! @}
323329

324330
private:
@@ -408,6 +414,10 @@ namespace AzToolsFramework
408414

409415
// Defines entity id and instance relationship. The default relationship is one-to-one.
410416
EntityIdInstanceRelationship m_entityIdInstanceRelationship = DefaultEntityIdInstanceRelationship;
417+
418+
// Whether the instance should store a cache of its DOM or not.
419+
// Default value is set by s_DomCachingEnabledDefault in the constructor.
420+
bool m_isDomCachingEnabled = false;
411421
};
412422
} // namespace Prefab
413423
} // namespace AzToolsFramework

Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabFocusHandler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,13 @@ namespace AzToolsFramework::Prefab
287287
m_rootAliasFocusPath = focusedInstance->get().GetAbsoluteInstanceAliasPath();
288288
m_rootAliasFocusPathLength = aznumeric_cast<int>(AZStd::distance(m_rootAliasFocusPath.begin(), m_rootAliasFocusPath.end()));
289289

290+
// Unset the DOM caching for previous focus and enabled it in new focus to optimize editing.
291+
if (previousFocusedInstance.has_value())
292+
{
293+
previousFocusedInstance->get().EnableDomCaching(false);
294+
}
295+
focusedInstance->get().EnableDomCaching(true);
296+
290297
// Focus on the container entity in the Editor, if the interface is initialized.
291298
if (m_focusModeInterface)
292299
{

Code/Framework/AzToolsFramework/Tests/Prefab/PrefabTestFixture.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ namespace UnitTest
6161
m_settingsRegistryInterface = AZ::SettingsRegistry::Get();
6262
EXPECT_TRUE(m_settingsRegistryInterface);
6363

64+
// Have all instances cache their DOM for testing purposes.
65+
Instance::s_DomCachingEnabledDefault = true;
66+
6467
// This is for calling CreateEditorRepresentation that adds required editor components.
6568
AzToolsFramework::EditorRequestBus::Handler::BusConnect();
6669

0 commit comments

Comments
 (0)