No Description
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.

PooledHashSet.cs 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Assertions;
  4. namespace UnityEditor.ShaderGraph
  5. {
  6. class PooledHashSet<T> : HashSet<T>, IDisposable
  7. {
  8. static Stack<PooledHashSet<T>> s_Pool = new Stack<PooledHashSet<T>>();
  9. bool m_Active;
  10. PooledHashSet() { }
  11. public static PooledHashSet<T> Get()
  12. {
  13. if (s_Pool.Count == 0)
  14. {
  15. return new PooledHashSet<T> { m_Active = true };
  16. }
  17. var list = s_Pool.Pop();
  18. list.m_Active = true;
  19. #if DEBUG
  20. GC.ReRegisterForFinalize(list);
  21. #endif
  22. return list;
  23. }
  24. public void Dispose()
  25. {
  26. Assert.IsTrue(m_Active);
  27. m_Active = false;
  28. Clear();
  29. s_Pool.Push(this);
  30. #if DEBUG
  31. GC.SuppressFinalize(this);
  32. #endif
  33. }
  34. // Destructor causes some GC alloc so only do this sanity check in debug build
  35. #if DEBUG
  36. ~PooledHashSet()
  37. {
  38. throw new InvalidOperationException($"{nameof(PooledHashSet<T>)} must be disposed manually.");
  39. }
  40. #endif
  41. }
  42. }