Skip to content

Commit

Permalink
Reduce string use in coll_t::calc_str()
Browse files Browse the repository at this point in the history
1. Reduce string use.
2. Speedup dec and hex to char conversion.

Signed-off-by: Igor Podoski <[email protected]>
  • Loading branch information
Igor Podoski committed Jan 11, 2016
1 parent 089e1a2 commit 95e885e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 16 deletions.
67 changes: 55 additions & 12 deletions src/osd/osd_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ string ceph_osd_op_flag_string(unsigned flags)
return string("-");
}

const char *num_char_map = "0123456789abcdef";
template<typename T, const int base>
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);
Expand Down Expand Up @@ -441,11 +457,24 @@ bool spg_t::parse(const char *s)
return true;
}

char *spg_t::calc_name(char *buf, const char *suffix_backwords) const
{
while (*suffix_backwords)
*--buf = *suffix_backwords++;

if (!is_no_shard()) {
buf = ritoa<int8_t, 10>(shard.id, buf);
*--buf = 's';
}

return pgid.calc_name(buf, "");
}

ostream& operator<<(ostream& out, const spg_t &pg)
{
out << pg.pgid;
if (!pg.is_no_shard())
out << "s" << (unsigned)pg.shard;
char buf[spg_t::calc_name_buf_size];
buf[spg_t::calc_name_buf_size - 1] = '\0';
out << pg.calc_name(buf + spg_t::calc_name_buf_size - 1, "");
return out;
}

Expand Down Expand Up @@ -561,15 +590,26 @@ void pg_t::generate_test_instances(list<pg_t*>& o)
o.push_back(new pg_t(131223, 4, 23));
}

ostream& operator<<(ostream& out, const pg_t &pg)
char *pg_t::calc_name(char *buf, const char *suffix_backwords) const
{
out << pg.pool() << '.';
out << hex << pg.ps() << dec;
while (*suffix_backwords)
*--buf = *suffix_backwords++;

if (m_preferred >= 0)
*--buf ='p';

buf = ritoa<uint32_t, 16>(m_seed, buf);

if (pg.preferred() >= 0)
out << 'p' << pg.preferred();
*--buf = '.';

//out << "=" << hex << (__uint64_t)pg << dec;
return ritoa<uint64_t, 10>(m_pool, buf);
}

ostream& operator<<(ostream& out, const pg_t &pg)
{
char buf[pg_t::calc_name_buf_size];
buf[pg_t::calc_name_buf_size - 1] = '\0';
out << pg.calc_name(buf + pg_t::calc_name_buf_size - 1, "");
return out;
}

Expand All @@ -580,13 +620,16 @@ void coll_t::calc_str()
{
switch (type) {
case TYPE_META:
_str = "meta";
strcpy(_str_buff, "meta");
_str = _str_buff;
break;
case TYPE_PG:
_str = stringify(pgid) + "_head";
_str_buff[spg_t::calc_name_buf_size - 1] = '\0';
_str = pgid.calc_name(_str_buff + spg_t::calc_name_buf_size - 1, "daeh_");
break;
case TYPE_PG_TEMP:
_str = stringify(pgid) + "_TEMP";
_str_buff[spg_t::calc_name_buf_size - 1] = '\0';
_str = pgid.calc_name(_str_buff + spg_t::calc_name_buf_size - 1, "PMET_");
break;
default:
assert(0 == "unknown collection type");
Expand Down
26 changes: 22 additions & 4 deletions src/osd/osd_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ struct pg_t {
return m_preferred;
}

static const uint8_t calc_name_buf_size = 36; // max length for max values len("18446744073709551615.ffffffff") + future suffix len("_head") + '\0'
char *calc_name(char *buf, const char *suffix_backwords) const;

void set_ps(ps_t p) {
m_seed = p;
}
Expand Down Expand Up @@ -448,6 +451,10 @@ struct spg_t {
int32_t preferred() const {
return pgid.preferred();
}

static const uint8_t calc_name_buf_size = pg_t::calc_name_buf_size + 4; // 36 + len('s') + len("255");
char *calc_name(char *buf, const char *suffix_backwords) const;

bool parse(const char *s);
bool parse(const std::string& s) {
return parse(s.c_str());
Expand Down Expand Up @@ -523,7 +530,8 @@ class coll_t {
spg_t pgid;
uint64_t removal_seq; // note: deprecated, not encoded

string _str; // cached string
char _str_buff[spg_t::calc_name_buf_size];
char *_str;

void calc_str();

Expand All @@ -549,6 +557,15 @@ class coll_t {
calc_str();
}

coll_t& operator=(const coll_t& rhs)
{
this->type = rhs.type;
this->pgid = rhs.pgid;
this->removal_seq = rhs.removal_seq;
this->calc_str();
return *this;
}

// named constructors
static coll_t meta() {
return coll_t();
Expand All @@ -557,12 +574,13 @@ class coll_t {
return coll_t(p);
}

const std::string& to_str() const {
return _str;
const std::string to_str() const {
return string(_str);
}
const char *c_str() const {
return _str.c_str();
return _str;
}

bool parse(const std::string& s);

int operator<(const coll_t &rhs) const {
Expand Down
19 changes: 19 additions & 0 deletions src/test/osd/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,25 @@ TEST(coll_t, temp) {
ASSERT_EQ(pgid, pgid2);
}

TEST(coll_t, assigment) {
spg_t pgid;
coll_t right(pgid);
ASSERT_EQ(right.to_str(), string("0.0_head"));

coll_t left, middle;

ASSERT_EQ(left.to_str(), string("meta"));
ASSERT_EQ(middle.to_str(), string("meta"));

left = middle = right;

ASSERT_EQ(left.to_str(), string("0.0_head"));
ASSERT_EQ(middle.to_str(), string("0.0_head"));

ASSERT_NE(middle.c_str(), right.c_str());
ASSERT_NE(left.c_str(), middle.c_str());
}

TEST(ghobject_t, cmp) {
ghobject_t min;
ghobject_t sep;
Expand Down

0 comments on commit 95e885e

Please sign in to comment.