From 7488c6dbf831b6aac12e82316ea0007027baf22f Mon Sep 17 00:00:00 2001 From: zilinzhu Date: Fri, 15 Jan 2021 15:12:59 +0800 Subject: [PATCH] optimize RunLineHelper in BufferedInputStream --- tensorflow/core/lib/io/buffered_inputstream.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tensorflow/core/lib/io/buffered_inputstream.cc b/tensorflow/core/lib/io/buffered_inputstream.cc index d9715e8d463..d9b2e4a0745 100644 --- a/tensorflow/core/lib/io/buffered_inputstream.cc +++ b/tensorflow/core/lib/io/buffered_inputstream.cc @@ -60,25 +60,32 @@ Status BufferedInputStream::ReadLineHelper(StringType* result, bool include_eol) { result->clear(); Status s; + size_t start_pos = pos_; while (true) { if (pos_ == limit_) { + result->append(buf_.data() + start_pos, pos_ - start_pos); // Get more data into buffer s = FillBuffer(); if (limit_ == 0) { break; } + start_pos = pos_; } - char c = buf_[pos_++]; + char c = buf_[pos_]; if (c == '\n') { + result->append(buf_.data() + start_pos, pos_ - start_pos); if (include_eol) { result->append(1, c); } + pos_++; return Status::OK(); } // We don't append '\r' to *result - if (c != '\r') { - result->append(1, c); + if (c == '\r') { + result->append(buf_.data() + start_pos, pos_ - start_pos); + start_pos = pos_ + 1; } + pos_++; } if (errors::IsOutOfRange(s) && !result->empty()) { return Status::OK();