Skip to content

Commit 730ab85

Browse files
committed
Reduced copying
1 parent 54dfcdc commit 730ab85

7 files changed

Lines changed: 11 additions & 18 deletions

File tree

pgvector/bit.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ def from_text(cls, value: str) -> Bit:
7474
return cls(str(value))
7575

7676
@classmethod
77-
def from_binary(cls, value: bytes) -> Bit:
78-
if not isinstance(value, bytes):
79-
raise ValueError('expected bytes')
80-
77+
def from_binary(cls, value: bytes | bytearray | memoryview) -> Bit:
8178
length, = unpack_from('>i', value)
82-
data = value[4:]
79+
data = memoryview(value)[4:].tobytes()
8380

8481
if len(data) != (length + 7) // 8:
8582
raise ValueError('invalid length')

pgvector/halfvec.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ def from_text(cls, value: str) -> HalfVector:
6464
return cls(cls._from_text(value))
6565

6666
@classmethod
67-
def from_binary(cls, value: bytes) -> HalfVector:
67+
def from_binary(cls, value: bytes | bytearray | memoryview) -> HalfVector:
6868
dim, unused = struct.unpack_from('>HH', value)
69-
data = value[4:]
69+
data = memoryview(value)[4:]
7070

7171
if len(data) != 2 * dim:
7272
raise ValueError('invalid length')
7373

7474
if unused != 0:
7575
raise ValueError('expected unused to be 0')
7676

77-
arr = array.array('H', data)
77+
arr = array.array('H')
78+
arr.frombytes(data)
7879
if sys.byteorder != 'big':
7980
arr.byteswap()
8081

pgvector/psycopg/halfvec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class HalfVectorBinaryLoader(HalfVectorLoader):
3535
format = Format.BINARY
3636

3737
def load(self, data: Buffer) -> HalfVector | None:
38-
if isinstance(data, (bytearray, memoryview)):
39-
data = bytes(data)
4038
return HalfVector.from_binary(data)
4139

4240

pgvector/psycopg/sparsevec.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class SparseVectorBinaryLoader(SparseVectorLoader):
3535
format = Format.BINARY
3636

3737
def load(self, data: Buffer) -> SparseVector | None:
38-
if isinstance(data, (bytearray, memoryview)):
39-
data = bytes(data)
4038
return SparseVector.from_binary(data)
4139

4240

pgvector/psycopg/vector.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class VectorBinaryLoader(VectorLoader):
4646
format = Format.BINARY
4747

4848
def load(self, data: Buffer) -> Vector | None:
49-
if isinstance(data, (bytearray, memoryview)):
50-
data = bytes(data)
5149
return Vector.from_binary(data)
5250

5351

pgvector/sparsevec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def from_text(cls, value: str) -> SparseVector:
127127
return cls._from_parts(int(dim), indices, values)
128128

129129
@classmethod
130-
def from_binary(cls, value: bytes) -> SparseVector:
130+
def from_binary(cls, value: bytes | bytearray | memoryview) -> SparseVector:
131131
dim, nnz, unused = unpack_from('>iii', value)
132132

133133
if len(value) != 12 + 8 * nnz:

pgvector/vector.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ def from_text(cls, value: str) -> Vector:
6262
return cls(cls._from_text(value))
6363

6464
@classmethod
65-
def from_binary(cls, value: bytes) -> Vector:
65+
def from_binary(cls, value: bytes | bytearray | memoryview) -> Vector:
6666
dim, unused = struct.unpack_from('>HH', value)
67-
data = value[4:]
67+
data = memoryview(value)[4:]
6868

6969
if len(data) != 4 * dim:
7070
raise ValueError('invalid length')
7171

7272
if unused != 0:
7373
raise ValueError('expected unused to be 0')
7474

75-
arr = array.array('f', data)
75+
arr = array.array('f')
76+
arr.frombytes(data)
7677
if sys.byteorder != 'big':
7778
arr.byteswap()
7879

0 commit comments

Comments
 (0)