Skip to content

Commit b53beb3

Browse files
committed
Zero-fill buffer only when necessary
1 parent 77cdd64 commit b53beb3

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

worker.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ DWORD ChunkDiskWorker::PostWork(SPD_STORAGE_UNIT_OPERATION_CONTEXT* context, Chu
166166

167167
if (op_kind == READ_CHUNK || op_kind == WRITE_CHUNK)
168168
{
169-
// prepare zero-filled buffer
169+
// prepare buffer
170170
// write back to ctx_buffer if read request processed immediately
171171
// write request is never processed immediately
172172
err = GetBuffer(work.buffer);
@@ -316,7 +316,6 @@ DWORD ChunkDiskWorker::GetBuffer(Pages& buffer)
316316
{
317317
buffer = std::move(buffers_.front());
318318
buffers_.pop_front();
319-
// buf was zero-filled in ReturnBuffer()
320319
return ERROR_SUCCESS;
321320
}
322321
}
@@ -325,9 +324,6 @@ DWORD ChunkDiskWorker::ReturnBuffer(Pages buffer)
325324
{
326325
if (!buffer) return ERROR_INVALID_PARAMETER;
327326

328-
auto buffer_size = service_.MaxTransferLength() + u32(service_.params.PageBytes(1));
329-
memset(buffer.get(), 0, buffer_size);
330-
331327
try
332328
{
333329
// buffers_ used by the dispatcher and the worker thread
@@ -565,11 +561,15 @@ DWORD ChunkDiskWorker::PrepareChunkOps(ChunkWork& work, ChunkOpKind kind, u64 ch
565561
{
566562
try
567563
{
568-
// buffer was zero-filled, nothing to do if READ_CHUNK
569564
// nothing to zero-fill if UNMAP_CHUNK
570565
auto it = ops.emplace(ops.end(), &work, kind, chunk_idx, start_off, end_off,
571566
LONGLONG(params.BlockBytes(start_off)), buffer);
572-
if (buffer != nullptr) buffer = recast<u8*>(buffer) + params.BlockBytes(end_off - start_off);
567+
if (buffer != nullptr)
568+
{
569+
// zero-fill if READ_CHUNK
570+
memset(buffer, 0, params.BlockBytes(end_off - start_off));
571+
buffer = recast<u8*>(buffer) + params.BlockBytes(end_off - start_off);
572+
}
573573
ReportOpResult(*it);
574574
return ERROR_SUCCESS;
575575
}
@@ -1149,9 +1149,9 @@ DWORD ChunkDiskWorker::PostReadChunk(ChunkOpState& state)
11491149
err = OpenChunk(state.idx, false, h);
11501150
if (err != ERROR_SUCCESS) return err;
11511151

1152+
auto length_bytes = params.BlockBytes(state.end_off - state.start_off);
11521153
if (h != INVALID_HANDLE_VALUE)
11531154
{
1154-
auto length_bytes = params.BlockBytes(state.end_off - state.start_off);
11551155
auto bytes_read = DWORD();
11561156
err = ReadFile(h, state.buffer, DWORD(length_bytes), &bytes_read, &state.ovl)
11571157
? ERROR_SUCCESS : GetLastError();
@@ -1164,7 +1164,7 @@ DWORD ChunkDiskWorker::PostReadChunk(ChunkOpState& state)
11641164
{
11651165
// handle synchronous EOF when unmap then read
11661166
// simulate ReadFile()
1167-
// buffer already zero-filled
1167+
memset(state.buffer, 0, length_bytes);
11681168
err = PostQueuedCompletionStatus(iocp_.get(), length_bytes, CK_IO, &state.ovl)
11691169
? ERROR_SUCCESS : GetLastError();
11701170
if (err != ERROR_SUCCESS)
@@ -1183,6 +1183,7 @@ DWORD ChunkDiskWorker::PostReadChunk(ChunkOpState& state)
11831183
else
11841184
{
11851185
// simulate ReadFile()
1186+
memset(state.buffer, 0, length_bytes);
11861187
// set bytes_transferred to -1 to indicate
11871188
err = PostQueuedCompletionStatus(iocp_.get(), u32(-1), CK_IO, &state.ovl)
11881189
? ERROR_SUCCESS : GetLastError();
@@ -1240,7 +1241,11 @@ void ChunkDiskWorker::CompleteChunkOp(ChunkOpState& state, DWORD error, DWORD by
12401241
}
12411242
if (error == ERROR_HANDLE_EOF && state.kind == READ_CHUNK && bytes_transferred == 0)
12421243
{
1243-
if (CheckAsyncEOF(state) == ERROR_SUCCESS) error = ERROR_SUCCESS;
1244+
if (CheckAsyncEOF(state) == ERROR_SUCCESS)
1245+
{
1246+
memset(state.buffer, 0, length_bytes);
1247+
error = ERROR_SUCCESS;
1248+
}
12441249
}
12451250
if (!(state.kind == READ_CHUNK && bytes_transferred == u32(-1))) CloseChunk(state.idx);
12461251
ReportOpResult(state, error);

worker.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ class ChunkDiskWorker
181181
DWORD PostWork(SPD_STORAGE_UNIT_OPERATION_CONTEXT* context, ChunkOpKind op_kind, u64 block_addr, u32 count);
182182

183183
private:
184-
// get zero-filled, page aligned buffer from the pool
184+
// get page aligned buffer from the pool
185185
DWORD GetBuffer(Pages& buffer);
186186

187-
// zero-fill buffer and return it to the pool
187+
// return buffer to the pool
188188
DWORD ReturnBuffer(Pages buffer);
189189

190190
// get shared chunk file handle from the pool

0 commit comments

Comments
 (0)