Skip to content

Commit

Permalink
Updated ComponentsGenerator to use entity.CreateComponent()
Browse files Browse the repository at this point in the history
  • Loading branch information
sschmid committed Mar 6, 2016
1 parent b303420 commit e641cf1
Show file tree
Hide file tree
Showing 47 changed files with 360 additions and 405 deletions.
Expand Up @@ -115,8 +115,7 @@ public class ComponentsGenerator : IComponentCodeGenerator {
static string addAddMethods(ComponentInfo componentInfo) {
return componentInfo.isSingletonComponent ? string.Empty : buildString(componentInfo, @"
public Entity Add$Name($typedArgs) {
var componentPool = GetComponentPool($Ids.$Name);
var component = ($Type)(componentPool.Count > 0 ? componentPool.Pop() : new $Type());
var component = CreateComponent<$Type>($Ids.$Name);
$assign
return AddComponent($Ids.$Name, component);
}
Expand All @@ -126,8 +125,7 @@ public class ComponentsGenerator : IComponentCodeGenerator {
static string addReplaceMethods(ComponentInfo componentInfo) {
return componentInfo.isSingletonComponent ? string.Empty : buildString(componentInfo, @"
public Entity Replace$Name($typedArgs) {
var componentPool = GetComponentPool($Ids.$Name);
var component = ($Type)(componentPool.Count > 0 ? componentPool.Pop() : new $Type());
var component = CreateComponent<$Type>($Ids.$Name);
$assign
ReplaceComponent($Ids.$Name, component);
return this;
Expand Down
8 changes: 7 additions & 1 deletion Entitas/Entitas/Entity.cs
Expand Up @@ -257,7 +257,13 @@ public partial class Entity {
/// Returns a new or reusable component from the componentPool for the specified component index.
public IComponent CreateComponent(int index, Type type) {
var componentPool = GetComponentPool(index);
return (IComponent)(componentPool.Count > 0 ? componentPool.Pop() : Activator.CreateInstance(type));;
return (IComponent)(componentPool.Count > 0 ? componentPool.Pop() : Activator.CreateInstance(type));
}

/// Returns a new or reusable component from the componentPool for the specified component index.
public T CreateComponent<T>(int index) where T : new() {
var componentPool = GetComponentPool(index);
return componentPool.Count > 0 ? (T)componentPool.Pop() : new T();
}

internal void destroy() {
Expand Down
6 changes: 6 additions & 0 deletions Entitas/Entitas/Serialization/PublicMemberInfo.cs
Expand Up @@ -71,6 +71,12 @@ public static class PublicMemberInfoExtension {
return clone;
}

public static T PublicMemberClone<T>(this object obj) where T : new() {
var clone = new T();
CopyPublicMemberValues(obj, clone);
return clone;
}

public static void CopyPublicMemberValues(this object obj, object target) {
var type = obj.GetType();
var fieldInfos = getFieldInfos(type);
Expand Down
3 changes: 3 additions & 0 deletions PerformanceTests/PerformanceTests.csproj
Expand Up @@ -75,6 +75,8 @@
<Compile Include="PerformanceTests\Pool\PoolDestroyEntity.cs" />
<Compile Include="PerformanceTests\DataStructures\HashSetContainsAdd.cs" />
<Compile Include="PerformanceTests\Helper.cs" />
<Compile Include="PerformanceTests\Misc\NewInstanceT.cs" />
<Compile Include="PerformanceTests\Misc\NewInstanceActivator.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand All @@ -86,6 +88,7 @@
<Folder Include="PerformanceTests\Observer\" />
<Folder Include="PerformanceTests\Properties\" />
<Folder Include="PerformanceTests\Pool\" />
<Folder Include="PerformanceTests\Misc\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Entitas\Entitas.csproj">
Expand Down
22 changes: 22 additions & 0 deletions PerformanceTests/PerformanceTests/Misc/NewInstanceActivator.cs
@@ -0,0 +1,22 @@
using System;

public class NewInstanceActivator : IPerformanceTest {
const int n = 1000000;

Type _type;

public void Before() {
_type = typeof(ComponentA);
}

public void Run() {
for (int i = 0; i < n; i++) {
createInstance();
}
}

void createInstance() {
Activator.CreateInstance(_type);
}
}

17 changes: 17 additions & 0 deletions PerformanceTests/PerformanceTests/Misc/NewInstanceT.cs
@@ -0,0 +1,17 @@
public class NewInstanceT : IPerformanceTest {
const int n = 1000000;

public void Before() {
}

public void Run() {
for (int i = 0; i < n; i++) {
createInstance<ComponentA>();
}
}

void createInstance<T>() where T : new() {
new T();
}
}

5 changes: 5 additions & 0 deletions PerformanceTests/Program.cs
Expand Up @@ -26,6 +26,9 @@ class MainClass {

run<MatcherEquals>();
run<MatcherGetHashCode>();

// run<NewInstanceT>();
// run<NewInstanceActivator>();
// run<EmptyTest>();
//
// run<IterateHashetToArray>();
Expand Down Expand Up @@ -75,6 +78,8 @@ class MainClass {
// MatcherEquals: 171 ms
// MatcherGetHashCode: 17 ms
//
// NewInstanceT: 393 ms
// NewInstanceActivator: 542 ms
// IterateHashetToArray: 456 ms
// IterateHashSet: 774 ms
//
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

40 changes: 0 additions & 40 deletions Tests/Tests/Entitas.CodeGenerator/Fixtures/Generated/Pools.cs

This file was deleted.

0 comments on commit e641cf1

Please sign in to comment.