From: Kefu Chai Date: Tue, 16 Mar 2021 02:42:43 +0000 (+0800) Subject: os/bluestore: let split() return string_view X-Git-Tag: v16.2.5~60^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=7f4c7ec0fbd1787df0180737e01a0919a52445be;p=ceph.git os/bluestore: let split() return string_view instead of creating two strings, just return string_views, for better performance. and for better readability. Signed-off-by: Kefu Chai (cherry picked from commit afc2c61a3ac6904f3c5ca0cede053cb4920baf90) --- diff --git a/src/os/bluestore/BlueRocksEnv.cc b/src/os/bluestore/BlueRocksEnv.cc index b3dd73a74428..8c4f80c7ed10 100644 --- a/src/os/bluestore/BlueRocksEnv.cc +++ b/src/os/bluestore/BlueRocksEnv.cc @@ -30,13 +30,17 @@ rocksdb::Status err_to_status(int r) } } -void split(const std::string &fn, std::string *dir, std::string *file) +std::pair +split(const std::string &fn) { size_t slash = fn.rfind('/'); - *file = fn.substr(slash + 1); - while (slash && fn[slash-1] == '/') + assert(slash != fn.npos); + size_t file_begin = slash + 1; + while (slash && fn[slash - 1] == '/') --slash; - *dir = fn.substr(0, slash); + return {string_view(fn.data(), slash), + string_view(fn.data() + file_begin, + fn.size() - file_begin)}; } } @@ -353,8 +357,7 @@ rocksdb::Status BlueRocksEnv::NewSequentialFile( { if (fname[0] == '/') return target()->NewSequentialFile(fname, result, options); - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); BlueFS::FileReader *h; int r = fs->open_for_read(dir, file, &h, false); if (r < 0) @@ -368,8 +371,7 @@ rocksdb::Status BlueRocksEnv::NewRandomAccessFile( std::unique_ptr* result, const rocksdb::EnvOptions& options) { - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); BlueFS::FileReader *h; int r = fs->open_for_read(dir, file, &h, true); if (r < 0) @@ -383,8 +385,7 @@ rocksdb::Status BlueRocksEnv::NewWritableFile( std::unique_ptr* result, const rocksdb::EnvOptions& options) { - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); BlueFS::FileWriter *h; int r = fs->open_for_write(dir, file, &h, false); if (r < 0) @@ -399,10 +400,8 @@ rocksdb::Status BlueRocksEnv::ReuseWritableFile( std::unique_ptr* result, const rocksdb::EnvOptions& options) { - std::string old_dir, old_file; - split(old_fname, &old_dir, &old_file); - std::string new_dir, new_file; - split(new_fname, &new_dir, &new_file); + auto [old_dir, old_file] = split(old_fname); + auto [new_dir, new_file] = split(new_fname); int r = fs->rename(old_dir, old_file, new_dir, new_file); if (r < 0) @@ -430,8 +429,7 @@ rocksdb::Status BlueRocksEnv::FileExists(const std::string& fname) { if (fname[0] == '/') return target()->FileExists(fname); - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); if (fs->stat(dir, file, NULL, NULL) == 0) return rocksdb::Status::OK(); return err_to_status(-ENOENT); @@ -450,8 +448,7 @@ rocksdb::Status BlueRocksEnv::GetChildren( rocksdb::Status BlueRocksEnv::DeleteFile(const std::string& fname) { - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); int r = fs->unlink(dir, file); if (r < 0) return err_to_status(r); @@ -486,8 +483,7 @@ rocksdb::Status BlueRocksEnv::GetFileSize( const std::string& fname, uint64_t* file_size) { - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); int r = fs->stat(dir, file, file_size, NULL); if (r < 0) return err_to_status(r); @@ -497,8 +493,7 @@ rocksdb::Status BlueRocksEnv::GetFileSize( rocksdb::Status BlueRocksEnv::GetFileModificationTime(const std::string& fname, uint64_t* file_mtime) { - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); utime_t mtime; int r = fs->stat(dir, file, NULL, &mtime); if (r < 0) @@ -511,10 +506,8 @@ rocksdb::Status BlueRocksEnv::RenameFile( const std::string& src, const std::string& target) { - std::string old_dir, old_file; - split(src, &old_dir, &old_file); - std::string new_dir, new_file; - split(target, &new_dir, &new_file); + auto [old_dir, old_file] = split(src); + auto [new_dir, new_file] = split(target); int r = fs->rename(old_dir, old_file, new_dir, new_file); if (r < 0) @@ -537,8 +530,7 @@ rocksdb::Status BlueRocksEnv::AreFilesSame( if (fs->dir_exists(path)) { continue; } - std::string dir, file; - split(path, &dir, &file); + auto [dir, file] = split(path); int r = fs->stat(dir, file, nullptr, nullptr); if (!r) { continue; @@ -556,8 +548,7 @@ rocksdb::Status BlueRocksEnv::LockFile( const std::string& fname, rocksdb::FileLock** lock) { - std::string dir, file; - split(fname, &dir, &file); + auto [dir, file] = split(fname); BlueFS::FileLock *l = NULL; int r = fs->lock_file(dir, file, &l); if (r < 0)