Skip to content

Speed up LexicalDistance.measure used for "Did you mean" suggestions #314

Open

Description

@dexhunter

Problem

suggestion_list() calls LexicalDistance.measure() once for each option. When a query has a typo, validation uses this to build "Did you mean ...?" hints. On large schemas, this distance code takes most of the validation time.

The method does extra work for each option:

  • It builds the full distance table even when a shared prefix or suffix needs no edits.
  • It builds the table even when the option has more missing characters than the threshold allows.
  • It calls min() with 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.

Numbers

Python 3.12 on Linux. The Before column is the current main, and the After column is the proposed change.

Situation Before After Change
tests/benchmarks/test_validate_invalid_gql.py (median) 22.49 ms 11.42 ms 49% faster
suggestion_list, 12-char typo, 2000 random names 27.04 ms 2.69 ms 90% faster
suggestion_list, 4-char typo, 2000 random names 2.28 ms 0.61 ms 73% faster
suggestion_list, 30-char input, 2000 random names 0.70 ms 0.30 ms 58% faster
Exact or case-only matches, 2000 options 0.170 ms 0.179 ms about the same (early return, code unchanged)

Proposed fix

Skip the common prefix and suffix, and return early when too many characters are missing. In the table, copy the diagonal cell for equal characters, and rotate the row buffers.

Environment

graphql-core main (1d505c1), CPython 3.12, Linux x86_64.

Activity

  1. Cito commented on Nov 15, 2026

    @Cito
    Member

    Thanks for the detailed numbers. This code only runs when a request contains an unknown name (a field, argument, type, enum value or input field), so valid requests never reach it, and the gain shows up mainly in the invalid query benchmark. If the cost of these suggestions matters in production, hide_suggestions=True skips them entirely. GraphQL.js added that option as its answer to a similar report (graphql/graphql-js#4190).

    Our LexicalDistance mirrors suggestionList.ts in GraphQL.js line by line, and I would like to keep it that way so that upstream changes stay easy to port. Skipping the common prefix and suffix and the additional early return change the algorithm itself, so they should go to GraphQL.js first. If you think they are worth it, please propose them there; once they land, they reach GraphQL-core through the normal porting.

    While checking this I found one place where our port had drifted from GraphQL.js: it called min() to track the smallest cell of a row, where GraphQL.js uses a plain comparison. That is fixed on main now (0c26e58) and makes the invalid query benchmark about 19 % faster on CPython 3.12 and 6 % faster on 3.14.

    One more part is specific to Python and could come here directly: when the two characters are equal, taking the diagonal cell instead of calling min(). That keeps the structure of the upstream loop. In my measurements it saves about 5 % on that benchmark. If you want to open a PR with only that change, please include benchmarks on PyPy as well as CPython, since a change that helps on CPython can be neutral or slower under the PyPy JIT.

  2. dexhunter commented on Nov 15, 2026

    @dexhunter
    ContributorAuthor

    The timings in this issue cover the combined patch against 1d505c1. They do not isolate the equal-character change or show its benefit after 0c26e582.

    I checked the diff for 0c26e582. It replaces the row-minimum min() call with a comparison in src/graphql/pyutils/suggestion_list.py, within LexicalDistance.measure. I have not rerun benchmarks against that commit or measured the smaller change on PyPy.

    The prefix/suffix trimming and extra early return need GraphQL.js review first. A Python-only patch would copy the diagonal cell for equal characters and keep the rest of the upstream loop. That needs separate CPython and PyPy results; the combined patch's timings cannot support it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions