Skip to content

Commit 332a3aa

Browse files
authored
UPM package version 3.4.5
1 parent 6dd6020 commit 332a3aa

20 files changed

+261
-149
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
# Svelto.ECS Changelog
22
All notable changes to this project will be documented in this file. Changes are listed in random order of importance.
33

4+
## [3.4.5] - 04-2023
5+
* improved code after internal data refactoring and fixed some bugs
6+
* Added static DynamicEntityDescriptor method to create a new DynamicEntityDescriptor without passing an array of components
7+
* factory method to build entities without entity descriptor is now internal
8+
9+
## [3.4.4] - 04-2023
10+
11+
* refactored internal datastructures
12+
* added IReactOnDisposeEx interface
13+
* added code to warmup all the entity descriptors at startup to avoid first time allocations when an entitydescriptor is used for the very first time
14+
* added the option to iterate transient filters per component like it already happens with persistent filters. Transient filters are tracked optionally.
15+
* fixed huge bug in the filter enumerator, truly surprised this never showed up
16+
417
## [3.4.2] - 03-2023
518

619
* removed static caches used in performance critical paths as they were causing unexpected performance issues (the fetching of static data is slower than i imagined)

Core/ComponentBuilder.cs

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Reflection;
5-
using System.Runtime.CompilerServices;
64
using System.Threading;
75
using DBC.ECS;
86
using Svelto.Common;
@@ -26,104 +24,6 @@ public int GetHashCode(IComponentBuilder obj)
2624
}
2725
}
2826

29-
public static class BurstCompatibleCounter
30-
{
31-
public static int counter;
32-
}
33-
34-
static public class ComponentTypeMap
35-
{
36-
static readonly FasterDictionary<RefWrapper<Type>, ComponentID> _componentTypeMap = new FasterDictionary<RefWrapper<Type>, ComponentID>();
37-
static readonly FasterDictionary<ComponentID, Type> _reverseComponentTypeMap = new FasterDictionary<ComponentID, Type>();
38-
39-
public static void Add(Type type, ComponentID idData)
40-
{
41-
_componentTypeMap.Add(type, idData);
42-
_reverseComponentTypeMap.Add(idData, type);
43-
}
44-
45-
public static ComponentID FetchID(Type type)
46-
{
47-
return _componentTypeMap[type];
48-
}
49-
50-
public static Type FetchType(ComponentID id)
51-
{
52-
return _reverseComponentTypeMap[id];
53-
}
54-
}
55-
56-
public class ComponentTypeID<T> where T : struct, _IInternalEntityComponent
57-
{
58-
static readonly SharedStaticWrapper<ComponentID, ComponentTypeID<T>> _id;
59-
60-
public static ComponentID id
61-
{
62-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
get => _id.Data;
64-
}
65-
66-
//todo: any reason to not do this? If I don't, I cannot Create filters in ready functions and
67-
//I have to remove the CreateFilter method
68-
static ComponentTypeID()
69-
{
70-
Init();
71-
}
72-
73-
#if UNITY_BURST
74-
[Unity.Burst.BurstDiscard]
75-
//SharedStatic values must be initialized from not burstified code
76-
#endif
77-
static void Init()
78-
{
79-
_id.Data = Interlocked.Increment(ref BurstCompatibleCounter.counter);
80-
ComponentTypeMap.Add(typeof(T), id);
81-
}
82-
}
83-
84-
sealed class ComponentIDDebugProxy
85-
{
86-
public ComponentIDDebugProxy(ComponentID id)
87-
{
88-
this._id = id;
89-
}
90-
91-
public Type type => ComponentTypeMap.FetchType(_id);
92-
93-
readonly ComponentID _id;
94-
}
95-
96-
[DebuggerTypeProxy(typeof(ComponentIDDebugProxy))]
97-
public struct ComponentID: IEquatable<ComponentID>
98-
{
99-
public static implicit operator int(ComponentID id)
100-
{
101-
return id._id;
102-
}
103-
104-
public static implicit operator uint(ComponentID id)
105-
{
106-
return (uint)id._id;
107-
}
108-
109-
public static implicit operator ComponentID(int id)
110-
{
111-
return new ComponentID() {_id = id};
112-
}
113-
114-
public bool Equals(ComponentID other)
115-
{
116-
return _id == other._id;
117-
}
118-
119-
public override int GetHashCode()
120-
{
121-
return _id;
122-
}
123-
124-
int _id;
125-
}
126-
12727
public class ComponentBuilder<T> : IComponentBuilder where T : struct, _IInternalEntityComponent
12828
{
12929
internal static readonly Type ENTITY_COMPONENT_TYPE;

Core/ComponentID.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace Svelto.ECS
5+
{
6+
sealed class ComponentIDDebugProxy
7+
{
8+
public ComponentIDDebugProxy(ComponentID id)
9+
{
10+
this._id = id;
11+
}
12+
13+
public Type type => ComponentTypeMap.FetchType(_id);
14+
15+
readonly ComponentID _id;
16+
}
17+
18+
19+
[DebuggerTypeProxy(typeof(ComponentIDDebugProxy))]
20+
public struct ComponentID: IEquatable<ComponentID>
21+
{
22+
public static implicit operator int(ComponentID id)
23+
{
24+
return id._id;
25+
}
26+
27+
public static implicit operator uint(ComponentID id)
28+
{
29+
return (uint)id._id;
30+
}
31+
32+
public static implicit operator ComponentID(int id)
33+
{
34+
return new ComponentID()
35+
{
36+
_id = id
37+
};
38+
}
39+
40+
public bool Equals(ComponentID other)
41+
{
42+
return _id == other._id;
43+
}
44+
45+
public override int GetHashCode()
46+
{
47+
return _id;
48+
}
49+
50+
int _id;
51+
}
52+
}

Core/ComponentID.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/ComponentTypeID.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Threading;
3+
using Svelto.Common;
4+
using Svelto.ECS.Internal;
5+
6+
namespace Svelto.ECS
7+
{
8+
public static class BurstCompatibleCounter
9+
{
10+
public static int counter;
11+
}
12+
13+
public class ComponentTypeID<T> where T : struct, _IInternalEntityComponent
14+
{
15+
static readonly SharedStaticWrapper<ComponentID, ComponentTypeID<T>> _id;
16+
17+
public static ComponentID id
18+
{
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20+
get => _id.Data;
21+
}
22+
23+
static ComponentTypeID()
24+
{
25+
Init();
26+
}
27+
28+
#if UNITY_BURST
29+
[Unity.Burst.BurstDiscard]
30+
//SharedStatic values must be initialized from not burstified code
31+
#endif
32+
static void Init()
33+
{
34+
_id.Data = Interlocked.Increment(ref BurstCompatibleCounter.counter);
35+
ComponentTypeMap.Add(typeof(T), id);
36+
}
37+
}
38+
}

Core/ComponentTypeID.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/ComponentTypeMap.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using Svelto.DataStructures;
4+
5+
namespace Svelto.ECS
6+
{
7+
public static class ComponentTypeMap
8+
{
9+
static readonly FasterDictionary<RefWrapper<Type>, ComponentID> _componentTypeMap = new FasterDictionary<RefWrapper<Type>, ComponentID>();
10+
static readonly FasterDictionary<ComponentID, Type> _reverseComponentTypeMap = new FasterDictionary<ComponentID, Type>();
11+
12+
public static void Add(Type type, ComponentID idData)
13+
{
14+
_componentTypeMap.Add(type, idData);
15+
_reverseComponentTypeMap.Add(idData, type);
16+
}
17+
18+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
19+
public static ComponentID FetchID(Type type)
20+
{
21+
if (_componentTypeMap.TryGetValue(type, out var index) == false)
22+
{
23+
//if warming up is working correctly, this should never happen
24+
var componentType = typeof(ComponentTypeID<>).MakeGenericType(type);
25+
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(componentType.TypeHandle);
26+
return _componentTypeMap[type];
27+
}
28+
29+
return index;
30+
}
31+
32+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
33+
public static Type FetchType(ComponentID id)
34+
{
35+
return _reverseComponentTypeMap[id];
36+
}
37+
}
38+
}

Core/ComponentTypeMap.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/EnginesRoot.GenericEntityFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Svelto.ECS
77
{
88
public partial class EnginesRoot
99
{
10-
class GenericEntityFactory : IEntityFactory
10+
class GenericEntityFactory : IEntityFactory, IEntitySerializationFactory
1111
{
1212
public GenericEntityFactory(EnginesRoot weakReference)
1313
{

Core/EntityDescriptor/DynamicEntityDescriptor.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,48 @@ namespace Svelto.ECS
1414
/// <typeparam name="TType"></typeparam>
1515
public struct DynamicEntityDescriptor<TType> : IDynamicEntityDescriptor where TType : IEntityDescriptor, new()
1616
{
17-
internal DynamicEntityDescriptor(bool isExtendible) : this()
17+
public static DynamicEntityDescriptor<TType> CreateDynamicEntityDescriptor()
1818
{
19+
var entityDescriptor = new DynamicEntityDescriptor<TType>();
20+
1921
var defaultEntities = EntityDescriptorTemplate<TType>.realDescriptor.componentsToBuild;
2022
var length = defaultEntities.Length;
2123

2224
if (FetchEntityInfoComponent(defaultEntities) == -1)
2325
{
24-
_componentsToBuild = new IComponentBuilder[length + 1];
26+
entityDescriptor._componentsToBuild = new IComponentBuilder[length + 1];
2527

26-
Array.Copy(defaultEntities, 0, _componentsToBuild, 0, length);
28+
Array.Copy(defaultEntities, 0, entityDescriptor._componentsToBuild, 0, length);
2729
//assign it after otherwise the previous copy will overwrite the value in case the item
2830
//is already present
29-
_componentsToBuild[length] = new ComponentBuilder<EntityInfoComponent>(new EntityInfoComponent
31+
entityDescriptor._componentsToBuild[length] = new ComponentBuilder<EntityInfoComponent>(new EntityInfoComponent
3032
{
31-
componentsToBuild = _componentsToBuild
33+
componentsToBuild = entityDescriptor._componentsToBuild
3234
});
3335
}
3436
else
3537
{
36-
_componentsToBuild = new IComponentBuilder[length];
38+
entityDescriptor._componentsToBuild = new IComponentBuilder[length];
3739

38-
Array.Copy(defaultEntities, 0, _componentsToBuild, 0, length);
40+
Array.Copy(defaultEntities, 0, entityDescriptor._componentsToBuild, 0, length);
3941
}
42+
43+
return entityDescriptor;
4044
}
4145

42-
public DynamicEntityDescriptor(IComponentBuilder[] extraEntityBuilders) : this(true)
46+
public DynamicEntityDescriptor(IComponentBuilder[] extraEntityBuilders)
4347
{
48+
this = CreateDynamicEntityDescriptor();
49+
4450
var extraEntitiesLength = extraEntityBuilders.Length;
4551

4652
_componentsToBuild = Construct(extraEntitiesLength, extraEntityBuilders);
4753
}
4854

49-
public DynamicEntityDescriptor(FasterList<IComponentBuilder> extraEntityBuilders) : this(true)
55+
public DynamicEntityDescriptor(FasterList<IComponentBuilder> extraEntityBuilders)
5056
{
57+
this = CreateDynamicEntityDescriptor();
58+
5159
var extraEntities = extraEntityBuilders.ToArrayFast(out _);
5260
var extraEntitiesLength = extraEntityBuilders.count;
5361

0 commit comments

Comments
 (0)