Exemplo n.º 1
0
 static LocalCacheTable()
 {
     s_GetPutLocks = new object[IntMath.GetPrimeCapacityOfAtLeast(System.Environment.ProcessorCount * 2)];
     for (var i = 0; i < s_GetPutLocks.Length; i++)
     {
         s_GetPutLocks[i] = new object();
     }
 }
Exemplo n.º 2
0
            public _bucket(LocalCacheTable <TKey> table)
            {
                Table = table;
                var capacity = (int)(table.m_Options.InitialCapacity) / BUCKETS;

                capacity = IntMath.GetPrimeCapacityOfAtLeast(capacity);
                Entries  = makeEntriesArray(capacity);
            }
Exemplo n.º 3
0
 public void GetPrimeCapacityOfAtLeast_2()
 {
     for (var i = 2; i < 1000000; i++)
     {
         var cap = IntMath.GetPrimeCapacityOfAtLeast(i);
         Aver.IsTrue(cap >= i);
         Aver.IsTrue(cap < i * 2);
         Aver.IsTrue(IntMath.IsPrime(cap));
     }
 }
Exemplo n.º 4
0
        public void GetPrimeCapacityOfAtLeast_1()
        {
            Aver.AreEqual(7, IntMath.GetPrimeCapacityOfAtLeast(4));
            Aver.AreEqual(59, IntMath.GetPrimeCapacityOfAtLeast(48));
            Aver.AreEqual(71, IntMath.GetPrimeCapacityOfAtLeast(64));

            Aver.AreEqual(131, IntMath.GetPrimeCapacityOfAtLeast(128));
            Aver.AreEqual(131, IntMath.GetPrimeCapacityOfAtLeast(129));
            Aver.AreEqual(131, IntMath.GetPrimeCapacityOfAtLeast(130));
            Aver.AreEqual(131, IntMath.GetPrimeCapacityOfAtLeast(131));
            Aver.AreEqual(163, IntMath.GetPrimeCapacityOfAtLeast(132));

            Aver.AreEqual(672827, IntMath.GetPrimeCapacityOfAtLeast(672800));

            Aver.AreEqual(334231259, IntMath.GetPrimeCapacityOfAtLeast(334231259));
            Aver.AreEqual(334231291, IntMath.GetPrimeCapacityOfAtLeast(334231260));
        }
Exemplo n.º 5
0
            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);
            }