From 3ccd449abe75adcc884d39ecdc648543cd06325d Mon Sep 17 00:00:00 2001 From: Candy Date: Tue, 21 Jul 2020 21:27:18 +0800 Subject: [PATCH] =?UTF-8?q?byte=E6=95=B0=E7=BB=84=E9=87=87=E7=94=A8?= =?UTF-8?q?=E6=B1=A0=E5=8C=96=E6=8A=80=E6=9C=AF=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/MarshalFixedMemoryBlock.cs | 10 +++++-- .../DBRuntime/His/Compress/CompressMemory.cs | 3 +- RunTime/DBRuntime/His/Serise/SeriseEnginer.cs | 28 +++++++++++++------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs b/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs index a89a454..d3b5030 100644 --- a/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs +++ b/Common/Cdy.Tag.Common/Common/MarshalFixedMemoryBlock.cs @@ -7,6 +7,7 @@ // 种道洋 //============================================================== using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.IO.MemoryMappedFiles; @@ -1505,12 +1506,13 @@ namespace Cdy.Tag { using (var stream = System.IO.File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { - byte[] bvals = new byte[1024]; + byte[] bvals = ArrayPool.Shared.Rent(1024); for (long i = 0; i < memory.Length / 1024; i++) { Marshal.Copy(new IntPtr(memory.Handles.ToInt64() + i * 1024), bvals, 0, 1024); stream.Write(bvals, 0, 1024); } + ArrayPool.Shared.Return(bvals); stream.Flush(); } } @@ -1523,7 +1525,7 @@ namespace Cdy.Tag public static void RecordToLog(this MarshalFixedMemoryBlock memory, Stream stream) { int ls = 1024 * 1024 * 128; - byte[] bvals = new byte[ls]; + //byte[] bvals = new byte[ls]; //long totalsize = memory.AllocSize; //long csize = 0; @@ -1531,6 +1533,8 @@ namespace Cdy.Tag int ltmp = (int)memory.AllocSize; var source = memory.Handles; + var bvals = ArrayPool.Shared.Rent(ls); + while (ltmp > 0) { int ctmp = Math.Min(bvals.Length, ltmp); @@ -1539,7 +1543,7 @@ namespace Cdy.Tag stmp += ctmp; ltmp -= ctmp; } - + ArrayPool.Shared.Return(bvals); //for (long i = 0; i < memory.Length / ls; i++) //{ // Marshal.Copy(new IntPtr(memory.Handles.ToInt64() + i * ls), bvals, 0, ls); diff --git a/RunTime/DBRuntime/His/Compress/CompressMemory.cs b/RunTime/DBRuntime/His/Compress/CompressMemory.cs index 38abcf0..e4452af 100644 --- a/RunTime/DBRuntime/His/Compress/CompressMemory.cs +++ b/RunTime/DBRuntime/His/Compress/CompressMemory.cs @@ -201,6 +201,7 @@ namespace Cdy.Tag foreach (var vv in mTagAddress) { var size = CompressBlockMemory(source, vv.Value.Item1, Offset, vv.Value.Item3, vv.Value.Item4, vv.Key); + if(dtmp.ContainsKey(vv.Key)) dtmp[vv.Key] = Offset; Offset += size; datasize += size; @@ -224,7 +225,7 @@ namespace Cdy.Tag ServiceLocator.Locator.Resolve().RequestToSeriseFile(this, mCurrentTime); sw.Stop(); - LoggerService.Service.Info("CompressEnginer", Id + "压缩完成 耗时:" + sw.ElapsedMilliseconds + " ltmp1:" + ltmp1 + " ltmp2:" + (ltmp2 - ltmp1) + " ltmp3:" + (ltmp3 - ltmp2), ConsoleColor.Blue); + LoggerService.Service.Info("CompressEnginer", Id + "压缩完成 耗时:" + sw.ElapsedMilliseconds + " ltmp1:" + ltmp1 + " ltmp2:" + (ltmp2 - ltmp1) + " ltmp3:" + (ltmp3 - ltmp2) +" CPU Id:"+ThreadHelper.GetCurrentProcessorNumber(), ConsoleColor.Blue); } catch(Exception ex) diff --git a/RunTime/DBRuntime/His/Serise/SeriseEnginer.cs b/RunTime/DBRuntime/His/Serise/SeriseEnginer.cs index 31e4486..6201570 100644 --- a/RunTime/DBRuntime/His/Serise/SeriseEnginer.cs +++ b/RunTime/DBRuntime/His/Serise/SeriseEnginer.cs @@ -13,6 +13,7 @@ using System.Threading; using System.Linq; using System.Threading.Tasks; using System.Diagnostics; +using System.Buffers; /* * ****文件结构**** @@ -460,10 +461,12 @@ namespace Cdy.Tag { DateTime date = new DateTime(time.Year, time.Month, time.Day, ((time.Hour / FileDuration) * FileDuration), 0, 0); mFileWriter.Write(date, 0); - byte[] nameBytes = new byte[64]; + //byte[] nameBytes = new byte[64]; + byte[] nameBytes = ArrayPool.Shared.Rent(64); var ntmp = Encoding.UTF8.GetBytes(databaseName); Buffer.BlockCopy(ntmp, 0, nameBytes, 0, Math.Min(64, ntmp.Length)); mFileWriter.Write(nameBytes, 8); + ArrayPool.Shared.Return(nameBytes); } /// @@ -473,28 +476,29 @@ namespace Cdy.Tag { byte[] bval; int totalLen; - + int datalen; //更新上个DataRegion 的Next DataRegion Pointer 指针 if (mPreDataRegion >= 0) { mFileWriter.Write(mCurrentDataRegion, mPreDataRegion + 8); } - bval = GeneratorDataRegionHeader(out totalLen); - mFileWriter.Append(bval, 0, bval.Length); - mFileWriter.AppendZore(totalLen - bval.Length); + bval = GeneratorDataRegionHeader(out totalLen,out datalen); + mFileWriter.Append(bval, 0, datalen); + mFileWriter.AppendZore(totalLen - datalen); mPreDataRegion = mCurrentDataRegion; mBlockPointOffset = mCurrentDataRegion + mBlockPointOffset; + ArrayPool.Shared.Return(bval); } /// /// 生成区域头部 /// 偏移位置 /// - private byte[] GeneratorDataRegionHeader(out int totallenght) + private byte[] GeneratorDataRegionHeader(out int totallenght,out int datalen) { Stopwatch sw = new Stopwatch(); sw.Start(); @@ -504,7 +508,9 @@ namespace Cdy.Tag totallenght = len; - using (System.IO.MemoryStream mHeadMemory = new System.IO.MemoryStream()) + byte[] bvals = ArrayPool.Shared.Rent(52 + mTagIdMemoryCach.Position); + + using (System.IO.MemoryStream mHeadMemory = new System.IO.MemoryStream(bvals)) { mHeadMemory.Position = 0; @@ -524,12 +530,13 @@ namespace Cdy.Tag mHeadMemory.Write(mTagIdMemoryCach.Buffer, 0, mTagIdMemoryCach.Position);//写入压缩数据 mBlockPointOffset = mHeadMemory.Position; - + datalen = (int)mHeadMemory.Position; sw.Stop(); LoggerService.Service.Info("SeriseFileItem" + Id, "GeneratorDataRegionHeader " + sw.ElapsedMilliseconds); - return mHeadMemory.ToArray(); + return bvals; + //return mHeadMemory.ToArray(); } } @@ -639,6 +646,9 @@ namespace Cdy.Tag //int blockcount = FileDuration * 60 / BlockDuration; var vv = ServiceLocator.Locator.Resolve(); var tags = vv.ListAllTags().Where(e => e.Id >= Id * TagCountOneFile && e.Id < (Id + 1) * TagCountOneFile).OrderBy(e => e.Id); + + if (mBlockPointMemory != null) mBlockPointMemory.Dispose(); + mBlockPointMemory = new MemoryBlock(tags.Count() * 8,1024*1024); mBlockPointMemory.Clear(); -- GitLab