pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal
6+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal
@ 2026-03-06 17:29 "cxmcc (@cxmcc)" <noreply+cxmcc@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: cxmcc (@cxmcc) @ 2026-03-06 17:29 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## Summary
- Optimize `SparsevecL2SquaredDistance` and `sparsevec_l1_distance` in `src/sparsevec.c` by switching from the previous nested-scan pattern to a linear two-pointer merge traversal over sorted indices.
- Keep `SparsevecInnerProduct` logic unchanged.

## Why this can improve performance
Sparse vectors store sorted indices. A two-pointer merge traversal can reduce control-flow overhead by walking both vectors in a single pass and handling leftovers in straight-line loops.

For L1/L2 distance kernels, this improves the mismatch-heavy path that dominates sparse workloads while preserving exact results.

## Latest benchmark results (PostgreSQL, L1/L2 only)
Bench setup:
- PostgreSQL 14, fresh temp cluster per variant
- `dim = 1,000,000`
- synthetic deterministic sparse data
- 5 measured runs per query (`EXPLAIN (ANALYZE, TIMING OFF)`)
- reverse-order run (`current` first, then `old`) to reduce order/cache bias

Speedup reported as `old / current`:

| nnz | L2 squared speedup | L1 speedup |
| ---: | ---: | ---: |
| 50  | 1.257x | 1.272x |
| 100 | 1.253x | 1.276x |
| 200 | 1.085x | 1.097x |
| 400 | 1.079x | 1.061x |

## Validation
- `make -j4`
- `make installcheck` (all regression tests passed)


inline patch 1: src/sparsevec.c (+43/-45 lines) download inline-1.patchdiff --git a/src/sparsevec.c b/src/sparsevec.c index d7811c75e..e70fd5f1a 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -816,40 +816,38 @@ SparsevecL2SquaredDistance(SparseVector * a, SparseVector * b) float *ax = SPARSEVEC_VALUES(a); float *bx = SPARSEVEC_VALUES(b); float distance = 0.0; - int bpos = 0; + int i = 0; + int j = 0; - for (int i = 0; i < a->nnz; i++) + while (i < a->nnz && j < b->nnz) { int ai = a->indices[i]; - int bi = -1; + int bi = b->indices[j]; - for (int j = bpos; j < b->nnz; j++) + if (ai == bi) { - bi = b->indices[j]; - - if (ai == bi) - { - float diff = ax[i] - bx[j]; - - distance += diff * diff; - } - else if (ai > bi) - distance += bx[j] * bx[j]; + float diff = ax[i] - bx[j]; - /* Update start for next iteration */ - if (ai >= bi) - bpos = j + 1; - - /* Found or passed it */ - if (bi >= ai) - break; + distance += diff * diff; + i++; + j++; } - - if (ai != bi) + else if (ai < bi) + { distance += ax[i] * ax[i]; + i++; + } + else + { + distance += bx[j] * bx[j]; + j++; + } } - for (int j = bpos; j < b->nnz; j++) + for (; i < a->nnz; i++) + distance += ax[i] * ax[i]; + + for (; j < b->nnz; j++) distance += bx[j] * bx[j]; return distance; @@ -1009,38 +1007,38 @@ sparsevec_l1_distance(PG_FUNCTION_ARGS) float *ax = SPARSEVEC_VALUES(a); float *bx = SPARSEVEC_VALUES(b); float distance = 0.0; - int bpos = 0; + int i = 0; + int j = 0; CheckDims(a, b); - for (int i = 0; i < a->nnz; i++) + while (i < a->nnz && j < b->nnz) { int ai = a->indices[i]; - int bi = -1; + int bi = b->indices[j]; - for (int j = bpos; j < b->nnz; j++) + if (ai == bi) { - bi = b->indices[j]; - - if (ai == bi) - distance += fabsf(ax[i] - bx[j]); - else if (ai > bi) - distance += fabsf(bx[j]); - - /* Update start for next iteration */ - if (ai >= bi) - bpos = j + 1; - - /* Found or passed it */ - if (bi >= ai) - break; + distance += fabsf(ax[i] - bx[j]); + i++; + j++; } - - if (ai != bi) + else if (ai < bi) + { distance += fabsf(ax[i]); + i++; + } + else + { + distance += fabsf(bx[j]); + j++; + } } - for (int j = bpos; j < b->nnz; j++) + for (; i < a->nnz; i++) + distance += fabsf(ax[i]); + + for (; j < b->nnz; j++) distance += fabsf(bx[j]); PG_RETURN_FLOAT8((double) distance);
^ permalink raw [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] PR #964 (approved): Optimize sparsevec L1/L2 distance traversal
@ 2026-03-07 05:50 ` "kitturmanasi-lgtm (@kitturmanasi-lgtm)" <noreply+kitturmanasi-lgtm@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: kitturmanasi-lgtm (@kitturmanasi-lgtm) @ 2026-03-07 05:50 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>



^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal
@ 2026-03-17 20:03 ` "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2026-03-17 20:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @cxmcc, thanks for the PR! Will spend some time benchmarking this. A few questions:

1. Can you share more about the benchmark setup for the numbers above (number of vectors, query, any indexes)?
2. Did you try the approach with inner product? If so, what were the results?

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal
@ 2026-03-17 23:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2026-03-17 23:35 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I'm seeing a regression on Linux and a speedup on Mac with seq scans, 100k vectors, 32k dimensions, and nnz = uniform(100, 1000):

Platform | L2 | L1 | IP ([branch](https://github.com/pgvector/pgvector/compare/sparsevec-ip))
--- | --- | --- | ---
Linux x86-64 | 1.00 | 0.98 | 0.97
Linux aarch64 | 0.95 | 0.97 | 0.97
Mac arm64 | 1.15 | 1.31 | 1.31

I also tried a static nnz, but the results were similar.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal
@ 2026-03-31 02:57 ` "cxmcc (@cxmcc)" <noreply+cxmcc@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: cxmcc (@cxmcc) @ 2026-03-31 02:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for review and the benchmarks and for catching the platform differences.

My setup used more sparse data and with/without indexes. Under those conditions, I consistently saw improvements for L1/L2. I did try inner product, but didn’t see improvement, so I didn't include IP in the original patch.

Given the lack of gains (and regression) on x86/amd64, it makes sense to close this PR.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal
@ 2026-03-31 02:57 ` "cxmcc (@cxmcc)" <noreply+cxmcc@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: cxmcc (@cxmcc) @ 2026-03-31 02:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@cxmcc closed this.

^ permalink  raw  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-03-31 02:57 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-06 17:29 [pgvector/pgvector] PR #964: Optimize sparsevec L1/L2 distance traversal "cxmcc (@cxmcc)" <noreply+cxmcc@github.com>
2026-03-07 05:50 ` Re: [pgvector/pgvector] PR #964 (approved): Optimize sparsevec L1/L2 distance traversal "kitturmanasi-lgtm (@kitturmanasi-lgtm)" <noreply+kitturmanasi-lgtm@github.com>
2026-03-17 20:03 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-17 23:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-31 02:57 ` "cxmcc (@cxmcc)" <noreply+cxmcc@github.com>
2026-03-31 02:57 ` "cxmcc (@cxmcc)" <noreply+cxmcc@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox