Skip to content

Commit 8529df1

Browse files
committed
Removed unnecessary copying
1 parent 9a6facb commit 8529df1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

include/pgvector/pqxx.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ template <> struct string_traits<pgvector::SparseVector> {
201201
cb(inner.substr(start));
202202
}
203203

204-
return pgvector::SparseVector(dimensions, indices, values);
204+
return pgvector::SparseVector(dimensions, std::move(indices), std::move(values));
205205
}
206206

207207
static std::string_view to_buf(std::span<char> buf, const pgvector::SparseVector& value, ctx c = {}) {

include/pgvector/sparsevec.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class SparseVector {
2828
values_ = values;
2929
}
3030

31+
/// @private
32+
SparseVector(int dimensions, std::vector<int>&& indices, std::vector<float>&& values) {
33+
if (values.size() != indices.size()) {
34+
throw std::invalid_argument("indices and values must be the same length");
35+
}
36+
dimensions_ = dimensions;
37+
indices_ = std::move(indices);
38+
values_ = std::move(values);
39+
}
40+
3141
/// Creates a sparse vector from a dense vector.
3242
explicit SparseVector(const std::vector<float>& value) {
3343
dimensions_ = value.size();

0 commit comments

Comments
 (0)