Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

_connect.py: Don't request context when inspecting stack #2835

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

Merged
merged 6 commits into from
Apr 30, 2025

Conversation

neoncube2
Copy link
Contributor

@neoncube2 neoncube2 commented Apr 29, 2025

This PR partially fixes #2744

This PR makes two changes:

  1. Call inspect.stack(0) instead of inspect.stack()
  2. Only call inspect.stack() when getattr(task, "__pw_stack__") doesn't find a stack in the task

inspect.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 use inspect.stack(context=0).

In my testing, this improved the speed of evaluate() by about 2.5x:

Test:

async def test_speed_test(page: Page) -> None:
    start_time = time.time()

    for i in range(0, 1000):
        await page.evaluate('() => 1 + 1')

    print(time.time() - start_time)

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 if getattr(task, "__pw_stack__") succeeded. I wasn't able to figure out how to get getattr(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 :)

@neoncube2
Copy link
Contributor Author

@microsoft-github-policy-service agree

@neoncube2
Copy link
Contributor Author

I have a feeling that this might noticeably improve the runtime of the test suite in tests/, too! :D

@neoncube2
Copy link
Contributor Author

Ready to have the workflows rerun :)

Copy link
Member

@mxschmitt mxschmitt left a comment

Choose a reason for hiding this comment

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

Looks reasonable, thank you for the contribution!

@mxschmitt mxschmitt merged commit fe886f7 into microsoft:main Apr 30, 2025
37 of 38 checks passed
@neoncube2
Copy link
Contributor Author

Yay, thank you! :D

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.

[Feature]: Option to disable inspect.stack() calls for performance optimization
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.