Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.5KB

  1. using System;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. public partial class EnginesRoot
  7. {
  8. internal class DoubleBufferedEntitiesToAdd
  9. {
  10. const int MaximumNumberOfItemsPerFrameBeforeToClear = 100;
  11. internal void Swap()
  12. {
  13. Swap(ref current, ref other);
  14. Swap(ref currentEntitiesCreatedPerGroup, ref otherEntitiesCreatedPerGroup);
  15. }
  16. void Swap<T>(ref T item1, ref T item2)
  17. {
  18. var toSwap = item2;
  19. item2 = item1;
  20. item1 = toSwap;
  21. }
  22. public void ClearOther()
  23. {
  24. //do not clear the groups created so far, they will be reused, unless they are too many!
  25. var otherCount = other.Count;
  26. if (otherCount > MaximumNumberOfItemsPerFrameBeforeToClear)
  27. {
  28. otherEntitiesCreatedPerGroup.FastClear();
  29. other.FastClear();
  30. return;
  31. }
  32. var otherValuesArray = other.valuesArray;
  33. for (int i = 0; i < otherCount; ++i)
  34. {
  35. var safeDictionariesCount = otherValuesArray[i].Count;
  36. var safeDictionaries = otherValuesArray[i].valuesArray;
  37. //do not remove the dictionaries of entities per type created so far, they will be reused
  38. if (safeDictionariesCount <= MaximumNumberOfItemsPerFrameBeforeToClear)
  39. {
  40. for (int j = 0; j < safeDictionariesCount; ++j)
  41. {
  42. //clear the dictionary of entities create do far (it won't allocate though)
  43. safeDictionaries[j].FastClear();
  44. }
  45. }
  46. else
  47. {
  48. otherValuesArray[i].FastClear();
  49. }
  50. }
  51. otherEntitiesCreatedPerGroup.FastClear();
  52. }
  53. internal FasterDictionary<uint, uint> currentEntitiesCreatedPerGroup;
  54. internal FasterDictionary<uint, uint> otherEntitiesCreatedPerGroup;
  55. internal FasterDictionary<uint, FasterDictionary<RefWrapper<Type>, ITypeSafeDictionary>> current;
  56. internal FasterDictionary<uint, FasterDictionary<RefWrapper<Type>, ITypeSafeDictionary>> other;
  57. readonly FasterDictionary<uint, FasterDictionary<RefWrapper<Type>, ITypeSafeDictionary>>
  58. _entityViewsToAddBufferA =
  59. new FasterDictionary<uint, FasterDictionary<RefWrapper<Type>, ITypeSafeDictionary>>();
  60. readonly FasterDictionary<uint, FasterDictionary<RefWrapper<Type>, ITypeSafeDictionary>>
  61. _entityViewsToAddBufferB =
  62. new FasterDictionary<uint, FasterDictionary<RefWrapper<Type>, ITypeSafeDictionary>>();
  63. readonly FasterDictionary<uint, uint> _entitiesCreatedPerGroupA = new FasterDictionary<uint, uint>();
  64. readonly FasterDictionary<uint, uint> _entitiesCreatedPerGroupB = new FasterDictionary<uint, uint>();
  65. public DoubleBufferedEntitiesToAdd()
  66. {
  67. currentEntitiesCreatedPerGroup = _entitiesCreatedPerGroupA;
  68. otherEntitiesCreatedPerGroup = _entitiesCreatedPerGroupB;
  69. current = _entityViewsToAddBufferA;
  70. other = _entityViewsToAddBufferB;
  71. }
  72. }
  73. }
  74. }