Repository navigation
refactor: keep only the best k candidates instead of sorting every one - #3
Merged
Merged
Conversation
Recall sorted every vector in the namespace and every keyword hit to take the top Candidates. A bounded heap keeps the same set and order (ties are still broken by document number) in O(n log k). Adds BenchmarkRecall and a CI step that keeps it compiling and running.
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.
Change
Recallpicked its candidates by sorting everything:memory.candidatescomputed the cosine of every live vector in the namespace, put all of them in a slice andsort.Sliced it, to keep the topCandidates(default 50).index.Searchsorted every keyword hit to return the topn.Both now stream into a bounded heap (
internal/topk, about 80 lines, generic) that keeps only the best k: O(N log k) instead of O(N log N), and the semantic path no longer allocates an N-sized slice. The ordering is unchanged (score descending, ties by document number), so the selected set and order are identical.Also adds
BenchmarkRecall(1k / 10k / 50k memories, 64-dim embeddings, text + vector query) and a CI step that runs it once (-benchtime=1x) so it keeps compiling.Measured (Apple M2 Pro, go1.26.9,
-count=5, medians via benchstat)Command (same benchmark file on both sides; "before" is origin/main with only
bench_test.goadded):Memory per recall: 3.871 MiB -> 2.341 MiB at 50k (-39.5%); 173.2 KiB -> 142.8 KiB at 1k. All p=0.008, n=5.
Raw output:
Tests
internal/topk: heap result equals a full sort for n in {0,1,5,50,1000}, k in {0,1,5,50,2000}, with many ties.internal/index:TestSearchLimitIsPrefixOfFullRanking,Search(n)equals the first n of the full ranking, on equal-score data.Existing recall and ranking tests unchanged and passing.
go vet ./... && go test -race -count=1 ./...
ok github.com/shaikn6/lethe/cmd/lethe-mcp
ok github.com/shaikn6/lethe/internal/events
ok github.com/shaikn6/lethe/internal/index
ok github.com/shaikn6/lethe/internal/memory
ok github.com/shaikn6/lethe/internal/policy
ok github.com/shaikn6/lethe/internal/server
ok github.com/shaikn6/lethe/internal/topk
Scope and caveats
Rollback: revert the single commit.