public bool growIfNeeded() //must be called under write lock { var loadf = LoadFactor; var options = Table.m_Options; if (loadf > options.LoadFactorHWM) { var newCapacity = IntMath.GetCapacityFactoredToPrime(Entries.Length, options.GrowthFactor); if (options.MaximumCapacity > 0) { var capacityPerBucket = (int)(options.MaximumCapacity / BUCKETS); if (newCapacity > capacityPerBucket) { newCapacity = IntMath.GetAdjacentPrimeNumberLessThanOrEqualTo(capacityPerBucket); } } if (resize(newCapacity)) { Interlocked.Increment(ref Table.m_stat_Grew); m_LastGrownTime = DateTime.UtcNow; return(true); } } return(false); }
public void GetCapacityFactoredToPrime_2() { int cap = 16; while (cap < 2000000000) { cap = IntMath.GetCapacityFactoredToPrime(cap, 2d); Console.WriteLine(cap); Aver.IsTrue(IntMath.IsPrime(cap)); } }
public void GetCapacityFactoredToPrime_1() { Aver.AreEqual(11, IntMath.GetCapacityFactoredToPrime(4, 2d)); Aver.AreEqual(37, IntMath.GetCapacityFactoredToPrime(16, 2d)); Aver.AreEqual(59, IntMath.GetCapacityFactoredToPrime(16, 3d)); Aver.AreEqual(521, IntMath.GetCapacityFactoredToPrime(256, 2d)); Aver.AreEqual(2333, IntMath.GetCapacityFactoredToPrime(1024, 2d)); Aver.AreEqual(521, IntMath.GetCapacityFactoredToPrime(1024, 0.5d)); Aver.AreEqual(293, IntMath.GetCapacityFactoredToPrime(1024, 0.25d)); Aver.AreEqual(2411033, IntMath.GetCapacityFactoredToPrime(1024 * 1024, 2d)); Aver.AreEqual(16777259, IntMath.GetCapacityFactoredToPrime(8 * 1024 * 1024, 2d)); Aver.AreEqual(33554467, IntMath.GetCapacityFactoredToPrime(16 * 1024 * 1024, 2d)); }
public bool shrinkIfNeeded() //must be called under write lock { const int SHRINK_WAIT_AFTER_LAST_GROW_SEC = 3 * 60; const int SHRINK_WAIT_AFTER_LAST_SHRINK_SEC = 2 * 60; var loadf = LoadFactor; var options = Table.m_Options; if (loadf < options.LoadFactorLWM) { var utcNow = DateTime.UtcNow; if ( ((utcNow - m_LastGrownTime).TotalSeconds > SHRINK_WAIT_AFTER_LAST_GROW_SEC) && ((utcNow - m_LastShrunkTime).TotalSeconds > SHRINK_WAIT_AFTER_LAST_SHRINK_SEC) ) { var newCapacity = COUNT > 0 ? IntMath.GetCapacityFactoredToPrime(Entries.Length, options.ShrinkFactor) : 0; if (options.MinimumCapacity > 0) { var capacityPerBucket = (int)(options.MinimumCapacity / BUCKETS); if (newCapacity < capacityPerBucket) { newCapacity = IntMath.GetPrimeCapacityOfAtLeast(capacityPerBucket); } } var initialCapacityPerBucket = (int)(options.InitialCapacity / BUCKETS); if (newCapacity < initialCapacityPerBucket) { newCapacity = IntMath.GetPrimeCapacityOfAtLeast(initialCapacityPerBucket); } if (resize(newCapacity)) { Interlocked.Increment(ref Table.m_stat_Shrunk); m_LastShrunkTime = utcNow; return(true); } } } return(false); }