⚡ perf(link): skip fragment regex when fragment marker absent#13987
Open
gaborbernat wants to merge 1 commit into
Open
⚡ perf(link): skip fragment regex when fragment marker absent#13987gaborbernat wants to merge 1 commit into
gaborbernat wants to merge 1 commit into
Conversation
Every `Link` runs `_egg_fragment_re.search` and `_subdirectory_fragment_re.search` against `self._url` to find the matching fragment, even though Warehouse-served URLs (the bulk of links a Simple-API response carries) never include either fragment. Each regex search compiles a Boyer-Moore state and walks the full URL, which adds up across the ~65000 links a moderately sized cross-platform lock iterates per resolver pass. Guard each search with a literal `"egg=" not in self._url` (resp. `"subdirectory=" not in self._url`) pre-check. Python's substring search is C-implemented and runs an order of magnitude faster than the regex when the marker is absent; when the marker is present the existing regex still runs, so behaviour is preserved for VCS / direct-URL / `find-links` style URLs that carry it. A new `test_fragments_absent_for_typical_links` parametrize asserts both accessors return `None` for a representative spread of Simple-API URLs. The existing `test_fragments` and `test_invalid_egg_fragments` cases all carry explicit `#egg=…` / `&subdirectory=…` fragments and continue to exercise the slow path.
98aca10 to
8ba2dbe
Compare
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.
Every
Linkruns_egg_fragment_re.searchand_subdirectory_fragment_re.searchagainstself._urlto find the matching fragment, even though Warehouse-served URLs (the bulk of links a Simple-API response carries) never include either fragment. Each regex search walks the full URL, which adds up across the ~65000 links a moderately sized cross-platform lock iterates per resolver pass. ⚡The patch guards each search with a literal
"egg=" not in self._url(resp."subdirectory=" not in self._url) pre-check. Python's substring search is C-implemented and runs roughly 8x faster than the regex when the marker is absent; when the marker is present the existing regex still runs, so behaviour is preserved byte-for-byte for VCS / direct-URL /find-linksstyle URLs that carry it.Function-level micro-bench against
https://files.pythonhosted.org/packages/12/34/foo-1.0-py3-none-any.whl#sha256=…(noegg=): the egg accessor drops from 535 ns/call to 69 ns/call, a 7.8x speedup. End-to-end against a cross-platform lock pipeline iterating ~65000 links across 8 resolver passes (n=8 paired runs alternatingupstream/mainvsupstream/main + this patch): user-CPU mean falls 2.8% (median 55 ms reduction), 7/8 paired runs faster.