You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Speed up LexicalDistance.measure used for "Did you mean" suggestions #314
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.
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.
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.
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.
Activity
Cito commentedon Nov 15, 2026
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=Trueskips them entirely. GraphQL.js added that option as its answer to a similar report (graphql/graphql-js#4190).Our
LexicalDistancemirrorssuggestionList.tsin 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.dexhunter commentedon Nov 15, 2026
The timings in this issue cover the combined patch against
1d505c1. They do not isolate the equal-character change or show its benefit after0c26e582.I checked the diff for
0c26e582. It replaces the row-minimummin()call with a comparison insrc/graphql/pyutils/suggestion_list.py, withinLexicalDistance.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.