Skip to content

Commit

Permalink
fixes #57 Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rpopescu committed Oct 8, 2020
1 parent 5c1f319 commit b0f2d60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
16 changes: 16 additions & 0 deletions clickhouse/base/wire_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class WireFormat {
static bool ReadFixed(CodedInputStream* input, T* value);

static bool ReadString(CodedInputStream* input, std::string* value);
static bool SkipString(CodedInputStream* input);

static bool ReadBytes(CodedInputStream* input, void* buf, size_t len);

Expand Down Expand Up @@ -53,6 +54,21 @@ inline bool WireFormat::ReadString(
return false;
}

inline bool WireFormat::SkipString(
CodedInputStream* input)
{
uint64_t len;

if (input->ReadVarint64(&len)) {
if (len > 0x00FFFFFFULL) {
return false;
}
return input->Skip((size_t)len);
}

return false;
}

inline bool WireFormat::ReadBytes(
CodedInputStream* input, void* buf, size_t len)
{
Expand Down
26 changes: 8 additions & 18 deletions clickhouse/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,14 @@ void Client::Impl::Insert(const std::string& table_name, const Block& block) {
RetryGuard([this]() { Ping(); });
}

std::vector<std::string> fields;
fields.reserve(block.GetColumnCount());

// Enumerate all fields
for (unsigned int i = 0; i < block.GetColumnCount(); i++) {
fields.push_back(block.GetColumnName(i));
}

std::stringstream fields_section;
const auto num_columns = block.GetColumnCount();

for (auto elem = fields.begin(); elem != fields.end(); ++elem) {
if (std::distance(elem, fields.end()) == 1) {
fields_section << *elem;
for (unsigned int i = 0; i < num_columns; ++i) {
if (i == num_columns - 1) {
fields_section << block.GetColumnName(i);
} else {
fields_section << *elem << ",";
fields_section << block.GetColumnName(i) << ",";
}
}

Expand Down Expand Up @@ -432,10 +425,9 @@ bool Client::Impl::ReadBlock(Block* block, CodedInputStream* input) {
return false;
}

std::string name;
std::string type;
for (size_t i = 0; i < num_columns; ++i) {
std::string name;
std::string type;

if (!WireFormat::ReadString(input, &name)) {
return false;
}
Expand All @@ -461,9 +453,7 @@ bool Client::Impl::ReceiveData() {
Block block;

if (REVISION >= DBMS_MIN_REVISION_WITH_TEMPORARY_TABLES) {
std::string table_name;

if (!WireFormat::ReadString(&input_, &table_name)) {
if (!WireFormat::SkipString(&input_)) {
return false;
}
}
Expand Down

0 comments on commit b0f2d60

Please sign in to comment.