Skip to content

Commit 9f8f72d

Browse files
zakalibitAlex Revetchi
andauthored
Performance improvement in Decoder::decode_value (#505)
* - decode_row() performance improvement * - reduce branching Co-authored-by: Alex Revetchi <[email protected]>
1 parent 6d0ccf7 commit 9f8f72d

File tree

7 files changed

+37
-34
lines changed

7 files changed

+37
-34
lines changed

src/collection_iterator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ bool CollectionIterator::decode_value() {
3535
data_type = collection_->primary_data_type();
3636
}
3737

38-
return decoder_.decode_value(data_type, value_, true);
38+
value_ = decoder_.decode_value(data_type);
39+
return value_.is_valid();
3940
}
4041

4142
bool TupleIterator::next() {
4243
if (next_ == end_) {
4344
return false;
4445
}
4546
current_ = next_++;
46-
return decoder_.decode_value(*current_, value_);
47+
48+
value_ = decoder_.decode_value(*current_);
49+
return value_.is_valid();
4750
}

src/decoder.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,33 +138,22 @@ bool Decoder::decode_warnings(WarningVec& output) {
138138
return true;
139139
}
140140

141-
bool Decoder::decode_value(const DataType::ConstPtr& data_type, Value& output,
142-
bool is_inside_collection /*= false*/) {
143-
const char* buffer = NULL;
141+
Value Decoder::decode_value(const DataType::ConstPtr& data_type) {
144142
int32_t size = 0;
145-
146-
if (!decode_int32(size)) {
147-
return false;
148-
}
143+
if (!decode_int32(size)) return Value();
149144

150145
if (size >= 0) {
151-
buffer = input_;
146+
Decoder decoder(input_, size, protocol_version_);
152147
input_ += size;
153148
remaining_ -= size;
154-
Decoder decoder(buffer, size, protocol_version_);
155-
156-
if (data_type->is_collection()) {
157-
int32_t count;
158-
if (!decoder.decode_int32(count)) return false;
159-
output = Value(data_type, count, decoder);
160-
} else {
161-
output = Value(data_type, decoder);
149+
150+
int32_t count = 0;
151+
if (data_type->is_collection() && !decoder.decode_int32(count)) {
152+
return Value();
162153
}
163-
} else { // null value
164-
output = Value(data_type);
154+
return Value(data_type, count, decoder);
165155
}
166-
167-
return true;
156+
return Value(data_type);
168157
}
169158

170159
void Decoder::notify_error(const char* detail, size_t bytes) const {

src/decoder.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ class Decoder {
558558
bool decode_write_type(CassWriteType& output);
559559
bool decode_warnings(WarningVec& output);
560560

561-
bool decode_value(const DataType::ConstPtr& data_type, Value& output,
562-
bool is_inside_collection = false);
561+
Value decode_value(const DataType::ConstPtr& data_type);
563562

564563
protected:
565564
// Testing only

src/map_iterator.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
using namespace datastax::internal::core;
2020

2121
bool MapIterator::decode_pair() {
22-
if (!decoder_.decode_value(map_->primary_data_type(), key_, true)) return false;
23-
return decoder_.decode_value(map_->secondary_data_type(), value_, true);
22+
key_ = decoder_.decode_value(map_->primary_data_type());
23+
if (key_.data_type()) {
24+
value_ = decoder_.decode_value(map_->secondary_data_type());
25+
return value_.is_valid();
26+
}
27+
return false;
2428
}
2529

2630
bool MapIterator::next() {

src/row.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,19 @@ namespace datastax { namespace internal { namespace core {
5151

5252
bool decode_row(Decoder& decoder, const ResultResponse* result, OutputValueVec& output) {
5353
output.clear();
54-
output.reserve(result->column_count());
55-
for (int i = 0; i < result->column_count(); ++i) {
56-
Value value;
57-
const ColumnDefinition& def = result->metadata()->get_column_definition(i);
58-
CHECK_RESULT(decoder.decode_value(def.data_type, value));
59-
output.push_back(value);
54+
const int column_count = result->column_count();
55+
if (column_count > 0) {
56+
output.reserve(column_count);
57+
const SharedRefPtr<ResultMetadata>& metadata = result->metadata();
58+
for (int i = 0; i < column_count; ++i) {
59+
const ColumnDefinition& def = metadata->get_column_definition(i);
60+
Value value = decoder.decode_value(def.data_type);
61+
if (value.is_valid()) {
62+
output.push_back(value);
63+
} else
64+
return false;
65+
}
6066
}
61-
6267
return true;
6368
}
6469

src/user_type_field_iterator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ bool UserTypeFieldIterator::next() {
2525
return false;
2626
}
2727
current_ = next_++;
28-
return decoder_.decode_value(current_->type, value_);
28+
value_ = decoder_.decode_value(current_->type);
29+
return value_.is_valid();
2930
}

src/value.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Value {
5151
ProtocolVersion protocol_version() const { return decoder_.protocol_version(); }
5252
int64_t size() const { return (is_null_ ? -1 : decoder_.remaining()); }
5353

54+
bool is_valid() const { return !!data_type_; }
55+
5456
CassValueType value_type() const {
5557
if (!data_type_) {
5658
return CASS_VALUE_TYPE_UNKNOWN;

0 commit comments

Comments
 (0)