Use StringBuilder in TlStorerToString to speed up it 2 times.

This commit is contained in:
levlam 2023-09-29 13:48:22 +03:00
parent 811f90da2f
commit d84a021a48
1 changed files with 37 additions and 71 deletions

View File

@ -9,101 +9,72 @@
#include "td/utils/common.h"
#include "td/utils/SharedSlice.h"
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/StackAllocator.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/UInt.h"
namespace td {
class TlStorerToString {
string result;
size_t shift = 0;
decltype(StackAllocator::alloc(0)) buffer_ = StackAllocator::alloc(1 << 14);
StringBuilder sb_ = StringBuilder(buffer_.as_slice(), true);
size_t shift_ = 0;
void store_field_begin(const char *name) {
result.append(shift, ' ');
sb_.append_char(shift_, ' ');
if (name && name[0]) {
result += name;
result += " = ";
sb_ << name << " = ";
}
}
void store_field_end() {
result += '\n';
}
void store_long(int64 value) {
result += (PSLICE() << value).c_str();
sb_.push_back('\n');
}
void store_binary(Slice data) {
static const char *hex = "0123456789ABCDEF";
result.append("{ ", 2);
sb_ << "{ ";
for (auto c : data) {
unsigned char byte = c;
result += hex[byte >> 4];
result += hex[byte & 15];
result += ' ';
sb_.push_back(hex[byte >> 4]);
sb_.push_back(hex[byte & 15]);
sb_.push_back(' ');
}
result += '}';
sb_.push_back('}');
}
public:
TlStorerToString() = default;
TlStorerToString(const TlStorerToString &) = delete;
TlStorerToString &operator=(const TlStorerToString &) = delete;
void store_field(const char *name, bool value) {
store_field_begin(name);
result += (value ? "true" : "false");
store_field_end();
}
void store_field(const char *name, int32 value) {
store_field(name, static_cast<int64>(value));
}
void store_field(const char *name, int64 value) {
store_field_begin(name);
store_long(value);
store_field_end();
}
void store_field(const char *name, double value) {
store_field_begin(name);
result += (PSLICE() << value).c_str();
store_field_end();
}
void store_field(const char *name, const char *value) {
store_field_begin(name);
result += value;
store_field_end();
}
TlStorerToString(TlStorerToString &&) = delete;
TlStorerToString &operator=(TlStorerToString &&) = delete;
void store_field(const char *name, const string &value) {
store_field_begin(name);
result += '"';
result += value;
result += '"';
sb_.push_back('"');
sb_ << value;
sb_.push_back('"');
store_field_end();
}
void store_field(const char *name, const SecureString &value) {
store_field_begin(name);
result.append("<secret>");
sb_ << "<secret>";
store_field_end();
}
template <class T>
void store_field(const char *name, const T &value) {
store_field_begin(name);
result.append(value.data(), value.size());
sb_ << value;
store_field_end();
}
void store_bytes_field(const char *name, const SecureString &value) {
store_field_begin(name);
result.append("<secret>");
sb_ << "<secret>";
store_field_end();
}
@ -112,27 +83,25 @@ class TlStorerToString {
static const char *hex = "0123456789ABCDEF";
store_field_begin(name);
result.append("bytes [");
store_long(static_cast<int64>(value.size()));
result.append("] { ");
sb_ << "bytes [" << value.size() << "] { ";
size_t len = min(static_cast<size_t>(64), value.size());
for (size_t i = 0; i < len; i++) {
int b = value[static_cast<int>(i)] & 0xff;
result += hex[b >> 4];
result += hex[b & 15];
result += ' ';
sb_.push_back(hex[b >> 4]);
sb_.push_back(hex[b & 15]);
sb_.push_back(' ');
}
if (len < value.size()) {
result.append("...");
sb_ << "...";
}
result += '}';
sb_.push_back('}');
store_field_end();
}
template <class ObjectT>
void store_object_field(const char *name, const ObjectT *value) {
if (value == nullptr) {
store_field(name, "null");
store_field(name, Slice("null"));
} else {
value->store(*this, name);
}
@ -152,28 +121,25 @@ class TlStorerToString {
void store_vector_begin(const char *field_name, size_t vector_size) {
store_field_begin(field_name);
result += "vector[";
result += (PSLICE() << vector_size).c_str();
result += "] {\n";
shift += 2;
sb_ << "vector[" << vector_size << "] {\n";
shift_ += 2;
}
void store_class_begin(const char *field_name, const char *class_name) {
store_field_begin(field_name);
result += class_name;
result += " {\n";
shift += 2;
sb_ << class_name << " {\n";
shift_ += 2;
}
void store_class_end() {
CHECK(shift >= 2);
shift -= 2;
result.append(shift, ' ');
result += "}\n";
CHECK(shift_ >= 2);
shift_ -= 2;
sb_.append_char(shift_, ' ');
sb_ << "}\n";
}
string move_as_string() {
return std::move(result);
return sb_.as_cslice().str();
}
};