Skip to content

perf(vectorindex): zero-copy GetVector/GetVectorRef via unsafe.Slice on mmap#58

Merged
oc-pegasus merged 5 commits into
mainfrom
perf/mmap-getvector-zerocopy
Apr 18, 2026
Merged

perf(vectorindex): zero-copy GetVector/GetVectorRef via unsafe.Slice on mmap#58
oc-pegasus merged 5 commits into
mainfrom
perf/mmap-getvector-zerocopy

Conversation

@oc-pegasus

Copy link
Copy Markdown
Collaborator

MmapStore GetVector 零拷贝优化

改动

  • GetVector: 128次 binary.Uint32 反序列化 → unsafe.Slice + 单次 copy(512B)
  • GetVectorRef: 返回 unsafe.Slice 直接指向 mmap 区域(真零拷贝)

50K SIFT-128 Benchmark

指标 优化前 优化后 变化
Insert 50K 604s 534s ⬇️ -12%
Search p50 0.63ms 0.23ms ⬇️ -63%
Search p99 0.98ms 0.39ms ⬇️ -60%
Recall@10 0.997 0.997 持平

搜索延迟提升 2.5x。

oc-pegasus and others added 5 commits April 18, 2026 09:44
Replace per-element binary.LittleEndian.Uint32 deserialization (128 iterations)
with a single unsafe.Slice + copy for GetVector, and true zero-copy return for
GetVectorRef (caller must not retain past grow/remap).

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
…p reference

GetVectorRef previously returned an unsafe.Slice directly into the mmap
region, which could become invalid after grow/remap. Delegate to GetVector
which already does unsafe.Slice + copy, eliminating the safety hazard while
retaining the binary-read performance benefit over binary.Uint32 decoding.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
Verify the mmap offset is 4-byte aligned before casting to *float32 via
unsafe.Pointer. The layout guarantees alignment (pageSize=4096 header,
dim*4 slot size), but an explicit check prevents silent UB if the format
ever changes.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
The mmap store reads float32/uint32/uint64 directly from mapped memory
assuming LE byte order. Add an init() panic guard so builds on BE
platforms fail fast instead of silently returning corrupt data.

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
@oc-pegasus oc-pegasus merged commit 2c5010c into main Apr 18, 2026
3 checks passed
@oc-pegasus oc-pegasus deleted the perf/mmap-getvector-zerocopy branch April 18, 2026 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants