_connect.py: Don't request context when inspecting stack #2835
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.
This PR partially fixes #2744
This PR makes two changes:
inspect.stack(0)
instead ofinspect.stack()
inspect.stack()
whengetattr(task, "__pw_stack__")
doesn't find a stack in the taskinspect.stack()
by default requests context for the first frame of the callstack, which is a very expensive operation. We don't use the returned context information, so we can safely useinspect.stack(context=0)
.In my testing, this improved the speed of
evaluate()
by about 2.5x:Test:
Before this PR: 10s
After this PR: 4s
The second change is that we originally were calling
getattr(task, "__pw_stack__", inspect.stack())
, which retrieved the entire callstack and then threw it away ifgetattr(task, "__pw_stack__")
succeeded. I wasn't able to figure out how to getgetattr(task, "__pw_stack__")
to return a value, so I'm not sure how much this change sped things up, but it seems like a sensible change to make.I've added async and sync tests.
I have a more detailed fix, but I figured I'd first try to get this PR landed, since it involves small changes for a pretty large speedup :)