StringBuilderPool.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using Best.HTTP.Shared.PlatformSupport.Threading;
  6. namespace Best.HTTP.Shared.PlatformSupport.Text
  7. {
  8. /// <summary>
  9. /// Implements pooling logic for <see cref="StringBuilder"/> instances.
  10. /// </summary>
  11. [Best.HTTP.Shared.PlatformSupport.IL2CPP.Il2CppEagerStaticClassConstructionAttribute]
  12. public static class StringBuilderPool
  13. {
  14. /// <summary>
  15. /// Setting this property to false the pooling mechanism can be disabled.
  16. /// </summary>
  17. public static bool IsEnabled
  18. {
  19. get { return _isEnabled; }
  20. set
  21. {
  22. _isEnabled = value;
  23. // When set to non-enabled remove all stored entries
  24. if (!_isEnabled)
  25. Clear();
  26. }
  27. }
  28. private static volatile bool _isEnabled = true;
  29. /// <summary>
  30. /// Buffer entries that released back to the pool and older than this value are moved when next maintenance is triggered.
  31. /// </summary>
  32. public static TimeSpan RemoveOlderThan = TimeSpan.FromSeconds(10);
  33. /// <summary>
  34. /// How often pool maintenance must run.
  35. /// </summary>
  36. public static TimeSpan RunMaintenanceEvery = TimeSpan.FromSeconds(5);
  37. private static DateTime lastMaintenance = DateTime.MinValue;
  38. private readonly static ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
  39. struct BuilderShelf
  40. {
  41. public StringBuilder builder;
  42. public DateTime released;
  43. public BuilderShelf(StringBuilder sb)
  44. {
  45. this.builder = sb;
  46. this.released = DateTime.Now;
  47. }
  48. }
  49. private static List<BuilderShelf> pooledBuilders = new List<BuilderShelf>();
  50. public static StringBuilder Get(int lengthHint)
  51. {
  52. if (!_isEnabled)
  53. return new StringBuilder(lengthHint);
  54. using (new WriteLock(rwLock))
  55. {
  56. for (int i = pooledBuilders.Count - 1; i >= 0; i--)
  57. {
  58. BuilderShelf shelf = pooledBuilders[i];
  59. if (shelf.builder.Capacity >= lengthHint)
  60. {
  61. pooledBuilders.RemoveAt(i);
  62. return shelf.builder;
  63. }
  64. }
  65. // no builder found with lengthHint, take the first available
  66. if (pooledBuilders.Count > 0)
  67. {
  68. BuilderShelf shelf = pooledBuilders[pooledBuilders.Count - 1];
  69. pooledBuilders.RemoveAt(pooledBuilders.Count - 1);
  70. return shelf.builder;
  71. }
  72. }
  73. return new StringBuilder(lengthHint);
  74. }
  75. public static void Release(StringBuilder builder)
  76. {
  77. if (builder == null)
  78. return;
  79. if (!_isEnabled)
  80. return;
  81. builder.Clear();
  82. using (new WriteLock(rwLock))
  83. pooledBuilders.Add(new BuilderShelf(builder));
  84. }
  85. public static string ReleaseAndGrab(StringBuilder builder)
  86. {
  87. if (builder == null)
  88. return null;
  89. var result = builder.ToString();
  90. if (!_isEnabled)
  91. return result;
  92. builder.Clear();
  93. using (new WriteLock(rwLock))
  94. pooledBuilders.Add(new BuilderShelf(builder));
  95. return result;
  96. }
  97. internal static void Maintain()
  98. {
  99. DateTime now = DateTime.Now;
  100. if (!_isEnabled || lastMaintenance + RunMaintenanceEvery > now)
  101. return;
  102. lastMaintenance = now;
  103. DateTime olderThan = now - RemoveOlderThan;
  104. using (new WriteLock(rwLock))
  105. {
  106. for (int i = 0; i < pooledBuilders.Count; i++)
  107. {
  108. BuilderShelf shelf = pooledBuilders[i];
  109. if (shelf.released < olderThan)
  110. pooledBuilders.RemoveAt(i--);
  111. }
  112. }
  113. }
  114. public static void Clear()
  115. {
  116. using (new WriteLock(rwLock))
  117. pooledBuilders.Clear();
  118. }
  119. }
  120. }