perf: lazily wrap elements in find_by_text/find_by_regex so first_match short-circuits#370
Merged
Merged
Conversation
Owner
|
It seems like you are studying the code to find stuff like this ahaha. Nice work, mate! |
Contributor
Author
Haha, yes, I have been studying!! Thank You! |
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.
Problem
find_by_text/find_by_regexdefault tofirst_match=Truebut callself.__elements_convertor(possible_targets)first, which eagerly wraps all Nelements into
Selectors before the loop. Sobreaksaves nothing: wrapping isO(N) instead of O(k), where k is the first match's index.
Fix
Lazy wrapping, one element at a time. Singular
__element_convertorbuilds eachSelectorwith identical kwargs, so output is unchanged; only the wrapping becomeslazy. Nothing after the loop needs
possible_targetsto be aSelectors.Result
Wrapping on
first_match=Truedrops from O(N) to O(k). Output-identical(early/last/no-match, both methods), existing suite green (94 passed).
Benchmark, minimum of 50 runs, page of N=5000 elements, times in milliseconds:
first_match=Truefind_by_textmatch at index 5 (early)find_by_textmatch at last index 4999find_by_textno match (full scan)find_by_regexmatch at index 5 (early)find_by_regexmatch at last index 4999find_by_regexno match (full scan)Early match is ~1.9x faster; last-match and no-match stay within noise (never slower).