From 058c2ab8ccdae2af0922d6035d8a9c9314b18498 Mon Sep 17 00:00:00 2001 From: congqixia Date: Tue, 3 Dec 2024 19:30:39 +0800 Subject: [PATCH] enhance: [hotfix] Use fdopen, fwrite to reduce direct syscall (#38157) (#38181) Cherry pick from 2.4 pr: #38157 `File.Write` and `File.WriteInt` use `write`, which may be just direct syscall in some systems. When mappding field data and write line by line, this could cost lost of CPU time when the row number is large. --------- Signed-off-by: Congqi Xia --- internal/core/src/common/File.h | 31 ++++++++++++++++++++++++++++--- internal/core/src/mmap/Utils.h | 17 +++++++++-------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/internal/core/src/common/File.h b/internal/core/src/common/File.h index f25f748ac1..4d9b765be3 100644 --- a/internal/core/src/common/File.h +++ b/internal/core/src/common/File.h @@ -11,11 +11,13 @@ #pragma once +#include #include #include "common/EasyAssert.h" #include "common/Types.h" #include "fmt/core.h" #include +#include #include namespace milvus { @@ -27,8 +29,8 @@ class File { file.fd_ = -1; } ~File() { - if (fd_ >= 0) { - close(fd_); + if (fs_ != nullptr) { + fclose(fs_); } } @@ -63,6 +65,22 @@ class File { return write(fd_, &value, sizeof(value)); } + ssize_t + FWrite(const void* buf, size_t size) { + return fwrite(buf, sizeof(char), size, fs_); + } + + template , int> = 0> + ssize_t + FWriteInt(T value) { + return fwrite(&value, 1, sizeof(value), fs_); + } + + int + FFlush() { + return fflush(fs_); + } + offset_t Seek(offset_t offset, int whence) { return lseek(fd_, offset, whence); @@ -70,15 +88,22 @@ class File { void Close() { - close(fd_); + fclose(fs_); + fs_ = nullptr; fd_ = -1; } private: explicit File(int fd, const std::string& filepath) : fd_(fd), filepath_(filepath) { + fs_ = fdopen(fd_, "wb+"); + AssertInfo(fs_ != nullptr, + "failed to open file {}: {}", + filepath, + strerror(errno)); } int fd_{-1}; + FILE* fs_; std::string filepath_; }; } // namespace milvus diff --git a/internal/core/src/mmap/Utils.h b/internal/core/src/mmap/Utils.h index fad5a19aae..9d5a5abe1e 100644 --- a/internal/core/src/mmap/Utils.h +++ b/internal/core/src/mmap/Utils.h @@ -101,12 +101,12 @@ WriteFieldData(File& file, auto str = static_cast(data->RawValue(i)); ssize_t written_data_size = - file.WriteInt(uint32_t(str->size())); + file.FWriteInt(uint32_t(str->size())); if (written_data_size != sizeof(uint32_t)) { THROW_FILE_WRITE_ERROR } total_written += written_data_size; - auto written_data = file.Write(str->data(), str->size()); + auto written_data = file.FWrite(str->data(), str->size()); if (written_data < str->size()) { THROW_FILE_WRITE_ERROR } @@ -120,14 +120,14 @@ WriteFieldData(File& file, indices.push_back(total_written); auto padded_string = static_cast(data->RawValue(i))->data(); - ssize_t written_data_size = - file.WriteInt(uint32_t(padded_string.size())); + ssize_t written_data_size = file.FWriteInt( + uint32_t(padded_string.size())); if (written_data_size != sizeof(uint32_t)) { THROW_FILE_WRITE_ERROR } total_written += written_data_size; ssize_t written_data = - file.Write(padded_string.data(), padded_string.size()); + file.FWrite(padded_string.data(), padded_string.size()); if (written_data < padded_string.size()) { THROW_FILE_WRITE_ERROR } @@ -141,7 +141,7 @@ WriteFieldData(File& file, indices.push_back(total_written); auto array = static_cast(data->RawValue(i)); ssize_t written = - file.Write(array->data(), array->byte_size()); + file.FWrite(array->data(), array->byte_size()); if (written < array->byte_size()) { THROW_FILE_WRITE_ERROR } @@ -157,7 +157,7 @@ WriteFieldData(File& file, static_cast*>( data->RawValue(i)); ssize_t written = - file.Write(vec->data(), vec->data_byte_size()); + file.FWrite(vec->data(), vec->data_byte_size()); if (written < vec->data_byte_size()) { break; } @@ -172,7 +172,7 @@ WriteFieldData(File& file, } } else { // write as: data|data|data|data|data|data...... - size_t written = file.Write(data->Data(), data->Size()); + size_t written = file.FWrite(data->Data(), data->Size()); if (written < data->Size()) { THROW_FILE_WRITE_ERROR } @@ -181,5 +181,6 @@ WriteFieldData(File& file, total_written += data->Size(i); } } + file.FFlush(); } } // namespace milvus