Skip to content

ref: replace inspect.stack() with a more performant iteration of the current stack - #63965

Merged
asottile-sentry merged 1 commit into
masterfrom
asottile-replace-inspect-stack
Jan 26, 2024
Merged

ref: replace inspect.stack() with a more performant iteration of the current stack#63965
asottile-sentry merged 1 commit into
masterfrom
asottile-replace-inspect-stack

Conversation

@asottile-sentry

Copy link
Copy Markdown
Contributor

inspect.stack() in python 3.11 is about 15x slower than in 3.10

the new function I made is even faster than inspect.stack even in 3.10

little benchmark scripty
Details
import sys
import inspect
import time
from typing import Generator


def _current_stack_filenames() -> Generator[str, None, None]:
    f = sys._getframe()
    while f is not None:
        yield f.f_code.co_filename
        f = f.f_back


def _current_stack_filenames_inspect() -> Generator[str, None, None]:
    for _, filename, _, _, _, _ in inspect.stack():
        yield filename


def j():
    l1 = list(_current_stack_filenames())
    l2 = list(_current_stack_filenames_inspect())
    assert l1 == l2, (l1, l2)

    t1 = time.monotonic() + 5
    i = 0
    while time.monotonic() < t1:
        tuple(_current_stack_filenames())
        i += 1

    print(f'new: {i}')

    t1 = time.monotonic() + 5
    i = 0
    while time.monotonic() < t1:
        tuple(_current_stack_filenames_inspect())
        i += 1

    print(f'old: {i}')



def i():
    j()


def h():
    i()


def g():
    h()


def f():
    g()


def test():
    f()


def main():
    f()


if __name__ == '__main__':
    raise SystemExit(main())
$ python3.10 t6.py 
new: 6267307
old: 51673
$ : 121x speedup in 3.10
$ python3.11 t6.py 
new: 7993958
old: 44539
$ : 179x speedup in 3.11

@asottile-sentry
asottile-sentry requested a review from a team January 26, 2024 18:45
@asottile-sentry
asottile-sentry requested a review from a team as a code owner January 26, 2024 18:45
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 26, 2024
@mwarkentin

Copy link
Copy Markdown
Member

@codecov

codecov Bot commented Jan 26, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4e55b40) 81.39% compared to head (ea711ea) 81.40%.
Report is 33 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #63965   +/-   ##
=======================================
  Coverage   81.39%   81.40%           
=======================================
  Files        5228     5228           
  Lines      232470   232509   +39     
  Branches    40253    40254    +1     
=======================================
+ Hits       189222   189264   +42     
- Misses      37396    37397    +1     
+ Partials     5852     5848    -4     
Files Coverage Δ
src/sentry/options/defaults.py 100.00% <100.00%> (ø)
src/sentry/relay/globalconfig.py 88.88% <ø> (ø)
src/sentry/utils/sdk.py 71.18% <100.00%> (+0.61%) ⬆️

... and 19 files with indirect coverage changes

@joshuarli joshuarli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👀

@asottile-sentry
asottile-sentry merged commit 9f545b6 into master Jan 26, 2024
@asottile-sentry
asottile-sentry deleted the asottile-replace-inspect-stack branch January 26, 2024 21:08
asottile-sentry added a commit that referenced this pull request Jan 29, 2024
blocked on #63965

This reverts commit 45d6647.

<!-- Describe your PR here. -->
snigdhas pushed a commit that referenced this pull request Jan 30, 2024
blocked on #63965

This reverts commit 45d6647.

<!-- Describe your PR here. -->
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants