Skip to content

Commit 4dba27b

Browse files
committed
Reduced allocations [skip ci]
1 parent 04b2fd5 commit 4dba27b

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

pgvector/halfvec.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def to_binary(self) -> bytes:
6161

6262
@classmethod
6363
def from_text(cls, value: str) -> HalfVector:
64-
return cls([float(v) for v in value[1:-1].split(',')])
64+
return cls(cls._list_from_text(value))
6565

6666
@classmethod
6767
def from_binary(cls, value: bytes) -> HalfVector:
@@ -109,6 +109,10 @@ def _from_db(cls, value: str | HalfVector | None) -> list[float] | None:
109109
return value
110110

111111
if isinstance(value, str):
112-
value = cls.from_text(value)
112+
return cls._list_from_text(value)
113113

114114
return value.to_list()
115+
116+
@classmethod
117+
def _list_from_text(cls, value: str) -> list[float]:
118+
return [float(v) for v in value[1:-1].split(',')]

pgvector/vector.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def to_binary(self) -> bytes:
5959

6060
@classmethod
6161
def from_text(cls, value: str) -> Vector:
62-
return cls([float(v) for v in value[1:-1].split(',')])
62+
return cls(cls._list_from_text(value))
6363

6464
@classmethod
6565
def from_binary(cls, value: bytes) -> Vector:
@@ -107,6 +107,10 @@ def _from_db(cls, value: str | Vector | None) -> list[float] | None:
107107
return value
108108

109109
if isinstance(value, str):
110-
value = cls.from_text(value)
110+
return cls._list_from_text(value)
111111

112112
return value.to_list()
113+
114+
@classmethod
115+
def _list_from_text(cls, value: str) -> list[float]:
116+
return [float(v) for v in value[1:-1].split(',')]

0 commit comments

Comments
 (0)