Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of st.domains() #4202

Merged
merged 2 commits into from
Dec 19, 2024
Merged

Conversation

tybug
Copy link
Member

@tybug tybug commented Dec 14, 2024

Closes #4201. My benchmark results:

Master

Field     Time:    Compared to: Time difference:
Baseline  0.000182 N/A
Text      0.353401 baseline     0.353219
Domains   3.109082 baseline     3.108901
URLs      3.952818 domains      0.843736
Emails    3.208230 domains      0.099147
Fake      0.065587 domains      -3.043495

This PR

Field     Time:    Compared to: Time difference:
Baseline  0.000170 N/A
Text      0.363252 baseline     0.363082
Domains   1.905849 baseline     1.905679
URLs      2.744583 domains      0.838734
Emails    2.032297 domains      0.126449
Fake      0.067094 domains      -1.838755

Comment on lines 108 to 120
@st.composite
def recase_randomly(draw, tld):
tld = list(tld)
changes = draw(st.tuples(*(st.booleans() for _ in range(len(tld)))))
for i, change_case in enumerate(changes):
if change_case:
tld[i] = tld[i].lower() if tld[i].isupper() else tld[i].upper()
return "".join(tld)

self.domain_strategy = (
st.sampled_from(get_top_level_domains())
Copy link
Member Author

@tybug tybug Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous flatmap dynamically created a lot of uncacheable strategies. st.tuples isn't enormously cacheable in general either, but is decently so when the length is the only thing that can vary.

            .flatmap(
                lambda tld: st.tuples(
                    *(st.sampled_from([c.lower(), c.upper()]) for c in tld)
                ).map("".join)
            )

Most of the performance gain was in moving the strategy definitions to __init__, though changing the flatmap here did have some additional effect as well.

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @tybug!

@Zac-HD Zac-HD merged commit a6a166a into HypothesisWorks:master Dec 19, 2024
49 checks passed
@tybug tybug deleted the domains branch December 19, 2024 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The domain strategy is very slow
3 participants