Skip to content

Commit dab4625

Browse files
committed
Removed unnecessary copying
1 parent 37d8db2 commit dab4625

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

include/pgvector/halfvec.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class HalfVector {
4141
return value_;
4242
}
4343

44+
/// Returns the half vector as a `std::span<const float>`.
45+
operator const std::span<const float>() const {
46+
return value_;
47+
}
48+
4449
friend bool operator==(const HalfVector& lhs, const HalfVector& rhs) {
4550
return lhs.value_ == rhs.value_;
4651
}

include/pgvector/pqxx.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ template <> struct string_traits<pgvector::Vector> {
4747
}
4848

4949
static std::string_view to_buf(std::span<char> buf, const pgvector::Vector& value, ctx c = {}) {
50-
auto values = static_cast<std::vector<float>>(value);
50+
auto values = static_cast<std::span<const float>>(value);
5151

5252
// important! size_buffer cannot throw an exception on overflow
5353
// so perform this check before writing any data
@@ -71,7 +71,7 @@ template <> struct string_traits<pgvector::Vector> {
7171
}
7272

7373
static size_t size_buffer(const pgvector::Vector& value) noexcept {
74-
auto values = static_cast<std::vector<float>>(value);
74+
auto values = static_cast<std::span<const float>>(value);
7575

7676
// cannot throw an exception here on overflow
7777
// so throw in into_buf
@@ -111,7 +111,7 @@ template <> struct string_traits<pgvector::HalfVector> {
111111
}
112112

113113
static std::string_view to_buf(std::span<char> buf, const pgvector::HalfVector& value, ctx c = {}) {
114-
auto values = static_cast<std::vector<float>>(value);
114+
auto values = static_cast<std::span<const float>>(value);
115115

116116
// important! size_buffer cannot throw an exception on overflow
117117
// so perform this check before writing any data
@@ -135,7 +135,7 @@ template <> struct string_traits<pgvector::HalfVector> {
135135
}
136136

137137
static size_t size_buffer(const pgvector::HalfVector& value) noexcept {
138-
auto values = static_cast<std::vector<float>>(value);
138+
auto values = static_cast<std::span<const float>>(value);
139139

140140
// cannot throw an exception here on overflow
141141
// so throw in into_buf

include/pgvector/vector.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Vector {
4141
return value_;
4242
}
4343

44+
/// Returns the vector as a `std::span<const float>`.
45+
operator const std::span<const float>() const {
46+
return value_;
47+
}
48+
4449
friend bool operator==(const Vector& lhs, const Vector& rhs) {
4550
return lhs.value_ == rhs.value_;
4651
}

0 commit comments

Comments
 (0)