Esempio n. 1
0
        public void AutoDispose()
        {
            OwnedMemory <byte> owned = new AutoPooledMemory(1000);
            var memory = owned.Memory;

            Assert.Equal(false, owned.IsDisposed);
            var reservation = memory.Reserve();

            Assert.Equal(false, owned.IsDisposed);
            owned.Release();
            Assert.Equal(false, owned.IsDisposed);
            reservation.Dispose();
            Assert.Equal(true, owned.IsDisposed);
        }
Esempio n. 2
0
        public void ReservationPerformance(int number, int size, int threads, ReferenceCountingMethod m)
        {
            var iterations = 1000000;

            var o = ReferenceCountingSettings.OwnedMemory;

            ReferenceCountingSettings.OwnedMemory = m;

            Benchmark.Iterate(() => {
                var owners   = new OwnedMemory <byte> [number];
                var memories = new Memory <byte> [owners.Length];

                for (int i = 0; i < owners.Length; i++)
                {
                    owners[i]   = new AutoPooledMemory(size);
                    memories[i] = owners[i].Memory;
                }

                var tasks = new List <Task>(threads);
                for (int t = 0; t < threads; t++)
                {
                    tasks.Add(Task.Run(() => {
                        for (int k = 0; k < iterations / owners.Length; k++)
                        {
                            for (int i = 0; i < owners.Length; i++)
                            {
                                using (var reserve = memories[i].Reserve()) {
                                    var s = reserve.Span;
                                    for (int j = 0; j < owners.Length; j++)
                                    {
                                        s[j] = (byte)1;
                                    }
                                }
                            }
                        }
                    }));
                }

                Task.WaitAll(tasks.ToArray());

                for (int i = 0; i < owners.Length; i++)
                {
                    owners[i].Release();
                }
            });

            ReferenceCountingSettings.OwnedMemory = o;
        }