Skip to content

Conversation

DrSmugleaf
Copy link
Collaborator

@DrSmugleaf DrSmugleaf commented May 8, 2023

Also makes Chunk.Has and Chunk.CopyComponents faster.
Heavily inspired by flecs. See https://github.com/SanderMertens/flecs/blob/master/docs/Relationships.md#id-ranges and https://ajmmertens.medium.com/building-an-ecs-2-archetypes-and-vectorization-fe21690805f9
When the component type id is below 256 an array is used, otherwise a dictionary is used, similar to flecs.
The array is only allocated after the first time that an archetype is modified, and only to the size that it needs to fit the component type id with each usage.
A preprocessor directive is used in case CollectionsMarshal does not exist (pre .NET 5)

BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19045.2846)
AMD Ryzen 9 7900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=7.0.203
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2


Old:

Method Amount Mean Error StdDev Median Allocated
AddArchetypeOne 10000 1.409 ms 0.0280 ms 0.0526 ms 1.387 ms 2 B
AddArchetypeFive 10000 3.165 ms 0.0084 ms 0.0070 ms 3.165 ms 3 B
AddArchetypeOne 100000 13.738 ms 0.1541 ms 0.1442 ms 13.808 ms 13 B
AddArchetypeFive 100000 31.367 ms 0.0547 ms 0.0427 ms 31.373 ms 51 B
AddArchetypeOne 1000000 135.100 ms 0.3742 ms 0.3500 ms 135.049 ms 204 B
AddArchetypeFive 1000000 313.442 ms 0.8176 ms 0.7648 ms 313.690 ms 816 B

New:

Method Amount Mean Error StdDev Allocated
AddArchetypeOne 10000 571.5 us 6.90 us 5.76 us 1 B
AddArchetypeFive 10000 1,416.0 us 7.15 us 5.58 us 2 B
AddArchetypeOne 100000 5,651.4 us 10.90 us 9.66 us 6 B
AddArchetypeFive 100000 13,616.9 us 48.06 us 42.60 us 13 B
AddArchetypeOne 1000000 56,841.2 us 185.63 us 155.01 us 91 B
AddArchetypeFive 1000000 139,875.7 us 2,063.16 us 1,929.88 us 204 B

@DrSmugleaf
Copy link
Collaborator Author

As a comparison, these are the same benchmarks without the World.Add or Archetype changes, using only the changes in Chunk.cs:

Method Amount Mean Error StdDev Allocated
AddArchetypeOne 10000 862.6 us 16.83 us 16.53 us 1 B
AddArchetypeFive 10000 1,814.8 us 4.53 us 3.78 us 2 B
AddArchetypeOne 100000 8,507.4 us 131.22 us 122.74 us 13 B
AddArchetypeFive 100000 17,359.0 us 304.09 us 284.45 us 26 B
AddArchetypeOne 1000000 86,454.2 us 875.31 us 818.77 us 117 B
AddArchetypeFive 1000000 175,630.9 us 3,484.25 us 5,916.53 us 272 B

@genaray
Copy link
Owner

genaray commented May 8, 2023

Looks great! :)
Just out of curiosity, is it possible to create its own container/data class that switches between dictionary and array based on its element amount?

E.g. i just realize that there other aspects which could make use of a similar mechanic ^^

@DrSmugleaf
Copy link
Collaborator Author

DrSmugleaf commented May 8, 2023

Looks great! :) Just out of curiosity, is it possible to create its own container/data class that switches between dictionary and array based on its element amount?

E.g. i just realize that there other aspects which could make use of a similar mechanic ^^

Done in this commit

New benchmarks:

BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19045.2846)
AMD Ryzen 9 7900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=7.0.203
[Host] : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2
DefaultJob : .NET 7.0.5 (7.0.523.17405), X64 RyuJIT AVX2


Old:

Method Amount Mean Error StdDev Median Allocated
AddArchetypeOne 10000 589.0 us 10.90 us 22.02 us 579.9 us 1 B
AddArchetypeFive 10000 1,329.4 us 3.61 us 3.01 us 1,328.4 us 2 B
AddArchetypeOne 100000 5,649.0 us 13.75 us 11.48 us 5,648.7 us 6 B
AddArchetypeFive 100000 13,768.5 us 54.73 us 48.52 us 13,752.0 us 13 B
AddArchetypeOne 1000000 55,089.3 us 246.16 us 205.56 us 55,104.9 us 82 B
AddArchetypeFive 1000000 141,392.8 us 3,213.12 us 9,473.95 us 138,269.6 us 204 B

New:

Method Amount Mean Error StdDev Allocated
AddArchetypeOne 10000 575.0 us 0.80 us 0.71 us 1 B
AddArchetypeFive 10000 1,378.2 us 3.25 us 2.54 us 2 B
AddArchetypeOne 100000 5,787.7 us 38.90 us 32.49 us 6 B
AddArchetypeFive 100000 13,590.3 us 32.61 us 28.91 us 13 B
AddArchetypeOne 1000000 58,872.8 us 208.38 us 184.73 us 91 B
AddArchetypeFive 1000000 141,110.8 us 432.42 us 404.48 us 204 B

@genaray genaray merged commit ee8a3a4 into genaray:master May 12, 2023
@DrSmugleaf DrSmugleaf mentioned this pull request May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants