Repository navigation
Speed up LevenshteinDistance and spellcheck corrections - #798
Open
TheNetherWatcher wants to merge 2 commits into
Open
TheNetherWatcher wants to merge 2 commits into
TheNetherWatcher wants to merge 2 commits into
Conversation
The non-search, non-Damerau path built a full matrix of cell objects and picked the cheapest parent with _.min for every cell, even though plain LevenshteinDistance only returns the final cost. Use two rows of numbers instead. Search, Damerau and non-string or non-numeric-cost inputs keep the original path, so results are unchanged.
edits() removed duplicates with filter + indexOf, which is quadratic in the number of candidate edits, and editsWithMaxDistanceHelper rebuilt its array with concat for every word. Use a Set (keeps first-occurrence order) and push. Returned corrections and their order are unchanged.
TheNetherWatcher
force-pushed
the
perf/levenshtein
branch
from
November 4, 2026 08:28
61e8142 to
d92f3fe
Compare
1 task done
This branch has not been deployed
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.
LevenshteinDistanceandSpellcheck.getCorrectionsspend most of their time on bookkeeping that doesn't affect the result. This PR removes it; outputs (including the order of corrections) are unchanged.Levenshtein distance: the plain path (no
search, nodamerau) built a full matrix where every cell allocated a few objects and picked the cheapest parent with_.min. Only the final cost is returned, so two rows of numbers are enough. Search mode, Damerau variants, and non-string inputs or non-numeric costs still go through the original code.Spellcheck:
edits()deduplicated withfilter+indexOf(quadratic in the number of candidate edits), andeditsWithMaxDistanceHelperrebuilt its array withconcatfor each word. It now uses aSet, which keeps first-occurrence order, andpush.Benchmarks
Node 24, Apple M4 Pro. Old and new code compared side by side, with outputs checked equal on 5,000 random distance pairs (mixed costs, including negative and fractional ones), plus search/Damerau pairs and spellcheck expansions. Dictionary: 4,000 words from
/usr/share/dict/words.Tests
editsWithMaxDistance.npm test: 904 specs, 0 failures.Proof
There's a Lean 4 proof that the change preserves output: lean-perf-proofs/natural-levenshtein. It covers:
filter+indexOf.concat.It uses Lean core only, has no
sorry, and relies on no extra axioms.