-
Notifications
You must be signed in to change notification settings - Fork 594
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
Conversation
@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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @tybug!
Closes #4201. My benchmark results:
Master
This PR