CacheItemPriorityQueue.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // Author(s):
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2009-2010 Novell, Inc (http://novell.com)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Xml;
  34. namespace System.Web.Caching
  35. {
  36. sealed partial class CacheItemPriorityQueue
  37. {
  38. const int INITIAL_HEAP_SIZE = 32;
  39. const int HEAP_RESIZE_THRESHOLD = 8192;
  40. CacheItem[] heap;
  41. int heapSize = 0;
  42. int heapCount = 0;
  43. // See comment for the cacheLock field at top of System.Web.Caching/Cache.cs
  44. ReaderWriterLockSlim queueLock;
  45. public int Count {
  46. get { return heapCount; }
  47. }
  48. public int Size {
  49. get { return heapSize; }
  50. }
  51. public CacheItemPriorityQueue ()
  52. {
  53. queueLock = new ReaderWriterLockSlim ();
  54. InitDebugMode ();
  55. }
  56. void ResizeHeap (int newSize)
  57. {
  58. CacheItem[] oldHeap = heap;
  59. Array.Resize <CacheItem> (ref heap, newSize);
  60. heapSize = newSize;
  61. // TODO: The code helps the GC in case the array is pinned. In such instance clearing
  62. // the old array will release references to the CacheItems stored in there. If the
  63. // array is not pinned, otoh, this is a waste of time.
  64. // Currently we don't know if the array is pinned or not so it's safer to always clear it.
  65. // However when we have more precise stack scanning the code should be
  66. // revisited.
  67. if (oldHeap != null) {
  68. ((IList)oldHeap).Clear ();
  69. oldHeap = null;
  70. }
  71. }
  72. CacheItem[] GetHeapWithGrow ()
  73. {
  74. if (heap == null) {
  75. heap = new CacheItem [INITIAL_HEAP_SIZE];
  76. heapSize = INITIAL_HEAP_SIZE;
  77. heapCount = 0;
  78. return heap;
  79. }
  80. if (heapCount >= heapSize)
  81. ResizeHeap (heapSize <<= 1);
  82. return heap;
  83. }
  84. CacheItem[] GetHeapWithShrink ()
  85. {
  86. if (heap == null)
  87. return null;
  88. if (heapSize > HEAP_RESIZE_THRESHOLD) {
  89. int halfTheSize = heapSize >> 1;
  90. if (heapCount < halfTheSize)
  91. ResizeHeap (halfTheSize + (heapCount / 3));
  92. }
  93. return heap;
  94. }
  95. public void Enqueue (CacheItem item)
  96. {
  97. if (item == null)
  98. return;
  99. CacheItem[] heap;
  100. try {
  101. queueLock.EnterWriteLock ();
  102. heap = GetHeapWithGrow ();
  103. heap [heapCount++] = item;
  104. BubbleUp (heap);
  105. AddSequenceEntry (item, EDSequenceEntryType.Enqueue);
  106. } finally {
  107. // See comment at the top of the file, above queueLock declaration
  108. queueLock.ExitWriteLock ();
  109. }
  110. }
  111. public CacheItem Dequeue ()
  112. {
  113. CacheItem ret = null;
  114. CacheItem[] heap;
  115. int index;
  116. try {
  117. queueLock.EnterWriteLock ();
  118. heap = GetHeapWithShrink ();
  119. if (heap == null || heapCount == 0)
  120. return null;
  121. ret = heap [0];
  122. index = --heapCount;
  123. heap [0] = heap [index];
  124. heap [index] = null;
  125. if (heapCount > 0)
  126. BubbleDown (heap);
  127. AddSequenceEntry (ret, EDSequenceEntryType.Dequeue);
  128. return ret;
  129. } finally {
  130. // See comment at the top of the file, above queueLock declaration
  131. queueLock.ExitWriteLock ();
  132. }
  133. }
  134. public CacheItem Peek ()
  135. {
  136. CacheItem ret;
  137. try {
  138. queueLock.EnterReadLock ();
  139. if (heap == null || heapCount == 0)
  140. return null;
  141. ret = heap [0];
  142. AddSequenceEntry (ret, EDSequenceEntryType.Peek);
  143. return ret;
  144. } finally {
  145. // See comment at the top of the file, above queueLock declaration
  146. queueLock.ExitReadLock ();
  147. }
  148. }
  149. void BubbleDown (CacheItem[] heap)
  150. {
  151. int index = 0;
  152. int left = 1;
  153. int right = 2;
  154. CacheItem item = heap [0];
  155. int selected = (right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt) ? 2 : 1;
  156. while (selected < heapCount && heap [selected].ExpiresAt < item.ExpiresAt) {
  157. heap [index] = heap [selected];
  158. index = selected;
  159. left = (index << 1) + 1;
  160. right = left + 1;
  161. selected = right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt ? right : left;
  162. }
  163. heap [index] = item;
  164. }
  165. void BubbleUp (CacheItem[] heap)
  166. {
  167. int index, parentIndex;
  168. CacheItem parent, item;
  169. if (heapCount <= 1)
  170. return;
  171. index = heapCount - 1;
  172. parentIndex = (index - 1) >> 1;
  173. item = heap [index];
  174. while (index > 0) {
  175. parent = heap [parentIndex];
  176. if (heap [index].ExpiresAt >= parent.ExpiresAt)
  177. break;
  178. heap [index] = parent;
  179. index = parentIndex;
  180. parentIndex = (index - 1) >> 1;
  181. }
  182. heap [index] = item;
  183. }
  184. }
  185. }