Repository navigation
Conversation
Return (key,) at once for names with letters only, which cannot contain a decimal digit. For other names, replace the digit parts of the regex split in one slice assignment instead of walking the parts with zip and itertools.cycle. The keys stay exactly the same. Sort keys for all GitHub schema names: 63.70 ms -> 25.62 ms. Co-Authored-By: Aiden <aiden@weco.ai>
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
natural_comparison_keyfaster. Schema sorting, suggestions and value-node sorting use it.(key,)at once. Other names replace the digit parts in one slice.PYTHONPATH=src python -m pytest tests/pyutils/test_natural_compare.py -qgives 5 passed.Refs #310
What & Why
Before, the function split every name with a regex. Then it walked the parts with
zipanditertools.cyclein a generator. Most GraphQL names, likeuserName, have no digits. For them, all this work gave the one-item tuple(name,).Now the function checks
key.isalpha()first and returns(key,)for such names. A name with letters only cannot contain a decimal digit, so the regex split would give the same tuple. Other names still use the regex split. The code replaces the digit parts at the odd positions in one slice assignment, without the generator. Thetype(key) is strcheck keeps the old result forstrsubclasses and the old error for other types.Results
Time to compute the keys for each list of names (CPython 3.12, Linux, best of 7
timeitruns). The underscore and number rows skip the new letters-only check. They show that the check adds no slowdown.userName(5000 names)USER_NAME(5000 names)42(5000 names)field12x3(5000 names)lexicographic_sort_schemaon the GitHub schemaWhy this is safe and easy to maintain
The change is six short lines in one function and removes the
itertoolsimport. The keys are exactly the same as before, including Unicode digits. The existing tests intests/pyutils/test_natural_compare.pyandtests/utilitiespass without changes.Validation
PYTHONPATH=src python -m pytest tests/pyutils/test_natural_compare.py -q: 5 passed.PYTHONPATH=src python -m pytest tests/utilities -q: 663 passed, 16 skipped.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/_QqKTzEBeaYlxdM2E1ve-t47SvlL9Iub