From f448422a04e56ed83fa52f2241243b49af69d6b8 Mon Sep 17 00:00:00 2001 From: chengtbf <472491134@qq.com> Date: Sat, 7 Apr 2018 17:59:39 +0800 Subject: [PATCH] in stream read memcpy (#773) Former-commit-id: 652052bbfd9fec4cd4dc8012a71d18cf8812a8e7 --- .../persistent_in_stream_without_local_copy.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/oneflow/core/persistence/persistent_in_stream_without_local_copy.cpp b/oneflow/core/persistence/persistent_in_stream_without_local_copy.cpp index 962f9ac48a..5f4846cb82 100644 --- a/oneflow/core/persistence/persistent_in_stream_without_local_copy.cpp +++ b/oneflow/core/persistence/persistent_in_stream_without_local_copy.cpp @@ -1,5 +1,6 @@ #include "oneflow/core/persistence/persistent_in_stream_without_local_copy.h" #include "oneflow/core/job/job_desc.h" +#include namespace oneflow { @@ -23,10 +24,15 @@ int32_t PersistentInStreamWithoutLocalCopy::ReadLine(std::string* l) { int32_t PersistentInStreamWithoutLocalCopy::Read(char* s, size_t n) { if (IsEof()) { return -1; } - while (n--) { + while (n) { if (cur_buf_begin_ == cur_buf_end_) { UpdateBuffer(); } - CHECK_NE(cur_buf_begin_, cur_buf_end_); - *s++ = *cur_buf_begin_++; + CHECK_LT(cur_buf_begin_, cur_buf_end_); + int64_t copy_size = + std::min(cur_buf_end_ - cur_buf_begin_, static_cast(n)); + std::memcpy(s, cur_buf_begin_, static_cast(copy_size)); + s += copy_size; + cur_buf_begin_ += copy_size; + n -= copy_size; } return 0; } -- GitLab