Beispiel #1
0
        public void MemoryAccessVecViaDbVecStorageUnsafe(RetainableMemory <int> rm)
        {
            var  rounds = TestUtils.GetBenchCount(100_000, 100);
            long sum    = 0;

            using (Benchmark.Run("DbVecStorageUnsafe (+)", rm.Length * rounds))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm, Vec = rm.GetVec().AsVec(), RetainedVec = RetainedVec.Create(rm, 0, rm.Length, true)
                };

                for (int r = 0; r < rounds; r++)
                {
                    for (long i = 0; i < rm.Length; i++)
                    {
                        sum += db.RetainedVec.Vec.UnsafeGetUnaligned <int>((IntPtr)i);
                    }
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #2
0
        public void MemoryFieldBench()
        {
            var count = (int)4 * 1024;  // TestUtils.GetBenchCount(1_000_000);
            var rm    = PrivateMemory <int> .Create(count);

            var           arr = new int[count];
            DataBlockLike db  = new DataBlockLike {
                Rm = rm, Vec = rm.GetVec().AsVec(), RetainedVec = RetainedVec.Create(rm, 0, rm.Length, true), Length = rm.Length
            };

            for (int i = 0; i < count; i++)
            {
                db.RetainedVec.UnsafeWriteUnaligned((IntPtr)i, i);
                arr[i] = i;
            }

            for (int r = 0; r < 10; r++)
            {
                MemoryFieldBench_Field(rm);
                MemoryFieldBench_CreateMem(rm);
            }

            Benchmark.Dump();
            rm.Dispose();
        }
Beispiel #3
0
        public void MemoryAccessVecViaDbVecStorageDangerous(RetainableMemory <int> rm)
        {
            long sum = 0;

            using (Benchmark.Run("DbVecStorageDangerous (_)", rm.Length))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm, Vec = rm.Vec, VecStorage = VecStorage.Create(rm, 0, rm.Length, true)
                };
                for (int i = 0; i < rm.Length; i++)
                {
                    sum += db.VecStorage.Vec.DangerousGetUnaligned <int>(i);
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #4
0
        public unsafe void MemoryAccessVecViaDbVecPointer(RetainableMemory <int> rm)
        {
            long sum = 0;

            using (Benchmark.Run("DbVecPointer (*)", rm.Length))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm, Vec = rm.Vec, Ptr = (int *)rm.Pointer
                };
                for (int i = 0; i < rm.Length; i++)
                {
                    sum += Unsafe.ReadUnaligned <int>(ref Unsafe.As <int, byte>(ref Unsafe.Add <int>(ref Unsafe.AsRef <int>((void *)db.Vec._byteOffset), i)));
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #5
0
        public void MemoryAccessVecViaDbVecUnsafe(RetainableMemory <int> rm)
        {
            long sum = 0;

            using (Benchmark.Run("DbVecUnsafe (+)", rm.Length))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm, Vec = rm.Vec
                };
                for (long i = 0; i < rm.Length; i++)
                {
                    sum += db.Vec.UnsafeGetUnaligned <int>((IntPtr)i);
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #6
0
        public void MemoryAccessVecViaDbHelperUnsafeGetRef(RetainableMemory <int> rm)
        {
            long          sum = 0;
            DataBlockLike db  = new DataBlockLike {
                Rm = rm, Vec = rm.Vec
            };

            using (Benchmark.Run("DbHelperUnsafeGet (^)", rm.Length))
            {
                for (long i = 0; i < rm.Length; i++)
                {
                    sum += Unsafe.As <PrivateMemory <byte> >(db.Rm).Vec.UnsafeGetUnaligned <int>((IntPtr)i);
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #7
0
        public void MemoryAccessVecViaDbHelperDangerous(RetainableMemory <int> rm)
        {
            long sum = 0;

            using (Benchmark.Run("DbHelperDangerous (^)", rm.Length))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm
                };
                for (int i = 0; i < rm.Length; i++)
                {
                    sum += RetainableMemoryHelper.GetVecRef(db.Rm).DangerousGetUnaligned <int>(i);
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #8
0
        public unsafe void SetUp()
        {
            var rng = new Random();

            _count   = Count;
            _idx     = rng.Next(_count / 4, _count / 2);
            _idxLong = _idx;
            _rm      = PrivateMemory <int> .Create(_count);

            _arr = new int[_count];
            _db  = new DataBlockLike
            {
                Rm = _rm, Vec = _rm.GetVec().AsVec(), RetainedVec = RetainedVec.Create(_rm, 0, _rm.Length, true), Length = _rm.Length, arr = _arr, Ptr = (int *)_rm.Pointer
            };
            _vec = _rm.GetVec().AsVec();

            for (int i = 0; i < _count; i++)
            {
                _db.RetainedVec.UnsafeWriteUnaligned((IntPtr)i, i + 1);
                _arr[i] = i + 1;
            }
        }
Beispiel #9
0
        public void MemoryAccessBench()
        {
            var count = (int)4 * 1024;  // TestUtils.GetBenchCount(1_000_000);
            var rm    = PrivateMemory <int> .Create(count);

            var           arr = new int[count];
            DataBlockLike db  = new DataBlockLike {
                Rm = rm, Vec = rm.GetVec().AsVec(), RetainedVec = RetainedVec.Create(rm, 0, rm.Length, true), Length = rm.Length
            };

            for (int i = 0; i < count; i++)
            {
                db.RetainedVec.UnsafeWriteUnaligned((IntPtr)i, i);
                arr[i] = i;
            }

            for (int r = 0; r < 10; r++)
            {
                Sum(rm);
                MemoryAccessViaPointer(rm);
                MemoryAccessViaArray(rm, arr);
                // MemoryAccessVecViaMemSpan(rm);
                // MemoryAccessVecViaLocalDangerous(rm);
                // MemoryAccessVecViaLocalUnsafe(rm);
                MemoryAccessVecViaDbVecUnsafe(rm);
                MemoryAccessVecViaDbVecStorageRead(rm);
                // MemoryAccessVecViaDbVecUnsafeX(rm);
                MemoryAccessVecViaDbVecPointer(rm);
                // MemoryAccessVecViaDbVecDangerous(rm);
                MemoryAccessVecViaDbVecStorageUnsafe(rm);
                MemoryAccessVecViaDbVecStorageDangerous(rm);

                Thread.Sleep(100);
            }

            Benchmark.Dump();
            rm.Dispose();
        }
Beispiel #10
0
        public unsafe void MemoryAccessVecViaDbVecPointer(RetainableMemory <int> rm)
        {
            var  rounds = TestUtils.GetBenchCount(100_000, 100);
            long sum    = 0;

            using (Benchmark.Run("DbVecPointer (*)", rm.Length * rounds))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm, Vec = rm.GetVec().AsVec(), Ptr = (int *)rm.Pointer
                };
                for (int r = 0; r < rounds; r++)
                {
                    for (long i = 0; i < rm.Length; i++)
                    {
                        sum += Unsafe.ReadUnaligned <int>(ref Unsafe.As <int, byte>(ref Unsafe.Add <int>(ref Unsafe.AsRef <int>((void *)db.Vec._byteOffset), (IntPtr)i)));
                    }
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #11
0
        public void MemoryAccessVecViaDbVecDangerous(RetainableMemory <int> rm)
        {
            long sum    = 0;
            var  rounds = TestUtils.GetBenchCount(10_000, 100);

            using (Benchmark.Run("DbVecDangerous (+)", rm.Length * rounds))
            {
                DataBlockLike db = new DataBlockLike {
                    Rm = rm, Vec = rm.GetVec().AsVec()
                };
                for (int r = 0; r < rounds; r++)
                {
                    for (int i = 0; i < rm.Length; i++)
                    {
                        sum += db.Vec.DangerousGetUnaligned <int>(i);
                    }
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #12
0
        public unsafe void MemoryAccessViaArray(RetainableMemory <int> rm, int[] arr)
        {
            var           rounds = TestUtils.GetBenchCount(100_000, 100);
            DataBlockLike db     = new DataBlockLike {
                Rm = rm, arr = arr
            };
            long sum = 0;

            using (Benchmark.Run("Array", rm.Length * rounds))
            {
                for (int r = 0; r < rounds; r++)
                {
                    for (int i = 0; i < db.arr.Length; i++)
                    {
                        sum += db.arr[i];
                    }
                }
            }

            if (sum < 1000)
            {
                throw new InvalidOperationException();
            }
        }