Skip to content

Commit ec293bc

Browse files
ezimuelCopilot
andauthored
Update src/HNSW/Index.php
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent dec24ad commit ec293bc

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/HNSW/Index.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,17 @@ public function insert(Document $document): void
196196
// beam width, ramping up to the configured maximum as the index grows.
197197
// This avoids wasting cycles searching a sparse graph while preserving
198198
// full recall quality at scale.
199-
$efC = max($this->config->M, min($this->config->efConstruction, (int) ($nodeId / 10)));
200-
201199
for ($lc = min($this->maxLayer, $maxLayer); $lc >= 0; $lc--) {
202200
$mMax = $lc === 0 ? $this->config->M0 : $this->config->M;
203201

202+
// Compute adaptive efConstruction per-layer, ensuring it is at least
203+
// the maximum desired degree for this layer (mMax). This guarantees
204+
// that searchLayer() can return enough candidates to fill M0 on the
205+
// base layer and M on upper layers, even during early inserts.
206+
$efC = max(
207+
$mMax,
208+
min($this->config->efConstruction, (int) ($nodeId / 10))
209+
);
204210
// Find efConstruction nearest neighbours at this layer.
205211
$W = $this->searchLayer($dv, [[$epDist, $ep]], $efC, $lc);
206212

0 commit comments

Comments
 (0)