Пример #1
0
        static void Main(string[] args)
        {
            uint[] input = new uint[10000];
            for (int i = 0; i < input.Length; i++)
            {
                input[i] = (uint)i;
            }
            byte[] output = new byte[100000];

            uint[] result = new uint[100000];

            long      rs = 0;
            uint      n  = ExternMethods.vbyte_encode(input, (size_t)input.Length, output);
            Stopwatch w  = Stopwatch.StartNew();

            for (int i = 0; i < 10000; i++)
            {
                rs += ExternMethods.masked_vbyte_decode(output, result, n);
            }

            Console.WriteLine($"count:{n},{rs},cost:{w.ElapsedMilliseconds}");

            //int result = ExternMethods.Add(10, 20);
            //Console.WriteLine("10 + 20 = {0}", result);
            Console.ReadLine();
        }
Пример #2
0
        private Span <byte> DecompressNative(ReadOnlySpan <byte> src, int decompressBound)
        {
            fixed(byte *srcPtr = src)
            {
                var buffer = new byte[decompressBound];

                fixed(byte *decompressedBufferNativePtr = buffer)
                {
                    var dctx = ExternMethods.ZSTD_createDCtx();

                    try
                    {
                        var length = ExternMethods.ZSTD_decompressDCtx(dctx,
                                                                       (IntPtr)decompressedBufferNativePtr,
                                                                       (nuint)buffer.Length,
                                                                       (IntPtr)srcPtr, (nuint)src.Length);
                        return(new Span <byte>(buffer, 0, (int)length));
                    }
                    finally
                    {
                        ExternMethods.ZSTD_freeDCtx(dctx);
                    }
                }
            }
        }
Пример #3
0
 public void DecompressNative()
 {
     fixed(byte *dstPtr = dest)
     fixed(byte *uncompressedPtr = uncompressed)
     {
         ExternMethods.ZSTD_decompressDCtx(dCtxNative, (IntPtr)uncompressedPtr, (nuint)uncompressed.Length,
                                           (IntPtr)dstPtr, compressedLength);
     }
 }
Пример #4
0
 public void CompressNative()
 {
     fixed(byte *dstPtr = dest)
     fixed(byte *srcPtr = src)
     {
         ExternMethods.ZSTD_compressCCtx(cCtxNative, (IntPtr)dstPtr, (nuint)dest.Length, (IntPtr)srcPtr,
                                         (nuint)src.Length, level);
     }
 }
Пример #5
0
        public void Setup()
        {
            cCtx = Methods.ZSTD_createCCtx();
            dCtx = Methods.ZSTD_createDCtx();

            cCtxNative = ExternMethods.ZSTD_createCCtx();
            dCtxNative = ExternMethods.ZSTD_createDCtx();

            src          = File.ReadAllBytes("dickens");
            dest         = new byte[Methods.ZSTD_compressBound((nuint)src.Length)];
            uncompressed = new byte[src.Length];

            fixed(byte *dstPtr = dest)
            fixed(byte *srcPtr = src)
            {
                compressedLength = ExternMethods.ZSTD_compressCCtx(cCtxNative, (IntPtr)dstPtr, (nuint)dest.Length,
                                                                   (IntPtr)srcPtr, (nuint)src.Length,
                                                                   level);
            }
        }
Пример #6
0
        private Span <byte> CompressNative(byte[] srcBuffer, int compressBound, int level)
        {
            var buffer = new byte[compressBound];

            fixed(byte *bufferPtr = buffer)
            fixed(byte *srcPtr = srcBuffer)
            {
                var cctx = ExternMethods.ZSTD_createCCtx();

                try
                {
                    var length = ExternMethods.ZSTD_compressCCtx(cctx,
                                                                 (IntPtr)bufferPtr, (nuint)buffer.Length,
                                                                 (IntPtr)srcPtr, (nuint)srcBuffer.Length,
                                                                 level);
                    return(new Span <byte>(buffer, 0, (int)length));
                }
                finally
                {
                    ExternMethods.ZSTD_freeCCtx(cctx);
                }
            }
        }
Пример #7
0
        static unsafe void Test2()
        {
            var cctx = ExternMethods.ZSTD_createCCtx();
            var dctx = ExternMethods.ZSTD_createDCtx();

            var src          = File.ReadAllBytes("dickens");
            var dest         = new byte[ExternMethods.ZSTD_compressBound((nuint)src.Length)];
            var uncompressed = new byte[src.Length];

            fixed(byte *dstPtr = dest)
            fixed(byte *srcPtr          = src)
            fixed(byte *uncompressedPtr = uncompressed)
            {
                var compressedLength = ExternMethods.ZSTD_compressCCtx(cctx, (IntPtr)dstPtr, (nuint)dest.Length, (IntPtr)srcPtr, (nuint)src.Length,
                                                                       level);

                var decompressedLength = ExternMethods.ZSTD_decompressDCtx(dctx, (IntPtr)uncompressedPtr, (nuint)uncompressed.Length, (IntPtr)dstPtr, compressedLength);

                Console.WriteLine($"{compressedLength} {decompressedLength} {src.Length}");
            }
            ExternMethods.ZSTD_freeCCtx(cctx);
            ExternMethods.ZSTD_freeDCtx(dctx);
        }
Пример #8
0
        public void CompressAndDecompressWithNative(int level)
        {
            var srcBuffer = File.ReadAllBytes("dickens");

            var compressBound = Compressor.GetCompressBound(srcBuffer.Length);

            Assert.Equal((int)ExternMethods.ZSTD_compressBound((nuint)srcBuffer.Length), compressBound);

            var compressor       = new Compressor(level);
            var compressedSharp  = compressor.Wrap(srcBuffer);
            var compressedNative = CompressNative(srcBuffer, compressBound, level);

            Assert.True(compressedNative.SequenceEqual(compressedSharp));

            var decompressBound = (int)Decompressor.GetDecompressedSize(compressedSharp);

            Assert.Equal(decompressBound, srcBuffer.Length);

            var decompressor       = new Decompressor();
            var decompressedSharp  = decompressor.Unwrap(compressedSharp);
            var decompressedNative = DecompressNative(compressedNative, decompressBound);

            Assert.True(decompressedSharp.SequenceEqual(decompressedNative));
        }