From: Evgeniy Firsov Date: Wed, 25 Nov 2015 22:13:55 +0000 (-0800) Subject: osd: Replace snprintf with faster implementation in eversion_t::get_key_name X-Git-Tag: v10.1.0~208^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=442be027114e17f2402fe4c1c143621dd29d77dc;p=ceph.git osd: Replace snprintf with faster implementation in eversion_t::get_key_name Signed-off-by: Evgeniy Firsov --- diff --git a/src/common/strtol.h b/src/common/strtol.h index 5575ed7b390b..6beee23d2cf2 100644 --- a/src/common/strtol.h +++ b/src/common/strtol.h @@ -48,4 +48,26 @@ Target strict_si_cast(const char *str, std::string *err) { template <> uint64_t strict_si_cast(const char *str, std::string *err); +/* On enter buf points to the end of the buffer, e.g. where the least + * significant digit of the input number will be printed. Returns pointer to + * where the most significant digit were printed, including zero padding. + * Does NOT add zero at the end of buffer, this is responsibility of the caller. + */ +template +static inline +char* ritoa(T u, char *buf) +{ + static_assert(std::is_unsigned::value, "signed types are not supported"); + static_assert(base <= 16, "extend character map below to support higher bases"); + unsigned digits = 0; + while (u) { + *--buf = "0123456789abcdef"[u % base]; + u /= base; + digits++; + } + while (digits++ < width) + *--buf = '0'; + return buf; +} + #endif diff --git a/src/osd/osd_types.cc b/src/osd/osd_types.cc index 2d874e5fd826..1c04d49c35b1 100644 --- a/src/osd/osd_types.cc +++ b/src/osd/osd_types.cc @@ -121,22 +121,6 @@ string ceph_osd_op_flag_string(unsigned flags) return string("-"); } -const char *num_char_map = "0123456789abcdef"; -template -static inline -char* ritoa(T u, char *buf) { - if (u < base) { - *--buf = num_char_map[u]; - return buf; - } - - while (u) { - *--buf = num_char_map[u % base]; - u /= base; - } - return buf; -} - void pg_shard_t::encode(bufferlist &bl) const { ENCODE_START(1, 1, bl); @@ -463,7 +447,7 @@ char *spg_t::calc_name(char *buf, const char *suffix_backwords) const *--buf = *suffix_backwords++; if (!is_no_shard()) { - buf = ritoa(shard.id, buf); + buf = ritoa((uint8_t)shard.id, buf); *--buf = 's'; } @@ -884,9 +868,12 @@ int pg_string_state(const std::string& state) // -- eversion_t -- string eversion_t::get_key_name() const { - char key[40]; - snprintf( - key, sizeof(key), "%010u.%020llu", epoch, (long long unsigned)version); + char key[32]; + // Below is equivalent of sprintf("%010u.%020llu"); + key[31] = 0; + ritoa(version, key + 31); + key[10] = '.'; + ritoa(epoch, key + 10); return string(key); }