From: Kefu Chai Date: Tue, 16 Mar 2021 01:38:41 +0000 (+0800) Subject: os/bluestore: use string_view in BlueFS X-Git-Tag: v16.2.5~60^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=441310929d531ff4e89103bf8974f78678fce741;p=ceph.git os/bluestore: use string_view in BlueFS this improves the performance of lookup. when it comes to mutation operations, std::map<> always require an instance of string, but it only require a single instance of string for each mutation operation in general, so this does not incurs performance regression due to multiple copies of the same string_view object. Signed-off-by: Kefu Chai (cherry picked from commit a90d63bc488b6f9c4f802b656222879130f725e3) --- diff --git a/src/os/bluestore/BlueFS.cc b/src/os/bluestore/BlueFS.cc index 3f19597292c8..6b42ea27705c 100644 --- a/src/os/bluestore/BlueFS.cc +++ b/src/os/bluestore/BlueFS.cc @@ -3180,8 +3180,8 @@ void BlueFS::_maybe_compact_log(std::unique_lock& l) } int BlueFS::open_for_write( - const string& dirname, - const string& filename, + std::string_view dirname, + std::string_view filename, FileWriter **h, bool overwrite) { @@ -3212,7 +3212,7 @@ int BlueFS::open_for_write( file = ceph::make_ref(); file->fnode.ino = ++ino_last; file_map[ino_last] = file; - dir->file_map[filename] = file; + dir->file_map[string{filename}] = file; ++file->refs; create = true; } else { @@ -3297,8 +3297,8 @@ void BlueFS::_close_writer(FileWriter *h) } int BlueFS::open_for_read( - const string& dirname, - const string& filename, + std::string_view dirname, + std::string_view filename, FileReader **h, bool random) { @@ -3328,8 +3328,8 @@ int BlueFS::open_for_read( } int BlueFS::rename( - const string& old_dirname, const string& old_filename, - const string& new_dirname, const string& new_filename) + std::string_view old_dirname, std::string_view old_filename, + std::string_view new_dirname, std::string_view new_filename) { std::lock_guard l(lock); dout(10) << __func__ << " " << old_dirname << "/" << old_filename @@ -3368,15 +3368,15 @@ int BlueFS::rename( dout(10) << __func__ << " " << new_dirname << "/" << new_filename << " " << " " << file->fnode << dendl; - new_dir->file_map[new_filename] = file; - old_dir->file_map.erase(old_filename); + new_dir->file_map[string{new_filename}] = file; + old_dir->file_map.erase(string{old_filename}); log_t.op_dir_link(new_dirname, new_filename, file->fnode.ino); log_t.op_dir_unlink(old_dirname, old_filename); return 0; } -int BlueFS::mkdir(const string& dirname) +int BlueFS::mkdir(std::string_view dirname) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << dendl; @@ -3385,16 +3385,16 @@ int BlueFS::mkdir(const string& dirname) dout(20) << __func__ << " dir " << dirname << " exists" << dendl; return -EEXIST; } - dir_map[dirname] = ceph::make_ref(); + dir_map[string{dirname}] = ceph::make_ref(); log_t.op_dir_create(dirname); return 0; } -int BlueFS::rmdir(const string& dirname) +int BlueFS::rmdir(std::string_view dirname) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << dendl; - map::iterator p = dir_map.find(dirname); + auto p = dir_map.find(dirname); if (p == dir_map.end()) { dout(20) << __func__ << " dir " << dirname << " does not exist" << dendl; return -ENOENT; @@ -3404,12 +3404,12 @@ int BlueFS::rmdir(const string& dirname) dout(20) << __func__ << " dir " << dirname << " not empty" << dendl; return -ENOTEMPTY; } - dir_map.erase(dirname); + dir_map.erase(string{dirname}); log_t.op_dir_remove(dirname); return 0; } -bool BlueFS::dir_exists(const string& dirname) +bool BlueFS::dir_exists(std::string_view dirname) { std::lock_guard l(lock); map::iterator p = dir_map.find(dirname); @@ -3418,7 +3418,7 @@ bool BlueFS::dir_exists(const string& dirname) return exists; } -int BlueFS::stat(const string& dirname, const string& filename, +int BlueFS::stat(std::string_view dirname, std::string_view filename, uint64_t *size, utime_t *mtime) { std::lock_guard l(lock); @@ -3446,7 +3446,7 @@ int BlueFS::stat(const string& dirname, const string& filename, return 0; } -int BlueFS::lock_file(const string& dirname, const string& filename, +int BlueFS::lock_file(std::string_view dirname, std::string_view filename, FileLock **plock) { std::lock_guard l(lock); @@ -3457,7 +3457,7 @@ int BlueFS::lock_file(const string& dirname, const string& filename, return -ENOENT; } DirRef dir = p->second; - map::iterator q = dir->file_map.find(filename); + auto q = dir->file_map.find(filename); FileRef file; if (q == dir->file_map.end()) { dout(20) << __func__ << " dir " << dirname << " (" << dir @@ -3467,7 +3467,7 @@ int BlueFS::lock_file(const string& dirname, const string& filename, file->fnode.ino = ++ino_last; file->fnode.mtime = ceph_clock_now(); file_map[ino_last] = file; - dir->file_map[filename] = file; + dir->file_map[string{filename}] = file; ++file->refs; log_t.op_file_update(file->fnode); log_t.op_dir_link(dirname, filename, file->fnode.ino); @@ -3495,7 +3495,7 @@ int BlueFS::unlock_file(FileLock *fl) return 0; } -int BlueFS::readdir(const string& dirname, vector *ls) +int BlueFS::readdir(std::string_view dirname, vector *ls) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << dendl; @@ -3523,7 +3523,7 @@ int BlueFS::readdir(const string& dirname, vector *ls) return 0; } -int BlueFS::unlink(const string& dirname, const string& filename) +int BlueFS::unlink(std::string_view dirname, std::string_view filename) { std::lock_guard l(lock); dout(10) << __func__ << " " << dirname << "/" << filename << dendl; @@ -3545,7 +3545,7 @@ int BlueFS::unlink(const string& dirname, const string& filename) << " is locked" << dendl; return -EBUSY; } - dir->file_map.erase(filename); + dir->file_map.erase(string{filename}); log_t.op_dir_unlink(dirname, filename); _drop_link(file); return 0; @@ -3773,7 +3773,7 @@ size_t BlueFS::probe_alloc_avail(int dev, uint64_t alloc_size) void* OriginalVolumeSelector::get_hint_for_log() const { return reinterpret_cast(BlueFS::BDEV_WAL); } -void* OriginalVolumeSelector::get_hint_by_dir(const string& dirname) const { +void* OriginalVolumeSelector::get_hint_by_dir(std::string_view dirname) const { uint8_t res = BlueFS::BDEV_DB; if (dirname.length() > 5) { // the "db.slow" and "db.wal" directory names are hard-coded at diff --git a/src/os/bluestore/BlueFS.h b/src/os/bluestore/BlueFS.h index 2f1388baea5d..4b5b187b204b 100644 --- a/src/os/bluestore/BlueFS.h +++ b/src/os/bluestore/BlueFS.h @@ -63,7 +63,7 @@ public: virtual ~BlueFSVolumeSelector() { } virtual void* get_hint_for_log() const = 0; - virtual void* get_hint_by_dir(const std::string& dirname) const = 0; + virtual void* get_hint_by_dir(std::string_view dirname) const = 0; virtual void add_usage(void* file_hint, const bluefs_fnode_t& fnode) = 0; virtual void sub_usage(void* file_hint, const bluefs_fnode_t& fnode) = 0; @@ -506,14 +506,14 @@ public: int get_block_extents(unsigned id, interval_set *extents); int open_for_write( - const std::string& dir, - const std::string& file, + std::string_view dir, + std::string_view file, FileWriter **h, bool overwrite); int open_for_read( - const std::string& dir, - const std::string& file, + std::string_view dir, + std::string_view file, FileReader **h, bool random = false); @@ -522,21 +522,21 @@ public: _close_writer(h); } - int rename(const std::string& old_dir, const std::string& old_file, - const std::string& new_dir, const std::string& new_file); + int rename(std::string_view old_dir, std::string_view old_file, + std::string_view new_dir, std::string_view new_file); - int readdir(const std::string& dirname, std::vector *ls); + int readdir(std::string_view dirname, std::vector *ls); - int unlink(const std::string& dirname, const std::string& filename); - int mkdir(const std::string& dirname); - int rmdir(const std::string& dirname); + int unlink(std::string_view dirname, std::string_view filename); + int mkdir(std::string_view dirname); + int rmdir(std::string_view dirname); bool wal_is_rotational(); - bool dir_exists(const std::string& dirname); - int stat(const std::string& dirname, const std::string& filename, + bool dir_exists(std::string_view dirname); + int stat(std::string_view dirname, std::string_view filename, uint64_t *size, utime_t *mtime); - int lock_file(const std::string& dirname, const std::string& filename, FileLock **p); + int lock_file(std::string_view dirname, std::string_view filename, FileLock **p); int unlock_file(FileLock *l); void compact_log(); @@ -662,7 +662,7 @@ public: : wal_total(_wal_total), db_total(_db_total), slow_total(_slow_total) {} void* get_hint_for_log() const override; - void* get_hint_by_dir(const std::string& dirname) const override; + void* get_hint_by_dir(std::string_view dirname) const override; void add_usage(void* hint, const bluefs_fnode_t& fnode) override { // do nothing diff --git a/src/os/bluestore/BlueStore.cc b/src/os/bluestore/BlueStore.cc index db098f5c727f..e4171a33859f 100644 --- a/src/os/bluestore/BlueStore.cc +++ b/src/os/bluestore/BlueStore.cc @@ -16391,7 +16391,7 @@ void RocksDBBlueFSVolumeSelector::get_paths(const std::string& base, paths& res) res.emplace_back(base + ".slow", l_totals[LEVEL_SLOW - LEVEL_FIRST]); } -void* RocksDBBlueFSVolumeSelector::get_hint_by_dir(const string& dirname) const { +void* RocksDBBlueFSVolumeSelector::get_hint_by_dir(std::string_view dirname) const { uint8_t res = LEVEL_DB; if (dirname.length() > 5) { // the "db.slow" and "db.wal" directory names are hard-coded at diff --git a/src/os/bluestore/BlueStore.h b/src/os/bluestore/BlueStore.h index ce48331b223b..e7c14757888d 100644 --- a/src/os/bluestore/BlueStore.h +++ b/src/os/bluestore/BlueStore.h @@ -3786,7 +3786,7 @@ public: void* get_hint_for_log() const override { return reinterpret_cast(LEVEL_LOG); } - void* get_hint_by_dir(const std::string& dirname) const override; + void* get_hint_by_dir(std::string_view dirname) const override; void add_usage(void* hint, const bluefs_fnode_t& fnode) override { if (hint == nullptr) diff --git a/src/os/bluestore/bluefs_types.h b/src/os/bluestore/bluefs_types.h index eea4845349e8..e1cb0d8e4a95 100644 --- a/src/os/bluestore/bluefs_types.h +++ b/src/os/bluestore/bluefs_types.h @@ -214,24 +214,24 @@ struct bluefs_transaction_t { using ceph::encode; encode((__u8)OP_INIT, op_bl); } - void op_dir_create(const std::string& dir) { + void op_dir_create(std::string_view dir) { using ceph::encode; encode((__u8)OP_DIR_CREATE, op_bl); encode(dir, op_bl); } - void op_dir_remove(const std::string& dir) { + void op_dir_remove(std::string_view dir) { using ceph::encode; encode((__u8)OP_DIR_REMOVE, op_bl); encode(dir, op_bl); } - void op_dir_link(const std::string& dir, const std::string& file, uint64_t ino) { + void op_dir_link(std::string_view dir, std::string_view file, uint64_t ino) { using ceph::encode; encode((__u8)OP_DIR_LINK, op_bl); encode(dir, op_bl); encode(file, op_bl); encode(ino, op_bl); } - void op_dir_unlink(const std::string& dir, const std::string& file) { + void op_dir_unlink(std::string_view dir, std::string_view file) { using ceph::encode; encode((__u8)OP_DIR_UNLINK, op_bl); encode(dir, op_bl);