From 1168be8b0433cb00ed6d8cde25d5fa53e07e9024 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 10 Dec 2022 10:31:21 +0800 Subject: [PATCH] rgw: drop rgw_obj_key::to_str() rgw_obj_key::to_str() is mainly used by operator<<(ostream&, ..), so we can just implement it with the specialization of fmt::formatter. and let operator<<(ostream&, ..) to call into fmt::format(..): 1. for better readability and 2. for probably better performance -- we don't need to do deep copy for constructing a `std::string` from a `char[]`. 3. for better standard compliance -- we don't need to use variable-length array in C++ code. it is a part of C99 standard. but not a C++ standard. Signed-off-by: Kefu Chai --- src/rgw/driver/rados/rgw_data_sync.cc | 2 +- src/rgw/rgw_obj_types.h | 33 +++++++++++++++------------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/rgw/driver/rados/rgw_data_sync.cc b/src/rgw/driver/rados/rgw_data_sync.cc index 47573b765da8..0655df91f4ac 100644 --- a/src/rgw/driver/rados/rgw_data_sync.cc +++ b/src/rgw/driver/rados/rgw_data_sync.cc @@ -3472,7 +3472,7 @@ public: status.shards_done_with_gen.resize(marker_mgr.get().size()); status.incremental_gen = info.latest_gen; - ldout(cct, 20) << "writing bucket sync status during init. state=" << status.state << ". marker=" << status.full.position.to_str() << dendl; + ldout(cct, 20) << "writing bucket sync status during init. state=" << status.state << ". marker=" << status.full.position << dendl; // write bucket sync status using CR = RGWSimpleRadosWriteCR; diff --git a/src/rgw/rgw_obj_types.h b/src/rgw/rgw_obj_types.h index c90d450793c7..1347a8ad00d4 100644 --- a/src/rgw/rgw_obj_types.h +++ b/src/rgw/rgw_obj_types.h @@ -381,28 +381,33 @@ struct rgw_obj_key { } void dump(Formatter *f) const; void decode_json(JSONObj *obj); - - std::string to_str() const { - if (instance.empty()) { - return name; - } - char buf[name.size() + instance.size() + 16]; - snprintf(buf, sizeof(buf), "%s[%s]", name.c_str(), instance.c_str()); - return buf; - } }; WRITE_CLASS_ENCODER(rgw_obj_key) -inline std::ostream& operator<<(std::ostream& out, const rgw_obj_key &o) { - return out << o.to_str(); -} - +#if FMT_VERSION >= 90000 template<> struct fmt::formatter : fmt::formatter { template auto format(const rgw_obj_key& key, FormatContext& ctx) const { - return formatter::format(key.to_str(), ctx); + if (key.instance.empty()) { + return formatter::format(key.name, ctx); + } else { + return fmt::format_to(ctx.out(), "{}[{}]", key.name, key.instance); + } } }; +#endif + +inline std::ostream& operator<<(std::ostream& out, const rgw_obj_key &key) { +#if FMT_VERSION >= 90000 + return out << fmt::format("{}", key); +#else + if (key.instance.empty()) { + return out << fmt::format("{}", key.name); + } else { + return out << fmt::format("{}[{}]", key.name, key.instance); + } +#endif +} struct rgw_raw_obj { rgw_pool pool; -- 2.47.3