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

cache extract_lambda_source #3966

Merged
merged 2 commits into from
May 3, 2024

Conversation

tybug
Copy link
Member

@tybug tybug commented May 3, 2024

Addresses the performance part of #3963. Have not profiled memory. It should also be significantly reduced as we're calling ast.parse less now, but this will not address any underlying memory leak, if one exists.

@tybug tybug force-pushed the cache-extract-lambda-source branch from 81852e2 to cc1aee8 Compare May 3, 2024 03:22
@jobh
Copy link
Contributor

jobh commented May 3, 2024

👍

Some general comments, for consideration.

  • The value of maxsize seems sensible enough, but there is a risk of the lambdas being memory heavy by pinning big objects (through their closures). We could consider a WeakKeyDictionary, or even invasively storing the source on the lambda itself.
  • repr(f) /"{self!r}" can potentially also be expensive, so much so that we have special checks for especially bad ones (see repr_call; I don't know if that is likely to apply to functions though). Elsewhere, we defer the repr by storing the format string plus arguments, not interpolating the string until it's requested. Is that a possibility here? I'm not familiar with the observability interface, so I don't know.

[edit to clarify: By "here", I mean at the call site in stateful.py]

@Zac-HD
Copy link
Member

Zac-HD commented May 3, 2024

  • The value of maxsize seems sensible enough, but there is a risk of the lambdas being memory heavy by pinning big objects (through their closures). We could consider a WeakKeyDictionary, or even invasively storing the source on the lambda itself.

I like the idea of using WeakKeyDictionary; it'd mean we get an unlimited-size cache with negligible memory overhead.

  • repr(f) /"{self!r}" can potentially also be expensive, so much so that we have special checks for especially bad ones (see repr_call; I don't know if that is likely to apply to functions though). Elsewhere, we defer the repr by storing the format string plus arguments, not interpolating the string until it's requested. Is that a possibility [in stateful.py]?

The contrasting concern is that if the arguments are mutated, the deferred reprs end up actively misleading. Given the nature of stateful tests, I think we should probably continue to repr in advance until we get a complaint or see a profile which shows that repr performance is a problem here in practice.

@Zac-HD
Copy link
Member

Zac-HD commented May 3, 2024

Coverage issue is tracked in #3968, I'll aim to get that over the weekend if nobody beats me to it.

@tybug
Copy link
Member Author

tybug commented May 3, 2024

Good call with WeakKeyDictionary, thanks!

@tybug tybug force-pushed the cache-extract-lambda-source branch 2 times, most recently from 6ddf158 to cebe6cb Compare May 3, 2024 19:33
@tybug tybug force-pushed the cache-extract-lambda-source branch from cebe6cb to dfb720e Compare May 3, 2024 21:17
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.

🚀

@Zac-HD Zac-HD merged commit 0a17774 into HypothesisWorks:master May 3, 2024
54 checks passed
@tybug tybug deleted the cache-extract-lambda-source branch May 3, 2024 23:58
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.

None yet

3 participants