Mirror of Svelto.ECS because we're a fan of it
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.6KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. static class EntityDescriptorsWarmup
  8. {
  9. /// <summary>
  10. /// c# Static constructors are guaranteed to be thread safe
  11. /// Warmup all EntityDescriptors and ComponentTypeID classes to avoid huge overheads when they are first used
  12. /// </summary>
  13. internal static void WarmUp()
  14. {
  15. List<Assembly> assemblies = AssemblyUtility.GetCompatibleAssemblies();
  16. foreach (Assembly assembly in assemblies)
  17. {
  18. var typeOfEntityDescriptors = typeof(IEntityDescriptor);
  19. foreach (Type type in AssemblyUtility.GetTypesSafe(assembly))
  20. {
  21. if (type.IsInterface == false && type.IsAbstract == false && type.IsGenericType == false && typeOfEntityDescriptors.IsAssignableFrom(type))
  22. {
  23. try
  24. {
  25. //the main goal of this iteration is to warm up the component builders and descriptors
  26. //note: I could have just instanced the entity descriptor, but in this way I will warm up the EntityDescriptorTemplate too
  27. var warmup = typeof(EntityDescriptorTemplate<>).MakeGenericType(type);
  28. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(warmup.TypeHandle);
  29. }
  30. catch { }
  31. }
  32. }
  33. var typeOfComponents = typeof(_IInternalEntityComponent);
  34. //the main goal of this iteration is to warm up the component IDs
  35. foreach (Type type in AssemblyUtility.GetTypesSafe(assembly))
  36. {
  37. if (type.IsInterface == false && typeOfComponents.IsAssignableFrom(type)) //IsClass and IsSealed and IsAbstract means only static classes
  38. {
  39. try
  40. {
  41. var componentType = typeof(ComponentTypeID<>).MakeGenericType(type);
  42. //is called only once ever, even if runs multiple times.
  43. //this warms up the component builder. There could be different implementation of components builders for the same component type in theory
  44. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(componentType.TypeHandle);
  45. }
  46. catch { }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }