Skip to content

chore: avoid redundant IndexDocument construction in heap emplace#152

Open
longway-code wants to merge 2 commits intoalibaba:mainfrom
longway-code:chore/flat-heap-early-pruning
Open

chore: avoid redundant IndexDocument construction in heap emplace#152
longway-code wants to merge 2 commits intoalibaba:mainfrom
longway-code:chore/flat-heap-early-pruning

Conversation

@longway-code
Copy link
Contributor

@longway-code longway-code commented Feb 19, 2026

Summary

IndexDocumentHeap::emplace now checks score < front().score() before calling into ailego::Heap, avoiding unnecessary object construction for candidates that would be rejected anyway.

Performance

Tested locally, decreased ~15% end-to-end latency of FlatSearcher::search_impl (N=100k, dim=128, topk=10, FP32 SquaredEuclidean, 200 queries averaged).

All Tests Passsed

…e cannot improve top-k

In IndexDocumentHeap::emplace, the parent ailego::Heap::emplace constructs
the IndexDocument object before checking whether the candidate score beats
the current heap front. For rejected candidates this construction is wasted.

Add an early-exit guard: when the heap is full, skip the emplace call
entirely if score >= front().score(). The short-circuit evaluation of ||
ensures front() is only accessed when the heap is non-empty.

Benchmark (N=100k, dim=128, topk=10, 200 queries): ~14% latency reduction.
@longway-code longway-code changed the title chroe: avoid redundant IndexDocument construction in heap emplace chore: avoid redundant IndexDocument construction in heap emplace Feb 19, 2026
@iaojnh
Copy link
Collaborator

iaojnh commented Mar 2, 2026

@greptile

@greptile-apps
Copy link

greptile-apps bot commented Mar 2, 2026

Greptile Summary

Optimizes IndexDocumentHeap::emplace by checking if a candidate would be rejected before constructing the IndexDocument object. The implementation adds a guard condition (!this->full() || score < this->front().score()) that mirrors the internal logic of ailego::Heap::emplace, preventing unnecessary object construction when:

  • The heap is full AND
  • The new score is not better than the current worst score

This optimization is semantically equivalent to the original implementation but avoids constructing IndexDocument objects that would be immediately discarded, achieving the reported ~15% latency improvement in search operations.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The optimization is a well-contained change that adds an early-rejection check mirroring the existing heap logic. The implementation correctly handles all edge cases (empty heap, not-full heap, full heap) through proper short-circuit evaluation, and the reported 15% performance improvement demonstrates measurable benefit with no functional changes to the algorithm
  • No files require special attention

Important Files Changed

Filename Overview
src/include/zvec/core/framework/index_document.h Added early rejection check in IndexDocumentHeap::emplace to avoid constructing IndexDocument objects that would be rejected by the heap

Last reviewed commit: fe83ee8

@richyreachy
Copy link
Collaborator

hi @longway-code, thanks for your kindly contribution.

As we see the fix, the condition to avoid emplace, is quite same as the emplace inner logical condition.

We'll take a test to see if this can bring the mentioned performance improvement.

@richyreachy
Copy link
Collaborator

Hi, @longway-code, a test we took by @iaojnh shows there's no remarkable improvement over current codes. Could you please provide more details about the test setup and datasets etc. ?

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.

3 participants