From c85149ed24a2f95d51a35d8dc21a8839964d56d1 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 29 Oct 2024 09:11:33 +0100 Subject: [PATCH] include/types: un-inline methods to reduce compile times Signed-off-by: Max Kellermann --- src/CMakeLists.txt | 1 + src/crimson/CMakeLists.txt | 1 + src/include/types.cc | 129 +++++++++++++++++++++++++++++++++++++ src/include/types.h | 115 ++++----------------------------- 4 files changed, 142 insertions(+), 104 deletions(-) create mode 100644 src/include/types.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 88964a1f685e2..1e21cb21f3609 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -502,6 +502,7 @@ set(libcommon_files xxHash/xxhash.c include/invoke_date.cc include/object.cc + include/types.cc include/utime.cc include/uuid.cc common/error_code.cc diff --git a/src/crimson/CMakeLists.txt b/src/crimson/CMakeLists.txt index e53846843ecc0..8fb417fe35910 100644 --- a/src/crimson/CMakeLists.txt +++ b/src/crimson/CMakeLists.txt @@ -99,6 +99,7 @@ add_library(crimson-common STATIC ${PROJECT_SOURCE_DIR}/src/global/global_context.cc ${PROJECT_SOURCE_DIR}/src/global/pidfile.cc ${PROJECT_SOURCE_DIR}/src/include/object.cc + ${PROJECT_SOURCE_DIR}/src/include/types.cc ${PROJECT_SOURCE_DIR}/src/include/utime.cc ${PROJECT_SOURCE_DIR}/src/include/uuid.cc ${PROJECT_SOURCE_DIR}/src/librbd/Features.cc diff --git a/src/include/types.cc b/src/include/types.cc new file mode 100644 index 0000000000000..bbafa04259ada --- /dev/null +++ b/src/include/types.cc @@ -0,0 +1,129 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "types.h" + +#include + +namespace { +inline std::ostream& format_u(std::ostream& out, const uint64_t v, const uint64_t n, + const int index, const uint64_t mult, const char* u) + { + char buffer[32]; + + if (index == 0) { + (void) snprintf(buffer, sizeof(buffer), "%" PRId64 "%s", n, u); + } else if ((v % mult) == 0) { + // If this is an even multiple of the base, always display + // without any decimal fraction. + (void) snprintf(buffer, sizeof(buffer), "%" PRId64 "%s", n, u); + } else { + // We want to choose a precision that reflects the best choice + // for fitting in 5 characters. This can get rather tricky when + // we have numbers that are very close to an order of magnitude. + // For example, when displaying 10239 (which is really 9.999K), + // we want only a single place of precision for 10.0K. We could + // develop some complex heuristics for this, but it's much + // easier just to try each combination in turn. + int i; + for (i = 2; i >= 0; i--) { + if (snprintf(buffer, sizeof(buffer), "%.*f%s", i, + static_cast(v) / mult, u) <= 7) + break; + } + } + + return out << buffer; + } +} + +void client_t::dump(ceph::Formatter *f) const { + f->dump_int("id", v); +} + +void client_t::generate_test_instances(std::list& ls) { + ls.push_back(new client_t); + ls.push_back(new client_t(1)); + ls.push_back(new client_t(123)); +} + +std::ostream& operator<<(std::ostream& out, const client_t& c) { + return out << c.v; +} + +std::ostream& operator<<(std::ostream& out, const si_u_t& b) +{ + uint64_t n = b.v; + int index = 0; + uint64_t mult = 1; + const char* u[] = {"", "k", "M", "G", "T", "P", "E"}; + + while (n >= 1000 && index < 7) { + n /= 1000; + index++; + mult *= 1000; + } + + return format_u(out, b.v, n, index, mult, u[index]); +} + +std::ostream& operator<<(std::ostream& out, const byte_u_t& b) +{ + uint64_t n = b.v; + int index = 0; + const char* u[] = {" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB"}; + + while (n >= 1024 && index < 7) { + n /= 1024; + index++; + } + + return format_u(out, b.v, n, index, 1ULL << (10 * index), u[index]); +} + +std::ostream& operator<<(std::ostream& out, const ceph_mon_subscribe_item& i) +{ + return out << (long)i.start + << ((i.flags & CEPH_SUBSCRIBE_ONETIME) ? "" : "+"); +} + +std::ostream& operator<<(std::ostream& out, const weightf_t& w) +{ + if (w.v < -0.01F) { + return out << "-"; + } else if (w.v < 0.000001F) { + return out << "0"; + } else { + std::streamsize p = out.precision(); + return out << std::fixed << std::setprecision(5) << w.v << std::setprecision(p); + } +} + +void shard_id_t::dump(ceph::Formatter *f) const { + f->dump_int("id", id); +} + +void shard_id_t::generate_test_instances(std::list& ls) { + ls.push_back(new shard_id_t(1)); + ls.push_back(new shard_id_t(2)); +} + +void errorcode32_t::dump(ceph::Formatter *f) const { + f->dump_int("code", code); +} + +void errorcode32_t::generate_test_instances(std::list& ls) { + ls.push_back(new errorcode32_t(1)); + ls.push_back(new errorcode32_t(2)); +} diff --git a/src/include/types.h b/src/include/types.h index 90d068606d4de..f3404495ed537 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -411,14 +411,8 @@ struct client_t { using ceph::decode; decode(v, bl); } - void dump(ceph::Formatter *f) const { - f->dump_int("id", v); - } - static void generate_test_instances(std::list& ls) { - ls.push_back(new client_t); - ls.push_back(new client_t(1)); - ls.push_back(new client_t(123)); - } + void dump(ceph::Formatter *f) const; + static void generate_test_instances(std::list& ls); }; WRITE_CLASS_ENCODER(client_t) @@ -432,46 +426,10 @@ static inline bool operator>=(const client_t& l, const client_t& r) { return l.v static inline bool operator>=(const client_t& l, int64_t o) { return l.v >= o; } static inline bool operator<(const client_t& l, int64_t o) { return l.v < o; } -inline std::ostream& operator<<(std::ostream& out, const client_t& c) { - return out << c.v; -} - - +std::ostream& operator<<(std::ostream& out, const client_t& c); // -- -namespace { -inline std::ostream& format_u(std::ostream& out, const uint64_t v, const uint64_t n, - const int index, const uint64_t mult, const char* u) - { - char buffer[32]; - - if (index == 0) { - (void) snprintf(buffer, sizeof(buffer), "%" PRId64 "%s", n, u); - } else if ((v % mult) == 0) { - // If this is an even multiple of the base, always display - // without any decimal fraction. - (void) snprintf(buffer, sizeof(buffer), "%" PRId64 "%s", n, u); - } else { - // We want to choose a precision that reflects the best choice - // for fitting in 5 characters. This can get rather tricky when - // we have numbers that are very close to an order of magnitude. - // For example, when displaying 10239 (which is really 9.999K), - // we want only a single place of precision for 10.0K. We could - // develop some complex heuristics for this, but it's much - // easier just to try each combination in turn. - int i; - for (i = 2; i >= 0; i--) { - if (snprintf(buffer, sizeof(buffer), "%.*f%s", i, - static_cast(v) / mult, u) <= 7) - break; - } - } - - return out << buffer; - } -} - /* * Use this struct to pretty print values that should be formatted with a * decimal unit prefix (the classic SI units). No actual unit will be added. @@ -481,21 +439,7 @@ struct si_u_t { explicit si_u_t(uint64_t _v) : v(_v) {}; }; -inline std::ostream& operator<<(std::ostream& out, const si_u_t& b) -{ - uint64_t n = b.v; - int index = 0; - uint64_t mult = 1; - const char* u[] = {"", "k", "M", "G", "T", "P", "E"}; - - while (n >= 1000 && index < 7) { - n /= 1000; - index++; - mult *= 1000; - } - - return format_u(out, b.v, n, index, mult, u[index]); -} +std::ostream& operator<<(std::ostream& out, const si_u_t& b); /* * Use this struct to pretty print values that should be formatted with a @@ -513,25 +457,8 @@ struct byte_u_t { template <> struct fmt::formatter : fmt::ostream_formatter {}; #endif -inline std::ostream& operator<<(std::ostream& out, const byte_u_t& b) -{ - uint64_t n = b.v; - int index = 0; - const char* u[] = {" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB"}; - - while (n >= 1024 && index < 7) { - n /= 1024; - index++; - } - - return format_u(out, b.v, n, index, 1ULL << (10 * index), u[index]); -} - -inline std::ostream& operator<<(std::ostream& out, const ceph_mon_subscribe_item& i) -{ - return out << (long)i.start - << ((i.flags & CEPH_SUBSCRIBE_ONETIME) ? "" : "+"); -} +std::ostream& operator<<(std::ostream& out, const byte_u_t& b); +std::ostream& operator<<(std::ostream& out, const ceph_mon_subscribe_item& i); struct weightf_t { float v; @@ -539,17 +466,7 @@ struct weightf_t { weightf_t(float _v) : v(_v) {} }; -inline std::ostream& operator<<(std::ostream& out, const weightf_t& w) -{ - if (w.v < -0.01F) { - return out << "-"; - } else if (w.v < 0.000001F) { - return out << "0"; - } else { - std::streamsize p = out.precision(); - return out << std::fixed << std::setprecision(5) << w.v << std::setprecision(p); - } -} +std::ostream& operator<<(std::ostream& out, const weightf_t& w); struct shard_id_t { int8_t id; @@ -572,13 +489,8 @@ struct shard_id_t { using ceph::decode; decode(id, bl); } - void dump(ceph::Formatter *f) const { - f->dump_int("id", id); - } - static void generate_test_instances(std::list& ls) { - ls.push_back(new shard_id_t(1)); - ls.push_back(new shard_id_t(2)); - } + void dump(ceph::Formatter *f) const; + static void generate_test_instances(std::list& ls); shard_id_t& operator++() { ++id; return *this; } friend constexpr std::strong_ordering operator<=>(const shard_id_t &lhs, const shard_id_t &rhs) { @@ -650,13 +562,8 @@ struct errorcode32_t { decode(newcode, bl); set_wire_to_host(newcode); } - void dump(ceph::Formatter *f) const { - f->dump_int("code", code); - } - static void generate_test_instances(std::list& ls) { - ls.push_back(new errorcode32_t(1)); - ls.push_back(new errorcode32_t(2)); - } + void dump(ceph::Formatter *f) const; + static void generate_test_instances(std::list& ls); }; WRITE_CLASS_ENCODER(errorcode32_t) -- 2.39.5