Skip to content

perf(pyutils): speed up LexicalDistance.measure - #325

Open
Torch4084 wants to merge 2 commits into
graphql-python:mainfrom
Torch4084:perf-lexical-distance
Open

Torch4084 wants to merge 2 commits into
graphql-python:mainfrom
Torch4084:perf-lexical-distance

Conversation

@Torch4084

Copy link
Copy Markdown

Addresses #314.

LexicalDistance.measure is called once per option by suggestion_list, so its cost dominates validation of a query with a typo on a large schema. The current implementation does work that is unnecessary for most comparisons:

  • It scores the whole string even when a common prefix or suffix needs no edits.
  • It calls min() over three values in every cell, even when the two characters are equal.
  • It converts the option to a list of code points on every call.
  • It keeps a three-row buffer but indexes it with % 3 on every cell.

This change:

  1. Strips the common prefix and suffix before building the table.
  2. Copies the diagonal cell directly when characters are equal; the other two candidates cannot be smaller in that case.
  3. Indexes the option string directly instead of building a list of code points.
  4. Rotates three row buffers by reference instead of using modulo arithmetic.

The early length check, case-only match, transposition handling, threshold early exit and all return values are unchanged.

Verification:

  • Differential test against the previous implementation: 68,641 string pairs (exhaustive short strings, 40,000 random pairs and 14,000 structured typos) over seven thresholds each, 414,782 comparisons, zero mismatches.
  • Local benchmark, 5 typo queries against 2,000 names, median of 7 runs: 825.73 ms before, 515.14 ms after (1.6x).
  • Full test suite: 3,821 passed, 18 skipped.
  • New focused tests cover shared prefix/suffix, transpositions after trimming, empty and prefix strings, case-only changes and threshold behaviour.

@Torch4084
Torch4084 requested a review from Cito as a code owner November 18, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant