Repository navigation
fix: reject M < 2 at compile time (closes #32) - #35
Merged
danielhuici merged 3 commits intoSep 1, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hnsw::random_level()computes the insertion level with-ln(uniform) / ln(M). WithM=1,ln(1) = 0, so the division producesf64::INFINITY, which casts tousize::MAXinstead of panicking. The firstinsert()then callsinitialize(usize::MAX), whose loop tries to allocate that many layers, consuming all available memory until the process is killed.M=1is a valid value at the type level, nothing rejected it before construction.Changes
src/controllers/hnsw.rs:Hnsw::new()now has a compile-timeconst { assert!(M >= 2, ...) }.M=1(orM=0) no longer compiles, with an error pointing at the exactHnsw::new()instantiation that violates it.Apotheosis::new()callsHnsw::new()internally, so this covers both entry points with a single guard.Confirmed with Ricardo that the correct boundary is
M > 1(equivalentlyM >= 2):M=0does not diverge (insertion_levelcomes out0, a different and unrelated correctness issue with zero-capacity neighbor arrays, not covered by this fix), onlyM=1produces theln(M)=0divergence.Test plan
Verified locally with the same commands CI runs:
cargo clippy --all-targets --all-features -- -D warningsclean,cargo fmt --checkclean, and all 44 tests passing with the defaultM.Also verified both directions manually: a scratch binary instantiating
Apotheosis<_, _, 1, 2, 10>failed to compile with the assert's message, pointing at theHnsw::new()call site; a normal build with default generics (M=16) compiled and ran unaffected.