Esempio n. 1
0
        public void PipelineOfShared()
        {
            int sum = 0;

            using (var p = Pipeline.Create())
            {
                var pool = new SharedPool <int[]>(1);
                var g    = Generators
                           .Range(p, 0, 10)
                           .Process <int, Shared <int[]> >(
                    (i, e, emitter) =>
                {
                    using (var shared = pool.GetOrCreate(() => new int[1]))
                    {
                        shared.Resource[0] = i;
                        emitter.Post(shared, e.OriginatingTime);
                    }
                });
                g.Select(a => a.Resource[0]).Do(v => sum = sum + v);
                p.Run();

                Assert.AreEqual(5 * 9, sum);
                Assert.IsTrue(pool.TotalCount > 0);
                Assert.AreEqual(pool.TotalCount, pool.AvailableCount);
            }
        }
Esempio n. 2
0
        public void SerializeGraph()
        {
            var buffer = new BufferWriter(100);
            var pool   = new SharedPool <byte[]>(() => new byte[10], 1);

            Shared <byte[]> s = pool.GetOrCreate();

            s.Resource[0] = 255;
            s.Resource[9] = 128;

            var t = Tuple.Create(s, s);

            Serializer.Serialize(buffer, t, new SerializationContext());
            Tuple <Shared <byte[]>, Shared <byte[]> > target = null;

            Serializer.Deserialize(new BufferReader(buffer.Buffer), ref target, new SerializationContext());

            Assert.ReferenceEquals(target.Item1, target.Item2);
            Assert.AreEqual(255, target.Item1.Resource[0]);
            Assert.AreEqual(0, target.Item1.Resource[1]);
            Assert.AreEqual(0, target.Item1.Resource[8]);
            Assert.AreEqual(128, target.Item1.Resource[9]);
            s.Dispose();
            target.Item1.Dispose();
        }
Esempio n. 3
0
        public void JoinOfShared()
        {
            var sum = 0;

            using (var p = Pipeline.Create())
            {
                var pool = new SharedPool <int[]>(() => new int[1], 1);
                var g    = Generators.Range(p, 0, 10);
                var s1   = g.Process <int, Shared <int[]> >(
                    (i, e, emitter) =>
                {
                    using (var shared = pool.GetOrCreate())
                    {
                        shared.Resource[0] = i;
                        emitter.Post(shared, e.OriginatingTime);
                    }
                });

                var j = s1.Join(g);
                j.Select(t => t.Item2 - t.Item1.Resource[0]).Do(v => sum = sum + v);
                p.Run();

                Assert.AreEqual(0, sum);
                Assert.IsTrue(pool.TotalCount > 0);
                Assert.AreEqual(pool.TotalCount, pool.AvailableCount);
            }
        }
Esempio n. 4
0
        public void RefCountedPipeline()
        {
            var sharedPool = new SharedPool <int[]>(() => new int[ArraySize], ParallelBranchCount * TransformCount);

            this.RunPipeline <Shared <int[]> >(
                create: i => sharedPool.GetOrCreate(),
                initialize: (old, i) =>
            {
                old.Dispose();
                var tgt = sharedPool.GetOrCreate();
                Set(tgt.Resource, i);
                return(tgt);
            },
                increment: (old, a) =>
            {
                old.Dispose();
                var tgt = sharedPool.GetOrCreate();
                Inc(tgt.Resource, a.Resource, 1);
                return(tgt);
            },
                add: (old, a, b) =>
            {
                old.Dispose();
                var tgt = sharedPool.GetOrCreate();
                Add(tgt.Resource, a.Resource, b.Resource);
                return(tgt);
            },
                extract: a => a.Resource[0],
                validateNoLoss: true,
                validateSync: false);
        }
Esempio n. 5
0
        public void RefCountedTest()
        {
            var sharedPool = new SharedPool <UnmanagedBuffer>(() => UnmanagedBuffer.Allocate(100), 1);
            var shared     = sharedPool.GetOrCreate(); // refcount = 1

            // a private copy shoudl point to the same resource
            var otherShared = shared.DeepClone(); // refcount = 1 + 1

            Assert.AreNotEqual(shared, otherShared);
            Assert.AreEqual(shared.Inner, otherShared.Inner);
            Assert.AreEqual(shared.Resource, otherShared.Resource);

            // a clone should point to the same resource, but should not reuse the container
            var cloned = otherShared;

            Serializer.Clone(shared, ref cloned, new SerializationContext()); // refcount = 2 - 1 + 1
            Assert.AreNotEqual(shared, cloned);
            Assert.AreEqual(shared.Inner, cloned.Inner);
            Assert.AreEqual(shared.Resource, cloned.Resource);

            // disposing should not affect other copies
            shared.Dispose(); // refcount = 2 - 1
            Assert.AreEqual(0, sharedPool.AvailableCount);
            Assert.IsNull(shared.Inner);
            Assert.IsNull(shared.Resource);
            Assert.IsNotNull(cloned.Inner);
            Assert.IsNotNull(cloned.Resource);

            // disposing the last copy should return the resource to the pool
            cloned.Dispose(); // refcount = 1 - 1
            Assert.AreEqual(1, sharedPool.AvailableCount);
            Assert.IsNull(cloned.Inner);
            Assert.IsNull(cloned.Resource);
        }
Esempio n. 6
0
        public void AddRefReleaseTest()
        {
            SharedPool <object> pool = new SharedPool <object>(() => new int?(1234), 1);

            // Verify that nothing is available (no objects have been allocated in the pool yet)
            Assert.AreEqual(pool.AvailableCount, 0);

            // Create our first object
            var origObj = pool.GetOrCreate();

            Assert.AreEqual(pool.TotalCount, 1);     // One object in pool
            Assert.AreEqual(pool.AvailableCount, 0); // None available for use (since only object is in use)

            // Take another reference on the object
            var refObj = origObj.AddRef();

            // Sanity check that underlying resource is the same
            Assert.AreEqual(refObj.Resource, 1234);
            Assert.AreEqual(origObj.Resource, 1234);

            // Get rid of our second reference. There should still be none available
            // in the pool since the refObj.inner hasn't been released yet
            refObj.Dispose();
            Assert.AreEqual(pool.AvailableCount, 0);

            // Next release the original object. Now everything should be released
            origObj.Dispose();
            Assert.AreEqual(pool.AvailableCount, 1);

            Assert.AreEqual(origObj.Inner, null);
        }
Esempio n. 7
0
        public virtual void WriteString(string s)
        {
            var buf = SharedPool <byte> .Rent(Encoding.UTF8.GetByteCount(s));

            try
            {
                var numberOfBytes = Encoding.UTF8.GetBytes(s, 0, s.Length, buf, 0);

                WriteBinary(buf, 0, numberOfBytes);
            }
            finally
            {
                SharedPool <byte> .Return(buf);
            }
        }
Esempio n. 8
0
        public void DoubleDispose()
        {
            var sharedPool = new SharedPool <UnmanagedBuffer>(() => UnmanagedBuffer.Allocate(100), 1);
            var shared     = sharedPool.GetOrCreate();

            shared.Dispose();
            try
            {
                shared.Dispose();
                Assert.Fail("Expected an exception from the second Dispose call");
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is ObjectDisposedException);
            }
        }
Esempio n. 9
0
    public void Create(uint uid, int confId)
    {
        var chars = config.characters;
        int len   = chars.Length;

        for (int i = 0; i < len; i++)
        {
            if (chars[i].id == confId)
            {
                var e = SharedPool <Entity> .Get();

                e.Initial(uid, chars[i]);
                dict.Add(uid, e);
                break;
            }
        }
    }
        public override void WriteString(string str)
        {
            var buf = SharedPool <byte> .Rent(Encoding.UTF8.GetByteCount(str));

            try
            {
                var numberOfBytes = Encoding.UTF8.GetBytes(str, 0, str.Length, buf, 0);

                Int32ToVarInt((uint)numberOfBytes, ref PreAllocatedVarInt);
                Trans.Write(PreAllocatedVarInt.bytes, 0, PreAllocatedVarInt.count);
                Trans.Write(buf, 0, numberOfBytes);
            }
            finally
            {
                SharedPool <byte> .Return(buf);
            }
        }
Esempio n. 11
0
        public void Serialize()
        {
            var buffer = new BufferWriter(100);
            var pool   = new SharedPool <byte[]>(() => new byte[10], 1);

            Shared <byte[]> s = pool.GetOrCreate();

            s.Resource[0] = 255;
            s.Resource[9] = 128;

            Shared <byte[]> s2 = pool.GetOrCreate();

            s2.Resource[0] = 1;
            s2.Resource[9] = 1;

            // serialize twice
            Serializer.Serialize(buffer, s, new SerializationContext());
            Serializer.Serialize(buffer, s2, new SerializationContext());
            Shared <byte[]> target = null;
            var             reader = new BufferReader(buffer.Buffer);

            Serializer.Deserialize(reader, ref target, new SerializationContext());

            Assert.AreEqual(255, target.Resource[0]);
            Assert.AreEqual(0, target.Resource[1]);
            Assert.AreEqual(0, target.Resource[8]);
            Assert.AreEqual(128, target.Resource[9]);

            // deserialize again reusing the first instance, make sure the first instance is not trampled over
            var firstTarget = target.AddRef();

            Serializer.Deserialize(reader, ref target, new SerializationContext());
            Assert.IsFalse(object.ReferenceEquals(firstTarget, target));
            Assert.IsFalse(object.ReferenceEquals(firstTarget.Inner, target.Inner));
            Assert.IsFalse(object.ReferenceEquals(firstTarget.Resource, target.Resource));
            Assert.AreEqual(1, target.Resource[0]);
            Assert.AreEqual(0, target.Resource[1]);
            Assert.AreEqual(0, target.Resource[8]);
            Assert.AreEqual(1, target.Resource[9]);

            // this should not throw, since refcount should be 1 on both
            firstTarget.Dispose();
            target.Dispose();
        }
Esempio n. 12
0
        public void RecyclerPerf()
        {
            var sharedPool = new SharedPool <UnmanagedBuffer>(() => UnmanagedBuffer.Allocate(100), 1);
            var shared     = sharedPool.GetOrCreate();

            shared.Dispose();

            Stopwatch sw         = Stopwatch.StartNew();
            int       iterations = 10;

            for (int i = 0; i < iterations; i++)
            {
                sharedPool.TryGet(out shared);
                shared.Dispose();
            }

            sw.Stop();
            Console.WriteLine($"Get + Release = {sw.ElapsedMilliseconds * 1000000d / iterations} ns");
        }
Esempio n. 13
0
        // [TestMethod, Timeout(60000)]
        public void RefCountedFinalizationTest()
        {
            var sharedPool  = new SharedPool <UnmanagedBuffer>(() => UnmanagedBuffer.Allocate(100), 1);
            var shared      = sharedPool.GetOrCreate();
            var otherShared = shared.DeepClone();

            shared = null;

            // after GC and finalization, the live copy is not affected
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Assert.AreNotEqual(IntPtr.Zero, otherShared.Resource.Data);
            otherShared = null;

            // after GC and finalization of all live copies, the resource goes back to the pool without being finalized itself
            GC.Collect();
            GC.WaitForPendingFinalizers();
            var wasRecycled = sharedPool.TryGet(out shared);

            Assert.IsTrue(wasRecycled);
            Assert.IsNotNull(shared.Resource);
            Assert.AreNotEqual(IntPtr.Zero, shared.Resource.Data);
        }
Esempio n. 14
0
        public void DeserializePooled()
        {
            const int iterations = 10;
            var       writer     = new BufferWriter(100);
            var       pool       = new SharedPool <byte[]>(() => new byte[10], 1);

            using (var s = pool.GetOrCreate())
            {
                for (int i = 0; i < iterations; i++)
                {
                    Serializer.Serialize(writer, s, new SerializationContext());
                }
            }

            // the array should be back in the pool
            Assert.AreEqual(1, pool.AvailableCount);
            Assert.AreEqual(1, pool.TotalCount);

            var reader = new BufferReader(writer.Buffer);

            Assert.IsTrue(pool.TryGet(out Shared <byte[]> s2), "Expected a free entry in the pool!");
            for (int i = 0; i < iterations; i++)
            {
                Serializer.Deserialize(reader, ref s2, new SerializationContext());

                // verify that the pool doesn't grow
                Assert.AreEqual(pool, s2.SharedPool);
                Assert.AreEqual(0, pool.AvailableCount);
                Assert.AreEqual(1, pool.TotalCount);
            }

            s2.Dispose();

            // disposing the last deserialized shared should release the array back to the pool
            Assert.AreEqual(1, pool.AvailableCount);
        }
Esempio n. 15
0
 public SharedPoolTests()
 {
     this.sut = new SharedPool <List <string> >(
         () => new List <string>(),
         l => l.Add("Returned"));
 }
Esempio n. 16
0
 public void Destroy(Entity e)
 {
     dict.Remove(e.UID);
     SharedPool <Entity> .Return(e);
 }
Esempio n. 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Pool{T}"/> class.
 /// </summary>
 /// <param name="allocator">Allocation function.</param>
 public Pool(Func <T> allocator)
 {
     this.sharedPool = new SharedPool <T>(allocator, 2);
 }
Esempio n. 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Pool{T}"/> class.
 /// </summary>
 /// <param name="allocator">Allocation function.</param>
 public Pool(Func <T> allocator)
 {
     this.allocator = allocator;
     this.recycler  = new SharedPool <T>(2);
 }