Eliminate per-posting iterator allocations in HFresh filtered allowlist scans

perfloop-oss/weaviate · Allocation In Hot Loop

https://perfloop.ai/t/oss/case_pq2452cygs

Verdict

VERIFIED · settled 2026-11-24

What happened: The check fails before this change and passes with it.

Hypothesis

Every uncached posting probe during a filtered HFresh search allocates four heap objects and decodes vector IDs one byte at a time via a range-over-func iterator closure. Under filtered centroid search (both flat centroid scans and ACORN graph traversal), a single query can probe hundreds to thousands of postings, creating thousands of short-lived heap allocations and adding measurable latency. Production posting counts, packing widths, and query rates remain unmeasured.

In adapters/repos/db/vector/hfresh/allow_list.go:117-132, allowList.containsAllowedMember calls `for vectorID := range p.Iter()` under the posting read lock. PackedPostingMetadata.Iter (posting_map.go:312-338) returns an `iter.Seq[uint64]` closure and decodes each packed ID with an inner byte loop (`for j := range bytesPerID`) while invoking a yield callback per vector ID. Escape analysis (`go build -gcflags='-m'`) confirms that this range-over-func loop allocates four heap objects per call (`#rv1`, `#state1`, the range-body closure, and the `Iter` closure), and `testing.AllocsPerRun` measures 4.00 allocations per posting probed across all five packing width schemes (2, 3, 4, 5, and 8 bytes).

Local benchmarks on an Intel Xeon Platinum 8581C sandbox (4 CPUs, Go 1.27.4) on a 1,000-posting x 200-vector (64-dim) filtered SearchByVector workload with 1% matching postings showed that replacing `range p.Iter()` with direct scheme-specialized decoding of `p.PackedPostingMetadata` reduced: - ACORN filtered SearchByVector from 7.29-7.44 ms/op (930 KB-1.04 MB/op, 8,473-8,499 allocs/op) to 5.00-5.56 ms/op (658-703 KB/op, 760-792 allocs/op; ~91% fewer allocations and ~25-31% lower latency). - Flat-centroid filtered SearchByVector from 7.42-7.54 ms/op (3.60-4.00 MB/op, 8,381-8,406 allocs/op) to 5.12-6.07 ms/op (2.23-3.43 MB/op, 2,667-3,705 allocs/op; ~60-68% fewer allocations and ~19-31% lower latency).

This optimization is orthogonal to negative posting memoization (hyp_apdr2fwx6j): every first probe of a posting (and every posting scanned by AllowListIterator or flat centroid search) still enters containsAllowedMember. Checked open upstream PRs (#13657, #13947); neither replaces `range p.Iter()` in allow_list.go.

The attached provisional check (TestAllowListContainsAllowedMemberZeroAlloc) sweeps all five packing width schemes (2, 3, 4, 5, and 8 bytes) on both rejected and matching uncached postings; it fails on unchanged code with 4.00 allocs/posting on all 10 subtests and passes with 0.00 allocs/posting when decoded directly. Case proof should verify exact behavioral equivalence across all packing schemes (including empty/short metadata and legacy schemes), zero allocations on uncached probes, and end-to-end filtered SearchByVector latency and allocation reductions on both ACORN and flat centroid search regimes.

Change to test: Decode packed posting vector IDs directly by packing width scheme inside allowList.containsAllowedMember (or a non-allocating PackedPostingMetadata helper) instead of ranging over PackedPostingMetadata.Iter(), eliminating per-posting iterator closure allocations and byte-by-byte inner loops.

Where it lives

perfloop-oss/weaviate

Evidence

The check fails before this change and passes with it: `After allow-list and visited-set storage are warmed, an uncached HFresh posting-membership probe makes zero heap allocations for each 2-, 3-, 4-, 5- and 8-byte packing scheme, on rejection and on first, middle or last member matches.`

Filtered SearchByVector (flat centroids) · 30 sample pairs

metric baseline candidate paired median change confidence range required result
narrow ns/op 4902024 3606268 −26.9% (−1316678) −1525702 to −1209282 gain > 0 PASSED
narrow B/op 680310 567344 −16.6% (−112976) −113029 to −112937 gain > 0 PASSED
narrow allocs/op 4253 253 −94.1% (−4000) −4000 to −4000 gain > 0 PASSED
broad ns/op 12605484 9744375 −6.5% (−820643) −3780264 to −237154 no regression (noise ≤ 0.05 × |baseline median|) PASSED
unfiltered ns/op 6690008 6595244 −128352 −544941 to +281674 no regression (noise ≤ 0.05 × |baseline median|) PASSED

Filtered SearchByVector (ACORN) · 71 sample pairs

metric baseline candidate paired median change confidence range required result
narrow ns/op 4354733 3284077 −25.2% (−1096367) −1162737 to −1016704 gain > 0 PASSED
narrow B/op 623119 517845 −16.9% (−105285) −105391 to −105180 gain > 0 PASSED
narrow allocs/op 3974 246 −93.8% (−3728) −3728 to −3728 gain > 0 PASSED
broad ns/op 10568786 9874134 −7% (−743522) −2070168 to −501633 no regression (noise ≤ 0.05 × |baseline median|) PASSED
unfiltered ns/op 6614929 6699844 +23085 −225013 to +298683 no regression (noise ≤ 0.05 × |baseline median|) PASSED

Checks: 2 of 2 passed. Verification: no defect found.

Timeline