Skip to content

Commit 191bee7

Browse files
committed
Reduced copying [skip ci]
1 parent e3e4308 commit 191bee7

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

‎pgvector/halfvec.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ def __init__(self, value: list[float] | np.ndarray[tuple[int], np.dtype[np.float
2222
if value.ndim != 1:
2323
raise ValueError('expected ndim to be 1')
2424

25-
if value.dtype != np.float16:
26-
value = np.asarray(value, dtype=np.float16)
27-
28-
# tobytes() important for performance
29-
self._value = array.array('H', value.tobytes())
25+
arr = array.array('H')
26+
arr.frombytes(value.astype(np.float16, order='C', copy=False).data.cast('B'))
27+
self._value = arr
3028
else:
3129
raise ValueError('expected list or ndarray')
3230

‎pgvector/vector.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ def __init__(self, value: list[float] | np.ndarray[tuple[int], np.dtype[np.float
2121
if value.ndim != 1:
2222
raise ValueError('expected ndim to be 1')
2323

24-
if value.dtype != np.float32:
25-
value = np.asarray(value, dtype=np.float32)
26-
27-
# tobytes() important for performance
28-
self._value = array.array('f', value.tobytes())
24+
arr = array.array('f')
25+
arr.frombytes(value.astype(np.float32, order='C', copy=False).data.cast('B'))
26+
self._value = arr
2927
else:
3028
raise ValueError('expected list or ndarray')
3129

0 commit comments

Comments
 (0)