Skip to content

Commit eabb8d6

Browse files
authored
optimize iteration through collections and UDTs (#507)
the hot path involved many unnecessary SharedRefPtr's copies, avoiding them improves performance
1 parent 9f8f72d commit eabb8d6

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

src/collection_iterator.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ bool CollectionIterator::next() {
2727
}
2828

2929
bool CollectionIterator::decode_value() {
30-
DataType::ConstPtr data_type;
3130
if (collection_->value_type() == CASS_VALUE_TYPE_MAP) {
32-
data_type =
31+
const DataType::ConstPtr& data_type =
3332
(index_ % 2 == 0) ? collection_->primary_data_type() : collection_->secondary_data_type();
33+
value_ = decoder_.decode_value(data_type);
3434
} else {
35-
data_type = collection_->primary_data_type();
35+
value_ = decoder_.decode_value(collection_->primary_data_type());
3636
}
3737

38-
value_ = decoder_.decode_value(data_type);
3938
return value_.is_valid();
4039
}
4140

src/ref_counted.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ class SharedRefPtr {
8787
return *this;
8888
}
8989

90+
#if defined(__cpp_rvalue_references)
91+
SharedRefPtr<T>& operator=(SharedRefPtr<T>&& ref) noexcept {
92+
if (ptr_ != NULL) {
93+
ptr_->dec_ref();
94+
}
95+
ptr_ = ref.ptr_;
96+
ref.ptr_ = NULL;
97+
return *this;
98+
}
99+
100+
SharedRefPtr(SharedRefPtr<T>&& ref) noexcept : ptr_(ref.ptr_) { ref.ptr_ = NULL; }
101+
#endif
102+
90103
~SharedRefPtr() {
91104
if (ptr_ != NULL) {
92105
ptr_->dec_ref();

src/user_type_field_iterator.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class UserTypeFieldIterator : public Iterator {
3030
UserTypeFieldIterator(const Value* user_type_value)
3131
: Iterator(CASS_ITERATOR_TYPE_USER_TYPE_FIELD)
3232
, decoder_(user_type_value->decoder()) {
33-
UserType::ConstPtr user_type(user_type_value->data_type());
34-
next_ = user_type->fields().begin();
35-
end_ = user_type->fields().end();
33+
const UserType& user_type = static_cast<const UserType&>(*user_type_value->data_type());
34+
next_ = user_type.fields().begin();
35+
end_ = user_type.fields().end();
3636
}
3737

3838
virtual bool next();

src/value.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ Value::Value(const DataType::ConstPtr& data_type, Decoder decoder)
199199
, is_null_(false) {
200200
assert(!data_type->is_collection());
201201
if (data_type->is_tuple()) {
202-
SharedRefPtr<const CompositeType> composite_type(data_type);
203-
count_ = composite_type->types().size();
202+
const CompositeType& composite_type = static_cast<const CompositeType&>(*data_type);
203+
count_ = composite_type.types().size();
204204
} else if (data_type->is_user_type()) {
205-
UserType::ConstPtr user_type(data_type);
206-
count_ = user_type->fields().size();
205+
const UserType& user_type = static_cast<const UserType&>(*data_type);
206+
count_ = user_type.fields().size();
207207
} else {
208208
count_ = 0;
209209
}

0 commit comments

Comments
 (0)