Skip to content

Commit 2ae28ba

Browse files
committed
Improved performance of Bit constructor with list argument [skip ci]
1 parent 515f861 commit 2ae28ba

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

pgvector/bit.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ def __init__(self, value: bytes | str | list[bool] | np.ndarray[tuple[int, ...],
1717
self._data = value
1818
elif isinstance(value, (list, str)):
1919
if isinstance(value, list):
20-
def bit_value(v: bool) -> str:
21-
if v is True:
22-
return '1'
23-
if v is False:
24-
return '0'
20+
bits = {True: '1', False: '0'}
21+
try:
22+
value = ''.join([bits[v] for v in value])
23+
except (KeyError, TypeError):
2524
raise ValueError('expected list[bool]')
2625

27-
value = ''.join([bit_value(v) for v in value])
28-
2926
length = len(value)
3027
if length % 8 != 0:
3128
value += '0' * (8 - (length % 8))

0 commit comments

Comments
 (0)