From 1d241aa67bf85fc37a7b766128f67b63640188e5 Mon Sep 17 00:00:00 2001 From: Ansis Date: Tue, 19 Nov 2024 16:26:21 +0100 Subject: [PATCH] Make sure all instances of PartitionComponent come from the pool and return to it --- .../Demo/AllNftShapePlaygroundBoot.cs | 15 +++- .../NFTShape/Demo/NFTShapeDemoWorld.cs | 19 +++-- .../NFTShape/Demo/NftShapePlaygroundBoot.cs | 17 ++++- .../Demo/WarmUpSettingsNftShapeDemoWorld.cs | 19 ++++- .../Components/PartitionComponent.cs | 14 +++- .../Components/VolatileScenePointers.cs | 19 ++--- ...dPointersByIncreasingRadiusSystemShould.cs | 8 +- .../Scripts/Global/ComponentsContainer.cs | 9 +-- .../Global/Dynamic/DynamicWorldContainer.cs | 4 +- .../Scripts/Global/Dynamic/RealmController.cs | 8 +- .../Scripts/Global/Static/SceneLauncher.cs | 75 ------------------- .../Global/Static/SceneLauncher.cs.meta | 11 --- 12 files changed, 99 insertions(+), 119 deletions(-) delete mode 100644 Explorer/Assets/Scripts/Global/Static/SceneLauncher.cs delete mode 100644 Explorer/Assets/Scripts/Global/Static/SceneLauncher.cs.meta diff --git a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/AllNftShapePlaygroundBoot.cs b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/AllNftShapePlaygroundBoot.cs index c4075aa560..5a7926f713 100644 --- a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/AllNftShapePlaygroundBoot.cs +++ b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/AllNftShapePlaygroundBoot.cs @@ -3,9 +3,11 @@ using DCL.AssetsProvision; using DCL.DemoWorlds; using DCL.ECSComponents; +using DCL.Optimization.Pools; using DCL.SDKComponents.NFTShape.Component; using DCL.SDKComponents.NFTShape.Frames.FramePrefabs; using DCL.SDKComponents.NFTShape.Frames.Pool; +using ECS.Prioritization.Components; using System; using System.Collections.Generic; using System.Linq; @@ -25,6 +27,8 @@ public class AllNftShapePlaygroundBoot : MonoBehaviour [SerializeField] private int countInRow = 5; [SerializeField] private float distanceBetween = 1f; + private IComponentPool? partitionComponentPool; + private void Start() { LaunchAsync().Forget(); @@ -34,6 +38,9 @@ private async UniTask LaunchAsync() { var world = World.Create(); + partitionComponentPool = new ComponentPool.WithDefaultCtor( + component => component.Clear()); + var framesPrefabs = new AssetProvisionerFramePrefabs(new AddressablesProvisioner()); var framesPool = new FramesPool(framesPrefabs); @@ -46,13 +53,19 @@ await framesPrefabs.InitializeAsync( new SeveralDemoWorld( AllFrameTypes() .Select(e => nftShapeProperties.With(e)) - .Select(e => new WarmUpSettingsNftShapeDemoWorld(world, framesPool, framesPrefabs, e, () => visible) as IDemoWorld) + .Select(e => new WarmUpSettingsNftShapeDemoWorld(world, framesPool, + framesPrefabs, e, () => visible, partitionComponentPool) as IDemoWorld) .Append(new GridDemoWorld(world, countInRow, distanceBetween)) .ToList() ).SetUpAndRunAsync(destroyCancellationToken) .Forget(); } + private void OnDestroy() + { + partitionComponentPool?.Dispose(); + } + private IEnumerable AllFrameTypes() => Enum.GetNames(typeof(NftFrameType)).Select(Enum.Parse); } diff --git a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NFTShapeDemoWorld.cs b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NFTShapeDemoWorld.cs index e3c90b01de..2de13c7999 100644 --- a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NFTShapeDemoWorld.cs +++ b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NFTShapeDemoWorld.cs @@ -30,11 +30,14 @@ public class NFTShapeDemoWorld : IDemoWorld { private readonly IDemoWorld origin; - public NFTShapeDemoWorld(World world, IFramesPool framesPool, IReadOnlyFramePrefabs framePrefabs, params (PBNftShape textShape, PBVisibilityComponent visibility, PBBillboard billboard)[] list) : this( - world, framesPool, framePrefabs, list.AsReadOnly() - ) { } + public NFTShapeDemoWorld(World world, IFramesPool framesPool, + IReadOnlyFramePrefabs framePrefabs, IComponentPool partitionComponentPool, + params (PBNftShape textShape, PBVisibilityComponent visibility, PBBillboard billboard)[] list) + : this(world, framesPool, framePrefabs, partitionComponentPool, list.AsReadOnly()) { } - public NFTShapeDemoWorld(World world, IFramesPool framesPool, IReadOnlyFramePrefabs framePrefabs, IReadOnlyList<(PBNftShape textShape, PBVisibilityComponent visibility, PBBillboard billboard)> list) + public NFTShapeDemoWorld(World world, IFramesPool framesPool, + IReadOnlyFramePrefabs framePrefabs, IComponentPool partitionComponentPool, + IReadOnlyList<(PBNftShape textShape, PBVisibilityComponent visibility, PBBillboard billboard)> list) { var buffer = new EntityEventBuffer(1); @@ -43,7 +46,13 @@ public NFTShapeDemoWorld(World world, IFramesPool framesPool, IReadOnlyFramePref w => { foreach ((PBNftShape nftShape, PBVisibilityComponent visibility, PBBillboard billboard) in list) - w.Create(nftShape, visibility, billboard, NewTransform(), new PartitionComponent { IsBehind = false, RawSqrDistance = 0 }); + { + PartitionComponent partitionComponent = partitionComponentPool.Get(); + partitionComponent.IsBehind = false; + partitionComponent.RawSqrDistance = 0f; + + w.Create(nftShape, visibility, billboard, NewTransform(), partitionComponent); + } }, w => new AssetsDeferredLoadingSystem(w, new NullPerformanceBudget(), new NullPerformanceBudget()), w => new LoadNFTShapeSystem( diff --git a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NftShapePlaygroundBoot.cs b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NftShapePlaygroundBoot.cs index a311f3cfaa..af8941fd53 100644 --- a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NftShapePlaygroundBoot.cs +++ b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/NftShapePlaygroundBoot.cs @@ -1,9 +1,12 @@ using Cysharp.Threading.Tasks; using DCL.AssetsProvision; using DCL.DemoWorlds; +using DCL.Optimization.Pools; using DCL.SDKComponents.NFTShape.Component; using DCL.SDKComponents.NFTShape.Frames.FramePrefabs; using DCL.SDKComponents.NFTShape.Frames.Pool; +using ECS.Prioritization.Components; +using System; using UnityEngine; namespace DCL.SDKComponents.NFTShape.Demo @@ -17,6 +20,8 @@ public class NftShapePlaygroundBoot : MonoBehaviour [SerializeField] private NFTShapeSettings settings = null!; + private IComponentPool? partitionComponentPool; + private void Start() { LaunchAsync().Forget(); @@ -24,8 +29,13 @@ private void Start() private async UniTask LaunchAsync() { + partitionComponentPool = new ComponentPool.WithDefaultCtor( + component => component.Clear()); + var framesPrefabs = new AssetProvisionerFramePrefabs(new AddressablesProvisioner()); - var world = new WarmUpSettingsNftShapeDemoWorld(new FramesPool(framesPrefabs), framesPrefabs, nftShapeProperties, () => visible); + + var world = new WarmUpSettingsNftShapeDemoWorld(new FramesPool(framesPrefabs), + framesPrefabs, nftShapeProperties, () => visible, partitionComponentPool); await framesPrefabs.InitializeAsync( settings.FramePrefabs(), @@ -35,5 +45,10 @@ await framesPrefabs.InitializeAsync( await world.SetUpAndRunAsync(destroyCancellationToken); } + + private void OnDestroy() + { + partitionComponentPool?.Dispose(); + } } } diff --git a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/WarmUpSettingsNftShapeDemoWorld.cs b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/WarmUpSettingsNftShapeDemoWorld.cs index 3a49eda883..a6af983f34 100644 --- a/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/WarmUpSettingsNftShapeDemoWorld.cs +++ b/Explorer/Assets/DCL/SDKComponents/NFTShape/Demo/WarmUpSettingsNftShapeDemoWorld.cs @@ -1,9 +1,11 @@ using Arch.Core; using DCL.DemoWorlds; using DCL.ECSComponents; +using DCL.Optimization.Pools; using DCL.SDKComponents.NFTShape.Component; using DCL.SDKComponents.NFTShape.Frames.FramePrefabs; using DCL.SDKComponents.NFTShape.Frames.Pool; +using ECS.Prioritization.Components; using System; namespace DCL.SDKComponents.NFTShape.Demo @@ -17,9 +19,17 @@ public class WarmUpSettingsNftShapeDemoWorld : IDemoWorld private readonly PBNftShape nftShape; private readonly PBVisibilityComponent visibility; - public WarmUpSettingsNftShapeDemoWorld(IFramesPool framesPool, IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties, Func visible) : this(World.Create(), framesPool, framePrefabs, nftShapeProperties, visible) { } + public WarmUpSettingsNftShapeDemoWorld(IFramesPool framesPool, + IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties, + Func visible, IComponentPool partitionComponentPool) + : this(World.Create(), framesPool, framePrefabs, nftShapeProperties, visible, + partitionComponentPool) { } - public WarmUpSettingsNftShapeDemoWorld(World world, IFramesPool framesPool, IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties, Func visible) : this(world, framesPool, framePrefabs, new PBNftShape(), new PBVisibilityComponent(), new PBBillboard(), nftShapeProperties, visible) { } + public WarmUpSettingsNftShapeDemoWorld(World world, IFramesPool framesPool, + IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties, + Func visible, IComponentPool partitionComponentPool) + : this(world, framesPool, framePrefabs, new PBNftShape(), new PBVisibilityComponent(), + new PBBillboard(), nftShapeProperties, visible, partitionComponentPool) { } public WarmUpSettingsNftShapeDemoWorld( World world, @@ -29,8 +39,8 @@ public WarmUpSettingsNftShapeDemoWorld( PBVisibilityComponent visibility, PBBillboard billboard, NftShapeProperties nftShapeProperties, - Func visible - ) + Func visible, + IComponentPool partitionComponentPool) { this.nftShape = nftShape; this.visibility = visibility; @@ -43,6 +53,7 @@ Func visible world, framesPool, framePrefabs, + partitionComponentPool, (nftShape, visibility, billboard) ) ); diff --git a/Explorer/Assets/Scripts/ECS/Prioritization/Components/PartitionComponent.cs b/Explorer/Assets/Scripts/ECS/Prioritization/Components/PartitionComponent.cs index 99399939ec..ef03a67056 100644 --- a/Explorer/Assets/Scripts/ECS/Prioritization/Components/PartitionComponent.cs +++ b/Explorer/Assets/Scripts/ECS/Prioritization/Components/PartitionComponent.cs @@ -15,11 +15,23 @@ public class PartitionComponent : IPartitionComponent, IEquatableOnly the component pool and tests may call this! + public PartitionComponent() { } + + public void Clear() + { + OutOfRange = false; + IsDirty = false; + RawSqrDistance = 0f; + Bucket = 0; + IsBehind = false; + } + /// /// Indicates that the partition is out of buckets range /// public bool OutOfRange { get; set; } - + /// /// Indicates that the partition value has changed and the processes assigned to it should be re-prioritized /// diff --git a/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Components/VolatileScenePointers.cs b/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Components/VolatileScenePointers.cs index 321dfda2e3..c58e982ac5 100644 --- a/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Components/VolatileScenePointers.cs +++ b/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Components/VolatileScenePointers.cs @@ -23,21 +23,22 @@ public struct VolatileScenePointers /// public AssetPromise? ActivePromise; - public VolatileScenePointers(List retrievedReusableList, List inputReusableList) + public VolatileScenePointers(List retrievedReusableList, + List inputReusableList, PartitionComponent partitionComponent) { RetrievedReusableList = retrievedReusableList; InputReusableList = inputReusableList; ActivePromise = null; - ActivePartitionComponent = new PartitionComponent - { - Bucket = 0, - //Lets lower the prio against asset bundles on the same bucket - IsBehind = true - }; + ActivePartitionComponent = partitionComponent; + + partitionComponent.Bucket = 0; + + // Lets lower the prio against asset bundles on the same bucket + partitionComponent.IsBehind = true; } - public static VolatileScenePointers Create() => + public static VolatileScenePointers Create(PartitionComponent partitionComponent) => new (new List(PoolConstants.SCENES_COUNT), - new List(PoolConstants.SCENES_COUNT)); + new List(PoolConstants.SCENES_COUNT), partitionComponent); } } diff --git a/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Tests/LoadPointersByIncreasingRadiusSystemShould.cs b/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Tests/LoadPointersByIncreasingRadiusSystemShould.cs index a775c79f25..a30f50773a 100644 --- a/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Tests/LoadPointersByIncreasingRadiusSystemShould.cs +++ b/Explorer/Assets/Scripts/ECS/SceneLifeCycle/Tests/LoadPointersByIncreasingRadiusSystemShould.cs @@ -1,6 +1,7 @@ using Arch.Core; using DCL.Ipfs; using ECS.Prioritization; +using ECS.Prioritization.Components; using ECS.SceneLifeCycle.Components; using ECS.SceneLifeCycle.IncreasingRadius; using ECS.TestSuite; @@ -44,7 +45,9 @@ public void StartLoading([Range(1, 10, 1)] int radius) using var processedParcels = new NativeHashSet(100, AllocatorManager.Persistent); parcelMathJobifiedHelper.StartParcelsRingSplit(new int2(1, 1), radius, processedParcels); - var scenePointers = new VolatileScenePointers(new List(), new List()); + + var scenePointers = new VolatileScenePointers(new List(), + new List(), new PartitionComponent()); Entity e = world.Create(realm, scenePointers, new ProcessedScenePointers { Value = processedParcels }); system.Update(0); @@ -71,7 +74,8 @@ public void NotStartLoadingProcessedParcels([Range(1, 10, 1)] int radius) foreach (ParcelMathJobifiedHelper.ParcelInfo parcel in array) processedParcels.Add(parcel.Parcel); - var scenePointers = new VolatileScenePointers(new List(), new List()); + var scenePointers = new VolatileScenePointers(new List(), + new List(), new PartitionComponent()); Entity e = world.Create(realm, scenePointers, new ProcessedScenePointers { Value = processedParcels }); diff --git a/Explorer/Assets/Scripts/Global/ComponentsContainer.cs b/Explorer/Assets/Scripts/Global/ComponentsContainer.cs index 3c69ac9704..2a1d3c2aaf 100644 --- a/Explorer/Assets/Scripts/Global/ComponentsContainer.cs +++ b/Explorer/Assets/Scripts/Global/ComponentsContainer.cs @@ -154,12 +154,9 @@ public static ComponentsContainer Create() private static IEnumerable<(Type type, IComponentPool pool)> GetMiscComponents() { // Partition Component - yield return (typeof(PartitionComponent), new ComponentPool.WithDefaultCtor(defaultCapacity: 2000, onRelease: p => - { - p.IsBehind = false; - p.Bucket = byte.MaxValue; - p.RawSqrDistance = 0; - })); + yield return (typeof(PartitionComponent), + new ComponentPool.WithDefaultCtor( + component => component.Clear())); } } } diff --git a/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs b/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs index 3011e0e6a9..ea8b0d30b1 100644 --- a/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs +++ b/Explorer/Assets/Scripts/Global/Dynamic/DynamicWorldContainer.cs @@ -294,7 +294,9 @@ IMultiPool MultiPoolFactory() => staticContainer.ScenesCache, staticContainer.PartitionDataContainer, staticContainer.SingletonSharedDependencies.SceneAssetLock, - debugBuilder); + debugBuilder, + staticContainer.ComponentsContainer.ComponentPoolsRegistry + .GetReferenceTypePool()); bool localSceneDevelopment = !string.IsNullOrEmpty(dynamicWorldParams.LocalSceneDevelopmentRealm); container.reloadSceneController = new ECSReloadScene(staticContainer.ScenesCache, globalWorld, playerEntity, localSceneDevelopment); diff --git a/Explorer/Assets/Scripts/Global/Dynamic/RealmController.cs b/Explorer/Assets/Scripts/Global/Dynamic/RealmController.cs index d4698a6332..481c6ed99a 100644 --- a/Explorer/Assets/Scripts/Global/Dynamic/RealmController.cs +++ b/Explorer/Assets/Scripts/Global/Dynamic/RealmController.cs @@ -46,6 +46,7 @@ public class RealmController : IGlobalRealmController private readonly PartitionDataContainer partitionDataContainer; private readonly IScenesCache scenesCache; private readonly SceneAssetLock sceneAssetLock; + private readonly IComponentPool partitionComponentPool; private GlobalWorld? globalWorld; private Entity realmEntity; @@ -77,7 +78,8 @@ public RealmController( IScenesCache scenesCache, PartitionDataContainer partitionDataContainer, SceneAssetLock sceneAssetLock, - IDebugContainerBuilder debugContainerBuilder) + IDebugContainerBuilder debugContainerBuilder, + IComponentPool partitionComponentPool) { this.web3IdentityCache = web3IdentityCache; this.webRequestController = webRequestController; @@ -89,7 +91,7 @@ public RealmController( this.scenesCache = scenesCache; this.partitionDataContainer = partitionDataContainer; this.sceneAssetLock = sceneAssetLock; - + this.partitionComponentPool = partitionComponentPool; realmNavigatorDebugView = new RealmNavigatorDebugView(debugContainerBuilder); } @@ -224,7 +226,7 @@ private async UniTask UnloadCurrentRealmAsync() private void ComplimentWithVolatilePointers(World world, Entity realmEntity) { - world.Add(realmEntity, VolatileScenePointers.Create()); + world.Add(realmEntity, VolatileScenePointers.Create(partitionComponentPool.Get())); } private bool ComplimentWithStaticPointers(World world, Entity realmEntity) diff --git a/Explorer/Assets/Scripts/Global/Static/SceneLauncher.cs b/Explorer/Assets/Scripts/Global/Static/SceneLauncher.cs deleted file mode 100644 index cdc44f6d75..0000000000 --- a/Explorer/Assets/Scripts/Global/Static/SceneLauncher.cs +++ /dev/null @@ -1,75 +0,0 @@ -using Cysharp.Threading.Tasks; -using ECS.Prioritization.Components; -using SceneRunner.Scene; -using System; -using System.Collections.Generic; -using System.Threading; -using UnityEngine; -using UnityEngine.UIElements; - -namespace Global.Static -{ - [Serializable] - public class SceneLauncher - { - [SerializeField] private UIDocument ui; - [SerializeField] private List scenes; - - private ISceneFacade currentScene; - - public void Initialize(SceneSharedContainer sceneSharedContainer, CancellationToken destroyCancellationToken) - { - DropdownField dropdown = ui.rootVisualElement.Q("ScenesDropdown"); - - dropdown.choices = scenes; - - IntegerField fps = ui.rootVisualElement.Q("FPS"); - - dropdown.RegisterValueChangedCallback(OnSceneSelected); - - fps.RegisterValueChangedCallback(OnFPSChanged); - - Button stopBtn = ui.rootVisualElement.Q