Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure all instances of PartitionComponent come from the pool #2833

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +27,8 @@ public class AllNftShapePlaygroundBoot : MonoBehaviour
[SerializeField] private int countInRow = 5;
[SerializeField] private float distanceBetween = 1f;

private IComponentPool<PartitionComponent>? partitionComponentPool;

private void Start()
{
LaunchAsync().Forget();
Expand All @@ -34,6 +38,9 @@ private async UniTask LaunchAsync()
{
var world = World.Create();

partitionComponentPool = new ComponentPool.WithDefaultCtor<PartitionComponent>(
component => component.Clear());

var framesPrefabs = new AssetProvisionerFramePrefabs(new AddressablesProvisioner());
var framesPool = new FramesPool(framesPrefabs);

Expand All @@ -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<NftFrameType> AllFrameTypes() =>
Enum.GetNames(typeof(NftFrameType)).Select(Enum.Parse<NftFrameType>);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PartitionComponent> 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<PartitionComponent> partitionComponentPool,
IReadOnlyList<(PBNftShape textShape, PBVisibilityComponent visibility, PBBillboard billboard)> list)
{
var buffer = new EntityEventBuffer<NftShapeRendererComponent>(1);

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,15 +20,22 @@ public class NftShapePlaygroundBoot : MonoBehaviour
[SerializeField]
private NFTShapeSettings settings = null!;

private IComponentPool<PartitionComponent>? partitionComponentPool;

private void Start()
{
LaunchAsync().Forget();
}

private async UniTask LaunchAsync()
{
partitionComponentPool = new ComponentPool.WithDefaultCtor<PartitionComponent>(
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(),
Expand All @@ -35,5 +45,10 @@ await framesPrefabs.InitializeAsync(

await world.SetUpAndRunAsync(destroyCancellationToken);
}

private void OnDestroy()
{
partitionComponentPool?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<bool> visible) : this(World.Create(), framesPool, framePrefabs, nftShapeProperties, visible) { }
public WarmUpSettingsNftShapeDemoWorld(IFramesPool framesPool,
IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties,
Func<bool> visible, IComponentPool<PartitionComponent> partitionComponentPool)
: this(World.Create(), framesPool, framePrefabs, nftShapeProperties, visible,
partitionComponentPool) { }

public WarmUpSettingsNftShapeDemoWorld(World world, IFramesPool framesPool, IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties, Func<bool> visible) : this(world, framesPool, framePrefabs, new PBNftShape(), new PBVisibilityComponent(), new PBBillboard(), nftShapeProperties, visible) { }
public WarmUpSettingsNftShapeDemoWorld(World world, IFramesPool framesPool,
IReadOnlyFramePrefabs framePrefabs, NftShapeProperties nftShapeProperties,
Func<bool> visible, IComponentPool<PartitionComponent> partitionComponentPool)
: this(world, framesPool, framePrefabs, new PBNftShape(), new PBVisibilityComponent(),
new PBBillboard(), nftShapeProperties, visible, partitionComponentPool) { }

public WarmUpSettingsNftShapeDemoWorld(
World world,
Expand All @@ -29,8 +39,8 @@ public WarmUpSettingsNftShapeDemoWorld(
PBVisibilityComponent visibility,
PBBillboard billboard,
NftShapeProperties nftShapeProperties,
Func<bool> visible
)
Func<bool> visible,
IComponentPool<PartitionComponent> partitionComponentPool)
{
this.nftShape = nftShape;
this.visibility = visibility;
Expand All @@ -43,6 +53,7 @@ Func<bool> visible
world,
framesPool,
framePrefabs,
partitionComponentPool,
(nftShape, visibility, billboard)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ public class PartitionComponent : IPartitionComponent, IEquatable<IPartitionComp
IsBehind = false,
};

/// <remarks>Only the component pool and tests may call this!</remarks>
public PartitionComponent() { }

public void Clear()
{
OutOfRange = false;
IsDirty = false;
RawSqrDistance = 0f;
Bucket = 0;
IsBehind = false;
}

/// <summary>
/// Indicates that the partition is out of buckets range
/// </summary>
public bool OutOfRange { get; set; }

/// <summary>
/// Indicates that the partition value has changed and the processes assigned to it should be re-prioritized
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ public struct VolatileScenePointers
/// </summary>
public AssetPromise<SceneDefinitions, GetSceneDefinitionList>? ActivePromise;

public VolatileScenePointers(List<SceneEntityDefinition> retrievedReusableList, List<int2> inputReusableList)
public VolatileScenePointers(List<SceneEntityDefinition> retrievedReusableList,
List<int2> inputReusableList, PartitionComponent partitionComponent)
AnsisMalins marked this conversation as resolved.
Show resolved Hide resolved
{
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<SceneEntityDefinition>(PoolConstants.SCENES_COUNT),
new List<int2>(PoolConstants.SCENES_COUNT));
new List<int2>(PoolConstants.SCENES_COUNT), partitionComponent);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,7 +45,9 @@ public void StartLoading([Range(1, 10, 1)] int radius)
using var processedParcels = new NativeHashSet<int2>(100, AllocatorManager.Persistent);

parcelMathJobifiedHelper.StartParcelsRingSplit(new int2(1, 1), radius, processedParcels);
var scenePointers = new VolatileScenePointers(new List<SceneEntityDefinition>(), new List<int2>());

var scenePointers = new VolatileScenePointers(new List<SceneEntityDefinition>(),
new List<int2>(), new PartitionComponent());

Entity e = world.Create(realm, scenePointers, new ProcessedScenePointers { Value = processedParcels });
system.Update(0);
Expand All @@ -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<SceneEntityDefinition>(), new List<int2>());
var scenePointers = new VolatileScenePointers(new List<SceneEntityDefinition>(),
new List<int2>(), new PartitionComponent());

Entity e = world.Create(realm, scenePointers, new ProcessedScenePointers { Value = processedParcels });

Expand Down
9 changes: 3 additions & 6 deletions Explorer/Assets/Scripts/Global/ComponentsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PartitionComponent>(defaultCapacity: 2000, onRelease: p =>
{
p.IsBehind = false;
p.Bucket = byte.MaxValue;
p.RawSqrDistance = 0;
}));
yield return (typeof(PartitionComponent),
new ComponentPool.WithDefaultCtor<PartitionComponent>(
component => component.Clear()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ IMultiPool MultiPoolFactory() =>
staticContainer.ScenesCache,
staticContainer.PartitionDataContainer,
staticContainer.SingletonSharedDependencies.SceneAssetLock,
debugBuilder);
debugBuilder,
staticContainer.ComponentsContainer.ComponentPoolsRegistry
.GetReferenceTypePool<PartitionComponent>());

bool localSceneDevelopment = !string.IsNullOrEmpty(dynamicWorldParams.LocalSceneDevelopmentRealm);
container.reloadSceneController = new ECSReloadScene(staticContainer.ScenesCache, globalWorld, playerEntity, localSceneDevelopment);
Expand Down
8 changes: 5 additions & 3 deletions Explorer/Assets/Scripts/Global/Dynamic/RealmController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class RealmController : IGlobalRealmController
private readonly PartitionDataContainer partitionDataContainer;
private readonly IScenesCache scenesCache;
private readonly SceneAssetLock sceneAssetLock;
private readonly IComponentPool<PartitionComponent> partitionComponentPool;

private GlobalWorld? globalWorld;
private Entity realmEntity;
Expand Down Expand Up @@ -77,7 +78,8 @@ public RealmController(
IScenesCache scenesCache,
PartitionDataContainer partitionDataContainer,
SceneAssetLock sceneAssetLock,
IDebugContainerBuilder debugContainerBuilder)
IDebugContainerBuilder debugContainerBuilder,
IComponentPool<PartitionComponent> partitionComponentPool)
{
this.web3IdentityCache = web3IdentityCache;
this.webRequestController = webRequestController;
Expand All @@ -89,7 +91,7 @@ public RealmController(
this.scenesCache = scenesCache;
this.partitionDataContainer = partitionDataContainer;
this.sceneAssetLock = sceneAssetLock;

this.partitionComponentPool = partitionComponentPool;
realmNavigatorDebugView = new RealmNavigatorDebugView(debugContainerBuilder);
}

Expand Down Expand Up @@ -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)
Expand Down
Loading
Loading