From 22b0007796629179d02bac516d2fd9b8f2f02ed0 Mon Sep 17 00:00:00 2001 From: Longwei Liu Date: Thu, 19 Feb 2026 22:44:01 +0800 Subject: [PATCH] perf(flat): skip IndexDocument construction in heap emplace when score 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. --- src/include/zvec/core/framework/index_document.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/include/zvec/core/framework/index_document.h b/src/include/zvec/core/framework/index_document.h index aa74e606..b3dc5826 100644 --- a/src/include/zvec/core/framework/index_document.h +++ b/src/include/zvec/core/framework/index_document.h @@ -248,14 +248,16 @@ class IndexDocumentHeap : public ailego::Heap { //! Insert a document into the heap void emplace(uint64_t key, float score) { - if (score <= threshold_) { + if (score <= threshold_ && + (!this->full() || score < this->front().score())) { ailego::Heap::emplace(key, score); } } //! Insert a document into the heap void emplace(uint64_t key, float score, uint32_t index) { - if (score <= threshold_) { + if (score <= threshold_ && + (!this->full() || score < this->front().score())) { ailego::Heap::emplace(key, score, index); } }