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