From b78aee363f1540e05cfb5007cb909a21fba13854 Mon Sep 17 00:00:00 2001 From: Peter Freiling Date: Wed, 3 Apr 2019 20:43:28 -0700 Subject: [PATCH] Removing extra buffer copy --- cs/src/core/Allocator/GenericAllocator.cs | 33 +++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/cs/src/core/Allocator/GenericAllocator.cs b/cs/src/core/Allocator/GenericAllocator.cs index 5f9dce576..934d91765 100644 --- a/cs/src/core/Allocator/GenericAllocator.cs +++ b/cs/src/core/Allocator/GenericAllocator.cs @@ -368,11 +368,26 @@ private void WriteAsync(long flushPage, ulong alignedDestinationAddres if (ms.Position > ObjectBlockSize || i == (end / recordSize) - 1) { + var memoryStreamLength = (int)ms.Position; + + var _objBuffer = bufferPool.Get(memoryStreamLength); + + asyncResult.done = new AutoResetEvent(false); + + var _alignedLength = (memoryStreamLength + (sectorSize - 1)) & ~(sectorSize - 1); + + var _objAddr = Interlocked.Add(ref localSegmentOffsets[(long)(alignedDestinationAddress >> LogSegmentSizeBits) % SegmentBufferSize], _alignedLength) - _alignedLength; + fixed (void* src_ = ms.GetBuffer()) + Buffer.MemoryCopy(src_, _objBuffer.aligned_pointer, memoryStreamLength, memoryStreamLength); + + foreach (var address in addr) + ((AddressInfo*)address)->Address += _objAddr; + if (KeyHasObjects()) keySerializer.EndSerialize(); if (ValueHasObjects()) valueSerializer.EndSerialize(); - var _s = ms.ToArray(); + ms.Close(); if (i < (end / recordSize) - 1) @@ -382,23 +397,7 @@ private void WriteAsync(long flushPage, ulong alignedDestinationAddres keySerializer.BeginSerialize(ms); if (ValueHasObjects()) valueSerializer.BeginSerialize(ms); - } - var _objBuffer = bufferPool.Get(_s.Length); - - asyncResult.done = new AutoResetEvent(false); - - var _alignedLength = (_s.Length + (sectorSize - 1)) & ~(sectorSize - 1); - - var _objAddr = Interlocked.Add(ref localSegmentOffsets[(long)(alignedDestinationAddress >> LogSegmentSizeBits) % SegmentBufferSize], _alignedLength) - _alignedLength; - fixed (void* src_ = _s) - Buffer.MemoryCopy(src_, _objBuffer.aligned_pointer, _s.Length, _s.Length); - - foreach (var address in addr) - ((AddressInfo*)address)->Address += _objAddr; - - if (i < (end / recordSize) - 1) - { objlogDevice.WriteAsync( (IntPtr)_objBuffer.aligned_pointer, (int)(alignedDestinationAddress >> LogSegmentSizeBits),