Esempio n. 1
0
        public void CannotDoubleDispose()
        {
            var memory = ArrayMemory <byte> .Create(32 * 1024, pin : true);

            ((IDisposable)memory).Dispose();
            Assert.Throws <ObjectDisposedException>(() => { ((IDisposable)memory).Dispose(); });
        }
Esempio n. 2
0
        public void WorksWithNonBlittables()
        {
            var arr = new string[] { "a" };
            var r   = ArrayMemory <string> .Create(arr, 0, arr.Length, externallyOwned : true, pin : false);

            var h0 = r.Pin();

            Assert.IsFalse(r.IsPinned);

            var h1 = r.Pin();

            h1.Dispose();

            Assert.AreEqual(1, r.ReferenceCount);

            var vec = r.Vec;

            Assert.AreEqual(1, vec.Length);
            Assert.AreEqual("a", vec[0]);
            var rm = r.Retain();

            Assert.AreEqual(2, r.ReferenceCount);

            Assert.AreEqual("a", rm.Span[0]);
            rm.Dispose();
            Assert.AreEqual(1, r.ReferenceCount);

            h0.Dispose();
        }
Esempio n. 3
0
        public void CouldCreateVsAndReadElements()
        {
            var count = 1000;
            var arr   = Enumerable.Range(0, count).ToArray();
            var r     = ArrayMemory <int> .Create(arr);

            var vs = VecStorage.Create(r, 0, r.Length);

            Assert.AreEqual(arr.Length, vs.Vec.Length);
            long sum = 0L;

            for (int i = 0; i < arr.Length; i++)
            {
                var vi = vs.Vec.DangerousGetUnaligned <int>(i);
                if (vi != i)
                {
                    Assert.Fail("vi != i");
                }
                sum += vs.Vec.DangerousGetUnaligned <int>(i);
            }

            Console.WriteLine(sum);

            vs.Dispose();
        }
Esempio n. 4
0
        public void RentReturnBenchmarkRetainablePoolOverCapacity()
        {
            var count    = 1_000_000;
            var capacity = 25;
            var batch    = capacity * 2;
            var pool     = new RetainableMemoryPool <byte>((p, l) =>
            {
                var am = ArrayMemory <byte> .Create(l, pin: true);
                // Attach pool
                am._pool = p;
                return(am);
            }, 16,
                                                           1024 * 1024, capacity, 0);

            var list = new List <RetainableMemory <byte> >(batch);

            using (Benchmark.Run("FullCycle", count * batch))
            {
                for (int i = 0; i < count; i++)
                {
                    for (int j = 0; j < batch; j++)
                    {
                        list.Add(pool.RentMemory(32 * 1024));
                    }

                    foreach (var arrayMemory in list)
                    {
                        pool.Return(arrayMemory);
                    }
                    list.Clear();
                }
            }

            pool.Dispose();
        }
Esempio n. 5
0
        public void SliceDisposeBenchmark()
        {
            // 6.3 MOPS
            var count   = 1_000_000;
            var rounds  = 10;
            var arrSize = 1000;
            var arr     = Enumerable.Range(0, arrSize).ToArray();
            var mem     = ArrayMemory <int> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true);

            var vs = VectorStorage.Create(mem, 0, mem.Length);

            Assert.AreEqual(arr.Length, vs.Length);
            for (int r = 0; r < rounds; r++)
            {
                using (Benchmark.Run("Slice/Dispose", count))
                {
                    for (int i = 0; i < count; i++)
                    {
                        var vs1 = vs.Slice(0, vs.Vec.Length, 1, externallyOwned: true);
                        vs1.Dispose();
                    }
                }
            }

            Benchmark.Dump();

            vs.Dispose();

            Assert.IsTrue(vs.IsDisposed);
        }
Esempio n. 6
0
        public void RentReturnPinUnpinBenchmark()
        {
#if !DEBUG
            var count = 10_000_000;
#else
            var count = 1_000;
#endif

            using (Benchmark.Run("FullCyclePinUnpin", count))
            {
                for (int i = 0; i < count; i++)
                {
                    var memory = ArrayMemory <byte> .Create(32 * 1024);

                    if (memory.Array.Length != 32 * 1024)
                    {
                        Assert.Fail("Length");
                    }
                    (memory.Pin(0)).Dispose();
                    if (memory.IsRetained)
                    {
                        Assert.Fail();
                    }
                }
            }
        }
Esempio n. 7
0
        public void CouldCreateVsAndReadElements()
        {
            var count = 1000;
            var arr   = Enumerable.Range(0, count).ToArray();
            var r     = ArrayMemory <int> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true);

            var vs = VectorStorage.Create(r, 0, r.Length);

            Assert.AreEqual(arr.Length, vs.Length);
            long sum = 0L;

            for (int i = 0; i < arr.Length; i++)
            {
                var vi = vs.DangerousGet <int>(i);
                if (vi != i)
                {
                    Assert.Fail("vi != i");
                }
                sum += vs.DangerousGet <int>(i);
            }

            Console.WriteLine(sum);

            vs.Dispose();

            Assert.IsTrue(vs.IsDisposed);
        }
Esempio n. 8
0
        public void CannotDisposeRetained()
        {
            var memory = ArrayMemory <byte> .Create(32 * 1024, pin : true);

            var rm = memory.Retain();

            Assert.Throws <InvalidOperationException>(() => { ((IDisposable)memory).Dispose(); });
            rm.Dispose();
        }
Esempio n. 9
0
        public void CannotDoubleDispose()
        {
            var memory = ArrayMemory <byte> .Create(32 * 1024);

            Assert.IsFalse(memory.IsPoolable);
            Assert.IsFalse(memory.IsPooled);
            Assert.IsFalse(memory.IsDisposed);
            memory.Dispose();
            Assert.IsTrue(memory.IsDisposed);
            Assert.Throws <ObjectDisposedException>(() => { memory.Dispose(); });
        }
Esempio n. 10
0
        internal Series(TKey[] keys, TValue[] values)
        {
            if (keys == null)
            {
                throw new ArgumentNullException(nameof(keys));
            }
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }
            if (keys.Length != values.Length)
            {
                throw new ArgumentException("Different keys and values length");
            }
            var ks = KeySorting.Strong;

            if (keys.Length > 1)
            {
                var cmp = KeyComparer <TKey> .Default;
                for (int i = 1; i < keys.Length; i++)
                {
                    var c = cmp.Compare(keys[i], keys[i - 1]);
                    if (c == 0)
                    {
                        ks = KeySorting.Weak;
                    }
                    else if (c < 0)
                    {
                        ks = KeySorting.NotSorted;
                        break;
                    }
                }
            }
            Flags = new Flags((byte)((byte)Mutability.ReadOnly | (byte)ks));

            if (keys.Length == 0 && values.Length == 0)
            {
                Debug.Assert(Data == DataBlock.Empty);
                return;
            }

            var keyMemory = ArrayMemory <TKey> .Create(keys);

            var keyVs = VecStorage.Create(keyMemory, 0, keyMemory.Length);

            var valMemory = ArrayMemory <TValue> .Create(values);

            var valVs = VecStorage.Create(valMemory, 0, valMemory.Length);

            var block = DataBlock.SeriesCreate(rowIndex: keyVs, values: valVs, rowLength: keys.Length);

            Data = block;
        }
Esempio n. 11
0
        public void CannotDisposeRetained()
        {
            var memory = ArrayMemory <byte> .Create(32 * 1024);

            Assert.IsFalse(memory.IsPoolable);
            Assert.IsFalse(memory.IsPooled);
            Assert.IsFalse(memory.IsDisposed);
            var rm = memory.Retain();

            Assert.Throws <InvalidOperationException>(() => { memory.Dispose(); });
            rm.Dispose();
            Assert.IsTrue(memory.IsDisposed);
        }
Esempio n. 12
0
        private static BaseContainer <int> CreateIntBaseContainer(int capacity, int length)
        {
            var bc = new BaseContainer <int>();

            var rm = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray(), externallyOwned : true);

            var vs = VectorStorage.Create(rm, 0, rm.Length, 1);

            var block = DataBlock.Create(rowIndex: vs, rowLength: length);

            bc.DataBlock = block;
            return(bc);
        }
Esempio n. 13
0
        private static BaseContainer <int> CreateIntBaseContainer(int capacity, int length)
        {
            var bc = new BaseContainer <int>();

            var rm = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray());

            var vs = RetainedVec.Create(rm, 0, rm.Length);

            var block = DataBlock.Create(rowIndex: vs, rowLength: length);

            bc.Data = block;
            return(bc);
        }
Esempio n. 14
0
        public void RentReturnBenchmark()
        {
            var count = 100_000_000;

            using (Benchmark.Run("FullCycle", count))
            {
                for (int i = 0; i < count; i++)
                {
                    var memory = ArrayMemory <byte> .Create(32 * 1024, pin : true);

                    ((IDisposable)memory).Dispose();
                }
            }
        }
Esempio n. 15
0
        public void RentReturnBenchmark()
        {
#if !DEBUG
            var count = 100_000_000;
#else
            var count = 1_000;
#endif

            using (Benchmark.Run("FullCycle", count))
            {
                for (int i = 0; i < count; i++)
                {
                    var memory = ArrayMemory <byte> .Create(32, pin : false);

                    ((IDisposable)memory).Dispose();
                }
            }
        }
Esempio n. 16
0
        public void CreateDisposeBenchmark()
        {
#if !DEBUG
            var count = 100_000_000;
#else
            var count = 1_000;
#endif

            using (Benchmark.Run("FullCycle", count))
            {
                for (int i = 0; i < count; i++)
                {
                    var memory = ArrayMemory <byte> .Create(32);

                    (memory).Dispose();
                }
            }
        }
Esempio n. 17
0
        public void OffsetsAreOk()
        {
            var array = new byte[100];

            using var rm = new RetainedMemory <byte>(ArrayMemory <byte> .Create(array, 50, 50, true), 25, 25, true);

            // 85 - 95
            using var rmc = rm.Clone(10, 10);

            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.WaitForPendingFinalizers();
            GC.Collect(2, GCCollectionMode.Forced, true);
            GC.WaitForPendingFinalizers();

            rmc.GetSpan()[5] = 1;

            Assert.AreEqual(1, array[90]);
        }
Esempio n. 18
0
        public void VectorStorageReadBench()
        {
            var count  = 1_000_000;
            var rounds = 10;
            var mult   = 500;
            var arr    = Enumerable.Range(0, count).ToArray();

            var mem = ArrayMemory <int> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true);

            var stride = 2;

            var vs = VectorStorage.Create(mem, (stride - 1), mem.Length - (stride - 1), stride);

            Assert.AreEqual(arr.Length / stride, vs.Length);

            int sum = 0;

            for (int r = 0; r < rounds; r++)
            {
                using (Benchmark.Run("VS Read", vs.Length * mult))
                {
                    var vector = vs.GetVector <int>();
                    for (int _ = 0; _ < mult; _++)
                    {
                        for (int i = 0; i < vs.Length; i++)
                        {
                            var vi = vector[i];
                            if (vi != i * stride + (stride - 1))
                            {
                                Assert.Fail("vi != i * 2");
                            }

                            unchecked
                            {
                                sum += vi;
                            }
                        }
                    }
                }
            }

            Benchmark.Dump();
            Console.WriteLine(sum);
        }
Esempio n. 19
0
        public void CouldTryFindBlockAtSingleChunkBench()
        {
            var count  = 50_000_000;
            var rounds = 20;

            // for this test capacity is irrelevant - interpolation search hits exact position on first try
            var capacity = count / 100;
            var bc       = new BaseContainer <int>();
            var rm       = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray());

            var vs = VecStorage.Create(rm, 0, rm.Length);

            var block = DataBlock.Create(rowIndex: vs, rowLength: vs.Vec.Length);

            bc.Data = block;

            for (int r = 0; r < rounds; r++)
            {
                using (Benchmark.Run("TryFindChunkAt", count))
                {
                    var m = count / capacity;
                    for (int _ = 0; _ < m; _++)
                    {
                        for (int i = 1; i < capacity; i++)
                        {
                            var searchIndexRef = i;
                            var found          = bc.TryFindBlockAt(ref searchIndexRef, Lookup.LE, out var c, out var ci);
                            if (!found ||
                                !ReferenceEquals(block, c) ||
                                i != ci ||
                                i != searchIndexRef
                                )
                            {
                                Assert.Fail();
                            }
                        }
                    }
                }
            }

            Benchmark.Dump();
            bc.Dispose();
        }
Esempio n. 20
0
        public void VectorStorageReadBench()
        {
            var count  = 1_000_000;
            var rounds = 10;
            var mult   = 500;
            var arr    = Enumerable.Range(0, count).ToArray();

            var mem = ArrayMemory <int> .Create(arr);

            var vs = VecStorage.Create(mem, 0, mem.Length);

            Assert.AreEqual(arr.Length, vs.Vec.Length);

            int sum = 0;

            for (int r = 0; r < rounds; r++)
            {
                using (Benchmark.Run("VS Read", vs.Vec.Length * mult))
                {
                    for (int _ = 0; _ < mult; _++)
                    {
                        for (int i = 0; i < vs.Vec.Length; i++)
                        {
                            var vi = vs.Vec.DangerousGetUnaligned <int>(i);
                            //if (vi != i)
                            //{
                            //    Assert.Fail("vi != i");
                            //}

                            unchecked
                            {
                                sum += vi;
                            }
                        }
                    }
                }
            }

            Benchmark.Dump();
            Console.WriteLine(sum);
        }
Esempio n. 21
0
        public void WrappedLookup()
        {
            var count = 10_000;
            var arr   = Enumerable.Range(0, count).Select(x => (long)x).ToArray();

            var r = ArrayMemory <long> .Create(arr,
                                               0, arr.Length, externallyOwned : true);

            var keys   = RetainedVec.Create(r, 0, r.Length);
            var values = keys.Slice(0, count, true);

            var block = DataBlock.SeriesCreate(keys, values, count);

            for (int i = 0; i < count; i++)
            {
                var ii = (long)i;
                Assert.AreEqual(i, block.LookupKey(ref ii, Lookup.EQ));
            }

            block.Dispose();
        }
Esempio n. 22
0
        public Series(TKey[] keys, TValue[] values)
        {
            if (keys.Length != values.Length)
            {
                throw new ArgumentException("Different keys and values length");
            }
            var ks = KeySorting.Strong;

            if (keys.Length > 1)
            {
                var cmp = KeyComparer <TKey> .Default;
                for (int i = 1; i < keys.Length; i++)
                {
                    var c = cmp.Compare(keys[i], keys[i - 1]);
                    if (c == 0)
                    {
                        ks = KeySorting.Weak;
                    }
                    else if (c < 0)
                    {
                        ks = KeySorting.NotSorted;
                        break;
                    }
                }
            }
            _flags = new Flags((byte)((byte)Mutability.Immutable | (byte)ks));


            var keyMemory = ArrayMemory <TKey> .Create(keys, externallyOwned : true);

            var keyVs = VectorStorage.Create(keyMemory, 0, keyMemory.Length, 1);

            var valMemory = ArrayMemory <TValue> .Create(values, externallyOwned : true);

            var valVs = VectorStorage.Create(valMemory, 0, valMemory.Length, 1);

            var block = DataBlock.Create(rowIndex: keyVs, values: valVs, rowLength: keys.Length);

            DataBlock = block;
        }
Esempio n. 23
0
        public void CouldTryGetBlockAtSingleChunk()
        {
            var capacity = 100;
            var bc       = new BaseContainer <int>();

            var rm = ArrayMemory <int> .Create(Enumerable.Range(0, capacity).ToArray());

            var vs = VecStorage.Create(rm, 0, rm.Length);

            var block = DataBlock.Create(rowIndex: vs, rowLength: vs.Vec.Length / 2);

            bc.Data = block;

            var searchIndex = 40;
            var found       = bc.TryGetBlockAt(searchIndex, out var dataBlock, out var ci);

            Assert.IsTrue(found);
            Assert.AreSame(block, dataBlock);
            Assert.AreEqual(searchIndex, ci);

            bc.Dispose();
        }
Esempio n. 24
0
        public void CouldTryGetBlockSingleChunk()
        {
            var capacity = 100;
            var bc       = new BaseContainer <long>();
            var rm       = ArrayMemory <long> .Create(Enumerable.Range(0, capacity).Select(x => (long)x).ToArray());

            var vs = VecStorage.Create(rm, 0, rm.Length);

            var block = DataBlock.Create(rowIndex: vs, rowLength: vs.Vec.Length / 2);

            bc.Data = block;

            var searchIndex    = 40L;
            var searchIndexRef = searchIndex;
            var found          = bc.TryGetBlock(searchIndexRef, out var c, out var ci);

            Assert.IsTrue(found);
            Assert.AreSame(block, c);
            Assert.AreEqual(searchIndex, ci);
            Assert.AreEqual(searchIndex, searchIndexRef);
            bc.Dispose();
        }
Esempio n. 25
0
        public void CouldTryFindBlockAtSingleChunk()
        {
            var capacity = 100;
            var bc       = new BaseContainer <long>();

            var rm = ArrayMemory <long> .Create(Enumerable.Range(0, capacity).Select(x => (long)x).ToArray(), externallyOwned : true);

            var vs = VectorStorage.Create(rm, 0, rm.Length, 1);

            var block = DataBlock.Create(rowIndex: vs, rowLength: vs.Length / 2);

            bc.DataBlock = block;

            var searchIndex    = 40L;
            var searchIndexRef = searchIndex;
            var found          = bc.TryFindBlockAt(ref searchIndexRef, Lookup.LT, out var c, out var ci);

            Assert.IsTrue(found);
            Assert.AreSame(block, c);
            Assert.AreEqual(searchIndex - 1, ci);
            Assert.AreEqual(searchIndex - 1, searchIndexRef);

            bc.Dispose();
        }
Esempio n. 26
0
        public void Equality()
        {
            VecStorage vs1 = default;
            VecStorage vs2 = default;

            Assert.AreEqual(vs1, vs2);
            Assert.AreEqual(vs1.Vec.Length, 0);

            var count = 1000;
            var arr   = Enumerable.Range(0, count).ToArray();
            var r     = ArrayMemory <int> .Create(arr);

            var vs = VecStorage.Create(r, 0, r.Length);

            Assert.AreNotEqual(vs1, vs);

            var vsCopy  = vs.Slice(0, vs.Vec.Length, true);
            var vsSlice = vs.Slice(0, vs.Vec.Length - 1, true);

            Assert.AreEqual(vs, vsCopy);
            Assert.AreNotEqual(vs, vsSlice);

            vs.Dispose();
        }
Esempio n. 27
0
        public void CouldSerializeVectorStorage()
        {
            var rng   = new Random(42);
            var count = 100_000;
            var arr   = new SmallDecimal[count];

            arr[0] = new SmallDecimal(1000 * 1.0, 4);

            for (int i = 1; i < count; i++)
            {
                arr[i] = arr[i - 1] + new SmallDecimal((double)arr[i - 1] * (0.02 + -0.04 * rng.NextDouble()), 4);
            }
            // arr = Enumerable.Range(0, count).Select(x => new SmallDecimal(1000 + (double)x + (double)Math.Round(0.1 * rng.NextDouble(), 5), precision:3)).ToArray();

            var r = ArrayMemory <SmallDecimal> .Create(arr, 0, arr.Length, externallyOwned : true, pin : true);

            var vs = VectorStorage.Create(r, 0, r.Length);

            var vsT = new VectorStorage <SmallDecimal>(vs);

            var payload = count * Unsafe.SizeOf <double>() + 4;

            foreach (SerializationFormat format in ((SerializationFormat[])Enum.GetValues(typeof(SerializationFormat))).OrderBy(e => e.ToString()))
            {
                var len = BinarySerializer.SizeOf(in vsT, out var rm, format);

                var destination   = BufferPool.Retain(len);
                var destinationDb = new DirectBuffer(destination);

                var len1 = BinarySerializer.Write(in vsT, ref destinationDb, in rm, format);
                Assert.AreEqual(destination.Length, destinationDb.Length);

                Assert.AreEqual(len, len1);

                var flags = destinationDb.Read <VersionAndFlags>(0);
                Assert.AreEqual(format, flags.SerializationFormat);
                var header = destinationDb.Read <DataTypeHeader>(0);
                Assert.AreEqual(TypeEnum.VectorStorage, header.TypeEnum);
                Assert.AreEqual(TypeEnum.SmallDecimal, header.ElementTypeEnum);
                Assert.AreEqual(Unsafe.SizeOf <SmallDecimal>(), header.TypeSize);

                var len2 = BinarySerializer.Read(ref destinationDb, out VectorStorage <SmallDecimal> value);
                Assert.AreEqual(destination.Length, destinationDb.Length);

                Assert.AreEqual(len, len2);
                Assert.AreEqual(vs.Length, value.Storage.Length);


                for (int i = 0; i < count; i++)
                {
                    SmallDecimal left;
                    SmallDecimal right;
                    if ((left = vs.Vec.DangerousGetRef <SmallDecimal>(i)) != (right = value.Storage.DangerousGetRef <SmallDecimal>(i)))
                    {
                        Console.WriteLine("Not equals");
                    }
                }

                Assert.IsTrue(vs.Vec.Slice(0, vs.Length).AsSpan <SmallDecimal>().SequenceEqual(value.Storage.Vec.Slice(0, value.Storage.Length).AsSpan <SmallDecimal>()));

                Console.WriteLine($"{format} len: {len:N0} x{Math.Round((double)payload/len, 2)}");

                destination.Dispose();
                value.Storage.Dispose();
            }

            vs.Dispose();
            Assert.IsTrue(vs.IsDisposed);
        }