Skip to content

Commit ec1cd79

Browse files
committed
common: improve hobject_t fmtlib formatter performance
The new version of the code only takes about 38% of the time of the old one. See https://github.com/ronen-fr/hobjtostr/tree/rf-2 for the code used to benchmark the new version. hobject_fmt.h is folded into hobject.h, as fmtlib is now an accepted dependency in all of Ceph. Signed-off-by: Ronen Friedman <[email protected]>
1 parent 941d3f2 commit ec1cd79

File tree

7 files changed

+64
-56
lines changed

7 files changed

+64
-56
lines changed

src/common/hobject.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#ifndef __CEPH_OS_HOBJECT_H
1616
#define __CEPH_OS_HOBJECT_H
1717

18+
#include <fmt/compile.h>
19+
#include <fmt/format.h>
20+
1821
#if FMT_VERSION >= 90000
1922
#include <fmt/ostream.h>
2023
#endif
@@ -351,6 +354,54 @@ template<> struct hash<hobject_t> {
351354
};
352355
} // namespace std
353356

357+
namespace fmt {
358+
template <>
359+
struct formatter<hobject_t> {
360+
361+
template <typename FormatContext>
362+
static inline auto
363+
append_sanitized(FormatContext& ctx, const std::string& in, int sep = 0)
364+
{
365+
for (const auto i : in) {
366+
if (i == '%' || i == ':' || i == '/' || i < 32 || i >= 127) {
367+
fmt::format_to(
368+
ctx.out(), FMT_COMPILE("%{:02x}"), static_cast<unsigned char>(i));
369+
} else {
370+
fmt::format_to(ctx.out(), FMT_COMPILE("{:c}"), i);
371+
}
372+
}
373+
if (sep) {
374+
fmt::format_to(
375+
ctx.out(), FMT_COMPILE("{:c}"), sep);
376+
}
377+
return ctx.out();
378+
}
379+
380+
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
381+
382+
template <typename FormatContext>
383+
auto format(const hobject_t& ho, FormatContext& ctx)
384+
{
385+
if (ho == hobject_t{}) {
386+
return fmt::format_to(ctx.out(), "MIN");
387+
}
388+
389+
if (ho.is_max()) {
390+
return fmt::format_to(ctx.out(), "MAX");
391+
}
392+
393+
fmt::format_to(
394+
ctx.out(), FMT_COMPILE("{}:{:08x}:"), static_cast<uint64_t>(ho.pool),
395+
ho.get_bitwise_key_u32());
396+
append_sanitized(ctx, ho.nspace, ':');
397+
append_sanitized(ctx, ho.get_key(), ':');
398+
append_sanitized(ctx, ho.oid.name);
399+
return fmt::format_to(ctx.out(), FMT_COMPILE(":{}"), ho.snap);
400+
}
401+
};
402+
} // namespace fmt
403+
404+
354405
std::ostream& operator<<(std::ostream& out, const hobject_t& o);
355406

356407
template <typename T>

src/common/hobject_fmt.h

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/crimson/osd/backfill_state.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <algorithm>
55
#include <boost/type_index.hpp>
66
#include <fmt/ranges.h>
7-
#include "common/hobject_fmt.h"
7+
#include "common/hobject.h"
88
#include "crimson/osd/backfill_state.h"
99
#include "osd/osd_types_fmt.h"
1010

src/crimson/osd/pg.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <fmt/format.h>
1515
#include <fmt/ostream.h>
1616

17-
#include "common/hobject_fmt.h"
17+
#include "common/hobject.h"
1818

1919
#include "messages/MOSDOp.h"
2020
#include "messages/MOSDOpReply.h"

src/crimson/osd/scrub/scrub_machine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "common/fmt_common.h"
2020
#include "common/hobject.h"
21-
#include "common/hobject_fmt.h"
2221
#include "crimson/common/log.h"
2322
#include "osd/osd_types_fmt.h"
2423
#include "scrub_validator.h"

src/msg/msg_types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ class entity_name_t {
9898
}
9999
void dump(ceph::Formatter *f) const;
100100

101+
template <typename FormatContext>
102+
auto fmt_print_ctx(FormatContext& ctx) const {
103+
if (is_new() || _num < 0) {
104+
return fmt::format_to(ctx.out(), "{}.?", type_str());
105+
} else {
106+
return fmt::format_to(ctx.out(), "{}.{}",type_str(), _num);
107+
}
108+
}
109+
101110
static void generate_test_instances(std::list<entity_name_t*>& o);
102111
};
103112
WRITE_CLASS_DENC(entity_name_t)

src/osd/osd_types_fmt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* \file fmtlib formatters for some types.h classes
66
*/
77

8-
#include "common/hobject_fmt.h"
8+
#include "common/hobject.h"
99
#include "osd/osd_types.h"
1010
#include <fmt/chrono.h>
11+
#include <fmt/ranges.h>
1112
#if FMT_VERSION >= 90000
1213
#include <fmt/ostream.h>
1314
#endif

0 commit comments

Comments
 (0)