Skip to content

Commit e88785d

Browse files
Deryuginalpinskiy
authored andcommitted
Simplify pack_string() signature
1 parent b3254af commit e88785d

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

statshouse.hpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ class TransportUDPBase {
182182
MetricBuilder & tag(string_view key, string_view str) {
183183
auto begin = buffer + buffer_pos;
184184
auto end = buffer + MAX_FULL_KEY_SIZE;
185-
if (STATSHOUSE_UNLIKELY(!(begin = pack_string(begin, end, key.data(), key.size())))) { did_not_fit = true; return *this; }
186-
if (STATSHOUSE_UNLIKELY(!(begin = pack_string(begin, end, str.data(), str.size())))) { did_not_fit = true; return *this; }
185+
if (STATSHOUSE_UNLIKELY(!(begin = pack_string(begin, end, key)))) { did_not_fit = true; return *this; }
186+
if (STATSHOUSE_UNLIKELY(!(begin = pack_string(begin, end, str)))) { did_not_fit = true; return *this; }
187187
buffer_pos = begin - buffer;
188188
tags_count++;
189189
return *this;
@@ -221,7 +221,7 @@ class TransportUDPBase {
221221
auto begin = buffer + buffer_pos;
222222
auto end = buffer + MAX_FULL_KEY_SIZE;
223223
if (STATSHOUSE_UNLIKELY(!(begin = pack32(begin, end, get_tag_name_tl(i))))) { did_not_fit = true; return *this; }
224-
if (STATSHOUSE_UNLIKELY(!(begin = pack_string(begin, end, str.data(), str.size())))) { did_not_fit = true; return *this; }
224+
if (STATSHOUSE_UNLIKELY(!(begin = pack_string(begin, end, str)))) { did_not_fit = true; return *this; }
225225
buffer_pos = begin - buffer;
226226
tags_count++;
227227
return *this;
@@ -236,7 +236,7 @@ class TransportUDPBase {
236236
static_assert(MAX_FULL_KEY_SIZE > 4 + TL_MAX_TINY_STRING_LEN + 4, "not enough space to not overflow in constructor");
237237
auto begin = buffer + buffer_pos;
238238
auto end = buffer + MAX_FULL_KEY_SIZE;
239-
begin = pack_string(begin, end, m.data(), m.size());
239+
begin = pack_string(begin, end, m);
240240
begin = pack32(begin, end, 0); // place for tags_count. Never updated in buffer and always stays 0,
241241
buffer_pos = begin - buffer;
242242
}
@@ -266,7 +266,7 @@ class TransportUDPBase {
266266
auto end = begin + default_env_tl_pair.size();
267267

268268
begin = pack32(begin, end, pack_key_name(0));
269-
begin = pack_string(begin, end, default_env.data(), default_env.size());
269+
begin = pack_string(begin, end, default_env);
270270

271271
default_env_tl_pair.resize(begin - &default_env_tl_pair[0]);
272272
}
@@ -463,27 +463,30 @@ class TransportUDPBase {
463463
return 0; // if even more tags added, we do not care, so empty string is good enough.
464464
return names[i];
465465
}
466-
static char * pack_string(char * begin, const char * end, const char * str, size_t len) {
466+
static char *pack_string(char *begin, const char *end, string_view str) {
467+
size_t len = str.size();
468+
const char *data = str.data();
469+
467470
if (STATSHOUSE_UNLIKELY(len > TL_MAX_TINY_STRING_LEN)) {
468471
if (STATSHOUSE_UNLIKELY(len > TL_BIG_STRING_LEN)) {
469472
len = TL_BIG_STRING_LEN;
470473
}
471-
auto fullLen = (4 + len + 3) & ~3;
474+
const size_t fullLen = (4 + len + 3) & ~3;
472475
if (STATSHOUSE_UNLIKELY(!enoughSpace(begin, end, fullLen))) {
473476
return nullptr;
474477
}
475478
put32(begin + fullLen - 4, 0); // padding first
476479
put32(begin, (len << 8U) | TL_BIG_STRING_MARKER);
477-
std::memcpy(begin+4, str, len);
480+
std::memcpy(begin+4, data, len);
478481
begin += fullLen;
479482
} else {
480-
auto fullLen = (1 + len + 3) & ~3;
483+
const size_t fullLen = (1 + len + 3) & ~3;
481484
if (STATSHOUSE_UNLIKELY(!enoughSpace(begin, end, fullLen))) {
482485
return nullptr;
483486
}
484487
put32(begin + fullLen - 4, 0); // padding first
485488
*begin = static_cast<char>(len); // or put32(p, len);
486-
std::memcpy(begin+1, str, len);
489+
std::memcpy(begin+1, data, len);
487490
begin += fullLen;
488491
}
489492
return begin;

0 commit comments

Comments
 (0)