Beispiel #1
0
        public void OK_AccessStringKey()
        {
            var cache   = new Dictionary <string, int>(EqualityComparer <String> .Default);
            var strings = new List <string>();

            for (int i = 0; i < ELEM_COUNT; ++i)
            {
                strings.Add(i.ToString());
                cache.Add(strings[i], i);
            }

            Assert.That(() =>
            {
                EqualityComparer <String> .Default.GetHashCode(strings[0]);
            }, Is.Not.AllocatingGCMemory()
                        );

            //---------EQ.Defaul.GetHashCode(string var) will NOT generate gc---------//
            string s = "helloworld";

            EqualityComparer <string> .Default.GetHashCode(s);

            Assert.That(() =>
            {
                EqualityComparer <string> .Default.GetHashCode(s);
            }, Is.Not.AllocatingGCMemory()
                        );

            //---------EQ.Default.GetHashCode(string literal / constant string) will generate gc---------//
            EqualityComparer <string> .Default.GetHashCode("helloworld");

            Assert.That(() =>
            {
                EqualityComparer <string> .Default.GetHashCode("helloworld");
            }, Is.AllocatingGCMemory()
                        );

            Assert.That(() =>
            {
                int v = 0;
                for (int j = 0; j < ELEM_COUNT; ++j)
                {
                    v += cache[strings[j]];
                }
            },
                        Is.Not.AllocatingGCMemory()
                        );
        }
Beispiel #2
0
        public void GetHashCodeTest()
        {
            var localStr = "ABCDE";

            Assert.That(() => { 100.GetHashCode(); }, Is.Not.AllocatingGCMemory());
            Assert.That(() => { 100f.GetHashCode(); }, Is.Not.AllocatingGCMemory());
            Assert.That(() => { ((double)100).GetHashCode(); }, Is.Not.AllocatingGCMemory());
            Assert.That(() => { Vector3.one.GetHashCode(); }, Is.Not.AllocatingGCMemory());

            Assert.That(() => { EqualityComparer <Vector3> .Default.GetHashCode(Vector3.one * 2); }, Is.Not.AllocatingGCMemory());

            Assert.That(() => { "hello".GetHashCode(); }, Is.AllocatingGCMemory());                                          //bad for literal
            Assert.That(() => { "hello".GetHashCode(); }, Is.AllocatingGCMemory());                                          //bad for literal
            Assert.That(() => { EqualityComparer <string> .Default.GetHashCode(localStr); }, Is.Not.AllocatingGCMemory());   //good for string var
            Assert.That(() => { EqualityComparer <string> .Default.GetHashCode(_globalStr); }, Is.Not.AllocatingGCMemory()); //good for string var
            Assert.That(() => { EqualityComparer <string> .Default.GetHashCode(_constantStr); }, Is.AllocatingGCMemory());   //bad for string constant = literal
            Assert.That(() => { EqualityComparer <string> .Default.GetHashCode(_constantStr); }, Is.AllocatingGCMemory());   //bad for string constant = literal
            Assert.That(() => { EqualityComparer <string> .Default.GetHashCode("XXX"); }, Is.AllocatingGCMemory());          //bad for string literal
        }
Beispiel #3
0
        public void BAD_LinkedList()
        {
            var cont = new LinkedList <DummyObjA>();

            for (int i = 0; i < ELEM_COUNT; ++i)
            {
                cont.AddLast(_cache[i]);
            }

            cont.Clear();
            Assert.That(
                () => {
                for (int i = 0; i < ELEM_COUNT; ++i)
                {
                    cont.AddLast(_cache[i]);
                }
            }, Is.AllocatingGCMemory()
                );
        }
Beispiel #4
0
        public void BAD_SortedDictionary()
        {
            var cont = new SortedDictionary <int, DummyObjA>();

            for (int i = 0; i < ELEM_COUNT; ++i)
            {
                cont.Add(i, _cache[i]);
            }

            cont.Clear();
            Assert.That(
                () => {
                for (int i = 0; i < ELEM_COUNT; ++i)
                {
                    cont.Add(i, _cache[i]);
                }
            }, Is.AllocatingGCMemory()
                );
        }
Beispiel #5
0
        public void BAD_IList_Foreach()
        {
            var cont = new List <int>();

            for (int i = 0; i < ELEM_COUNT; ++i)
            {
                cont.Add(i);
            }

            Assert.That(() =>
            {
                int v            = 0;
                IList <int> icol = cont;
                foreach (var val in icol)
                {
                    v += val;
                }
            }, Is.AllocatingGCMemory()
                        );
        }
Beispiel #6
0
        public void BAD_AccessStructKey()
        {
            var cache = new Dictionary <Data, int>();

            for (int i = 0; i < ELEM_COUNT; ++i)
            {
                cache.Add(new Data(i, i), i);
            }

            Assert.That(() =>
            {
                int v = 0;
                for (int j = 0; j < ELEM_COUNT; ++j)
                {
                    v += cache[new Data(j, j)];
                }
            },
                        Is.AllocatingGCMemory()
                        );


            // GC.Collect();
            // long startMem = Profiler.GetMonoUsedSizeLong();

            // //------------------//
            // for(int i=0; i<LOOP_COUNT; ++i)
            // {
            //     cont.Clear();
            //     for(int j=0; j<ELEM_COUNT; ++j)
            //     {
            //         cont.Add(cache[new Data(j,j)]);
            //     }
            // }

            // long mem1 = Profiler.GetMonoUsedSizeLong();
            // Assert.That(mem1, Is.GreaterThan(startMem));

            // //------------------//
            // Debug.Log(string.Format("startMem = {0}, mem1 = {1}", startMem, mem1));
        }
Beispiel #7
0
        public void BAD_SortedList()
        {
            SortedList <int, string> lst = new SortedList <int, string>();

            for (int i = 0; i < ELEM_COUNT; ++i)
            {
                lst.Add(i, i.ToString());
            }

            foreach (var v in lst)
            {
            }                       //warmup

            Assert.That(
                () => {
                int sum = 0;
                foreach (var v in lst)
                {
                    sum += v.Key;
                }
            }, Is.AllocatingGCMemory()
                );
        }