@@ -41,7 +41,9 @@ limitations under the License.
4141#include " tensorflow/core/lib/strings/str_util.h"
4242#include " tensorflow/core/lib/strings/stringprintf.h"
4343#include " tensorflow/core/platform/bfloat16.h"
44+ #include " tensorflow/core/platform/cord.h"
4445#include " tensorflow/core/platform/errors.h"
46+ #include " tensorflow/core/platform/mem.h"
4547#include " tensorflow/core/util/env_var.h"
4648#include " tensorflow/core/util/saved_tensor_slice_util.h"
4749#include " tensorflow/core/util/tensor_bundle/byte_swap.h"
@@ -406,11 +408,7 @@ Status PadAlignment(FileOutputBuffer* out, int alignment, int64* size) {
406408} // namespace
407409
408410BundleWriter::BundleWriter (Env* env, StringPiece prefix, const Options& options)
409- : env_(env),
410- options_ (options),
411- prefix_(prefix),
412- out_(nullptr ),
413- size_(0 ) {
411+ : env_(env), options_(options), prefix_(prefix), out_(nullptr ), size_(0 ) {
414412 status_ = env_->HasAtomicMove (prefix_, &use_temp_file_);
415413 if (!status_.ok ()) return ;
416414
@@ -1138,31 +1136,48 @@ string BundleReader::DebugString() {
11381136 return shape_str;
11391137}
11401138
1141- FileOutputBuffer::~FileOutputBuffer () { delete file_; }
1139+ namespace {
1140+ inline char * AlignedMalloc (size_t size) {
1141+ char * buffer = static_cast <char *>(port::AlignedMalloc (size, 64 ));
1142+ DCHECK (buffer);
1143+ return buffer;
1144+ }
1145+ } // namespace
1146+
1147+ FileOutputBuffer::FileOutputBuffer (WritableFile* file, size_t buffer_size)
1148+ : file_(file), position_(0 ), buffer_size_(buffer_size) {
1149+ DCHECK_GT (buffer_size, 0 );
1150+ buffer_ptr_ = AlignedMalloc (buffer_size);
1151+ }
1152+
1153+ FileOutputBuffer::~FileOutputBuffer () {
1154+ if (buffer_ptr_) port::AlignedFree (buffer_ptr_);
1155+ delete file_;
1156+ }
11421157
11431158Status FileOutputBuffer::Append (StringPiece data) {
11441159 // In the below, it is critical to calculate the checksum on the actually
11451160 // copied bytes, not the source bytes. This is because "data" typically
11461161 // points to tensor buffers, which may be concurrently written.
11471162 if (data.size () + position_ <= buffer_size_) {
11481163 // Can fit into the current buffer.
1149- memcpy (&buffer_[ position_] , data.data (), data.size ());
1150- crc32c_ = crc32c::Extend (crc32c_, &buffer_[ position_] , data.size ());
1164+ memcpy (buffer_ptr_ + position_, data.data (), data.size ());
1165+ crc32c_ = crc32c::Extend (crc32c_, buffer_ptr_ + position_, data.size ());
11511166 } else if (data.size () <= buffer_size_) {
11521167 // Cannot fit, but can fit after flushing.
1153- TF_RETURN_IF_ERROR (FlushBuffer ());
1154- memcpy (&buffer_[ 0 ] , data.data (), data.size ());
1155- crc32c_ = crc32c::Extend (crc32c_, &buffer_[ 0 ] , data.size ());
1168+ TF_RETURN_IF_ERROR (FlushBuffer (false ));
1169+ memcpy (buffer_ptr_ , data.data (), data.size ());
1170+ crc32c_ = crc32c::Extend (crc32c_, buffer_ptr_ , data.size ());
11561171 } else {
11571172 // Cannot fit even after flushing. So we break down "data" by chunk, and
11581173 // flush/checksum each chunk.
1159- TF_RETURN_IF_ERROR (FlushBuffer ());
1174+ TF_RETURN_IF_ERROR (FlushBuffer (false ));
11601175 for (size_t i = 0 ; i < data.size (); i += buffer_size_) {
11611176 const size_t nbytes = std::min (data.size () - i, buffer_size_);
1162- memcpy (&buffer_[ 0 ] , data.data () + i, nbytes);
1163- crc32c_ = crc32c::Extend (crc32c_, &buffer_[ 0 ] , nbytes);
1177+ memcpy (buffer_ptr_ , data.data () + i, nbytes);
1178+ crc32c_ = crc32c::Extend (crc32c_, buffer_ptr_ , nbytes);
11641179 position_ = nbytes;
1165- TF_RETURN_IF_ERROR (FlushBuffer ());
1180+ TF_RETURN_IF_ERROR (FlushBuffer (false ));
11661181 }
11671182 return Status::OK ();
11681183 }
@@ -1171,13 +1186,18 @@ Status FileOutputBuffer::Append(StringPiece data) {
11711186}
11721187
11731188Status FileOutputBuffer::Close () {
1174- TF_RETURN_IF_ERROR (FlushBuffer ());
1189+ TF_RETURN_IF_ERROR (FlushBuffer (true ));
11751190 return file_->Close ();
11761191}
11771192
1178- Status FileOutputBuffer::FlushBuffer () {
1193+ Status FileOutputBuffer::FlushBuffer (bool closing ) {
11791194 if (position_ > 0 ) {
1180- TF_RETURN_IF_ERROR (file_->Append (StringPiece (&buffer_[0 ], position_)));
1195+ // Use Cord to avoid extra data copy for some WritableFile implementations.
1196+ absl::Cord buffer = absl::MakeCordFromExternal (
1197+ StringPiece (buffer_ptr_, position_),
1198+ [ptr = buffer_ptr_](StringPiece) { port::AlignedFree (ptr); });
1199+ buffer_ptr_ = closing ? nullptr : AlignedMalloc (buffer_size_);
1200+ TF_RETURN_IF_ERROR (file_->Append (buffer));
11811201 position_ = 0 ;
11821202 }
11831203 return Status::OK ();
0 commit comments