From 095d6dedb52c3875175c8ccd169ec9c6ce73cfb3 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 19 Jul 2026 17:59:35 +0000 Subject: [PATCH 1/2] perf: Optimize transpose matrix multiplication in SnapIndex Replaces `(self._S @ r_scaled.T).T` with `r_scaled @ self._S.T` to avoid creating intermediate memory allocations and F-contiguous views, returning a C-contiguous array directly and offering a measurable speedup. Co-authored-by: stffns <70039235+stffns@users.noreply.github.com> --- snapvec/_index.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/snapvec/_index.py b/snapvec/_index.py index fdc793e..b4c95f9 100644 --- a/snapvec/_index.py +++ b/snapvec/_index.py @@ -303,7 +303,8 @@ def add_batch(self, ids: list[Any], vectors: NDArray[np.float32]) -> None: reconstructed: NDArray[np.float32] = self._centroids[batch_idx] r_scaled: NDArray[np.float32] = scaled - reconstructed # sign(S·r_rot) = sign(S·r_scaled) — scale-invariant - S_r: NDArray[np.float32] = (self._S @ r_scaled.T).T + # Optimized: r_scaled @ S.T is faster and avoids intermediate F-contiguous views + S_r: NDArray[np.float32] = r_scaled @ self._S.T qjl_signs = np.sign(S_r).astype(np.int8) qjl_signs[qjl_signs == 0] = 1 # Store ‖r_rot‖ = ‖r_scaled‖/√pdim (unscaled space norm) From 91da2dd959aa3c5be4b5606efd6d48c9762fb612 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 19 Jul 2026 18:04:47 +0000 Subject: [PATCH 2/2] perf: Optimize transpose matrix multiplication in SnapIndex Replaces `(self._S @ r_scaled.T).T` with `r_scaled @ self._S.T` to avoid creating intermediate memory allocations and F-contiguous views, returning a C-contiguous array directly and offering a measurable speedup. Also pinned `numpy<2.5.0` in CI to fix mypy parsing error. Co-authored-by: stffns <70039235+stffns@users.noreply.github.com> --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d28011b..68d9c54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,7 @@ jobs: - name: Install dev dependencies run: | python -m pip install --upgrade pip + pip install "numpy<2.5.0" pip install -e ".[dev]" - name: ruff check @@ -60,6 +61,7 @@ jobs: - name: Install package run: | python -m pip install --upgrade pip + pip install "numpy<2.5.0" pip install -e ".[dev]" - name: Run tests