|
1 | 1 | using System; |
| 2 | +using System.Collections.Concurrent; |
2 | 3 | using GameKit.Dependencies.Utilities.Types; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Runtime.CompilerServices; |
@@ -155,18 +156,12 @@ public static void Store(Dictionary<T1, T2> value) |
155 | 156 | /// <summary> |
156 | 157 | /// Cache for ResettableRingBuffer. |
157 | 158 | /// </summary> |
158 | | - [ThreadStatic] |
159 | | - private static readonly Stack<ResettableRingBuffer<T>> _resettableRingBufferCache; |
| 159 | + private static readonly ConcurrentStack<ResettableRingBuffer<T>> _resettableRingBufferCache = new(); |
160 | 160 | /// <summary> |
161 | 161 | /// Maximum number of entries allowed for the cache. |
162 | 162 | /// </summary> |
163 | 163 | private const int MAXIMUM_CACHE_COUNT = 50; |
164 | | - |
165 | | - static ResettableCollectionCaches() |
166 | | - { |
167 | | - _resettableRingBufferCache = new(); |
168 | | - } |
169 | | - |
| 164 | + |
170 | 165 | /// <summary> |
171 | 166 | /// Retrieves a collection. |
172 | 167 | /// </summary> |
@@ -465,18 +460,12 @@ public static class CollectionCaches<T1, T2> |
465 | 460 | /// <summary> |
466 | 461 | /// Cache for dictionaries. |
467 | 462 | /// </summary> |
468 | | - [ThreadStatic] |
469 | | - private static readonly Stack<Dictionary<T1, T2>> _dictionaryCache; |
| 463 | + private static readonly ConcurrentStack<Dictionary<T1, T2>> _dictionaryCache = new(); |
470 | 464 | /// <summary> |
471 | 465 | /// Maximum number of entries allowed for the cache. |
472 | 466 | /// </summary> |
473 | 467 | private const int MAXIMUM_CACHE_COUNT = 50; |
474 | 468 |
|
475 | | - static CollectionCaches() |
476 | | - { |
477 | | - _dictionaryCache = new(); |
478 | | - } |
479 | | - |
480 | 469 | /// <summary> |
481 | 470 | /// Retrieves a collection. |
482 | 471 | /// </summary> |
@@ -525,33 +514,27 @@ public static class CollectionCaches<T> |
525 | 514 | /// <summary> |
526 | 515 | /// Cache for arrays. |
527 | 516 | /// </summary> |
528 | | - [ThreadStatic] |
529 | | - private static readonly Stack<T[]> _arrayCache; |
| 517 | + private static readonly ConcurrentStack<T[]> _arrayCache = new(); |
530 | 518 | /// <summary> |
531 | 519 | /// Cache for lists. |
532 | 520 | /// </summary> |
533 | | - [ThreadStatic] |
534 | | - private static readonly Stack<List<T>> _listCache; |
| 521 | + private static readonly ConcurrentStack<List<T>> _listCache = new(); |
535 | 522 | /// <summary> |
536 | 523 | /// Cache for sortedset. |
537 | 524 | /// </summary> |
538 | | - [ThreadStatic] |
539 | | - private static readonly Stack<SortedSet<T>> _sortedSetCache; |
| 525 | + private static readonly ConcurrentStack<SortedSet<T>> _sortedSetCache = new(); |
540 | 526 | /// <summary> |
541 | 527 | /// Cache for queues. |
542 | 528 | /// </summary> |
543 | | - [ThreadStatic] |
544 | | - private static readonly Stack<Queue<T>> _queueCache; |
| 529 | + private static readonly ConcurrentStack<Queue<T>> _queueCache = new(); |
545 | 530 | /// <summary> |
546 | 531 | /// Cache for queues. |
547 | 532 | /// </summary> |
548 | | - [ThreadStatic] |
549 | | - private static readonly Stack<BasicQueue<T>> _basicQueueCache; |
| 533 | + private static readonly ConcurrentStack<BasicQueue<T>> _basicQueueCache = new(); |
550 | 534 | /// <summary> |
551 | 535 | /// Cache for hashset. |
552 | 536 | /// </summary> |
553 | | - [ThreadStatic] |
554 | | - private static readonly Stack<HashSet<T>> _hashSetCache; |
| 537 | + private static readonly ConcurrentStack<HashSet<T>> _hashSetCache = new(); |
555 | 538 | /// <summary> |
556 | 539 | /// Maximum number of entries allowed for the cache. |
557 | 540 | /// </summary> |
@@ -861,18 +844,12 @@ public static void Store(HashSet<T> value) |
861 | 844 | /// <summary> |
862 | 845 | /// Stack to use. |
863 | 846 | /// </summary> |
864 | | - [ThreadStatic] |
865 | | - private static readonly Stack<T> _stack; |
| 847 | + private static readonly ConcurrentStack<T> _stack = new(); |
866 | 848 | /// <summary> |
867 | 849 | /// Maximum number of entries allowed for the cache. |
868 | 850 | /// </summary> |
869 | 851 | private const int MAXIMUM_CACHE_COUNT = 50; |
870 | 852 |
|
871 | | - static ObjectCaches() |
872 | | - { |
873 | | - _stack = new(); |
874 | | - } |
875 | | - |
876 | 853 | /// <summary> |
877 | 854 | /// Returns a value from the stack or creates an instance when the stack is empty. |
878 | 855 | /// </summary> |
|
0 commit comments