Repository navigation
Conversation
Compute the final distance from the bit vectors and reduce work in the shared character loop. Keep string equality and sequence ordering checks limited to plain strings. Co-Authored-By: Aiden <aiden@weco.ai>
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.
TL;DR
_uniform_distance, the Python Levenshtein path for unit edit costs.What & Why
The old loop updated the distance after every character, although callers only need the final value. The new code sums the stored row differences once to get that value.
Plain strings also return early when equal and put the shorter string in the character loop. These changes preserve the edit-distance formula and keep the computation shared across input types.
Results
CPython 3.12.13 on Linux x86_64, with one isolated CPU core. The table uses random ASCII strings, seed 18, and 1,000 calls per length. Each value is the mean of two runs; each run uses the best of seven repeats from
bench.common.benchmark.Validation
The batch result above uses the median times from three runs. It combines Python Levenshtein distance and normalized similarity on 50 Unicode pairs, Indel controls, and sequence boundary cases. All returned values and exceptions matched the original version.
Native test command:
Black 25.1.0 and Ruff 0.11.5 pass with the repository configuration.
To reproduce the size checks, install
packagingandtqdm, then run this on each checkout. It prints seconds per call.Why this is safe and easy to maintain
The change stays inside one function and keeps one readable distance loop. Public results, cutoffs, weights, and exceptions are unchanged. Hypothesis and Levenshtein tests cover Unicode, integer sequences, and empty inputs. Non-string inputs allocate a temporary mask list to preserve the number of items actually consumed.
AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/IBeNYNHxcI8YFj4EM3nBE_bPTn9T6oZz