MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / random_layer

Method random_layer

nodedb-vector/src/hnsw/graph/index.rs:348–353  ·  view source on GitHub ↗

Assign a random layer using the exponential distribution. Capped at `MAX_LAYER_CAP` to prevent pathological RNG draws from promoting the index's `max_layer` to hundreds or thousands, which would make every search's Phase-1 greedy descent O(max_layer).

(&mut self)

Source from the content-addressed store, hash-verified

346 /// promoting the index's `max_layer` to hundreds or thousands, which
347 /// would make every search's Phase-1 greedy descent O(max_layer).
348 pub(crate) fn random_layer(&mut self) -> usize {
349 let ml = 1.0 / (self.params.m as f64).ln();
350 let r = self.rng.next_f64().max(f64::MIN_POSITIVE);
351 let layer = (-r.ln() * ml).floor() as usize;
352 layer.min(MAX_LAYER_CAP)
353 }
354
355 /// Compute distance between a pre-encoded query and a stored node.
356 ///

Callers 1

insertMethod · 0.45

Calls 1

next_f64Method · 0.45

Tested by

no test coverage detected