Skip to content

perf(console): avoid call to inspect.stack when possible#1253

Merged
willmcgugan merged 1 commit intoTextualize:masterfrom
P403n1x87:perf-console-log
Jun 9, 2021
Merged

perf(console): avoid call to inspect.stack when possible#1253
willmcgugan merged 1 commit intoTextualize:masterfrom
P403n1x87:perf-console-log

Conversation

@P403n1x87
Copy link
Contributor

@P403n1x87 P403n1x87 commented May 23, 2021

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other (performance)

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

This change improves the performance of the Console.log method by avoiding an expensive call to stack() and using currentframe instead. The same information is then available by navigating backward as many levels as required and peeking at the code object.

@P403n1x87
Copy link
Contributor Author

P403n1x87 commented May 23, 2021

CPU time comparison of the test runs

Before

rich_before

After

rich_after

Copy link
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

Good optimization, but wouldn't want to risk breaking it on other Python implementations. There are a few now.

rich/console.py Outdated
)
path = caller.filename.rpartition(os.sep)[-1]
line_no = caller.lineno
caller = inspect.currentframe()
Copy link
Member

Choose a reason for hiding this comment

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

According to the docs this function may return None if the interpreter isn't CPython. Not sure what supports it and what doesn't (Pypy maybe), but there would need to be a fallback that doesn't break.

@P403n1x87 P403n1x87 requested a review from willmcgugan May 23, 2021 20:54
@codecov
Copy link

codecov bot commented May 23, 2021

Codecov Report

Merging #1253 (efa324a) into master (c2f40e0) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1253   +/-   ##
=======================================
  Coverage   99.82%   99.82%           
=======================================
  Files          69       69           
  Lines        6410     6421   +11     
=======================================
+ Hits         6399     6410   +11     
  Misses         11       11           
Flag Coverage Δ
unittests 99.82% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
rich/console.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ce4f18c...efa324a. Read the comment docs.

@P403n1x87 P403n1x87 changed the title perf(console): avoid call to inspect.stack perf(console): avoid call to inspect.stack when possible May 23, 2021
@P403n1x87 P403n1x87 force-pushed the perf-console-log branch 2 times, most recently from 4f835ce to 88ed70d Compare May 23, 2021 22:38
Copy link
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

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

Some missing lines from coverage. You can do make test to see coverage locally.

@P403n1x87 P403n1x87 requested a review from willmcgugan May 24, 2021 14:14
@P403n1x87 P403n1x87 requested a review from willmcgugan May 24, 2021 16:03
@P403n1x87
Copy link
Contributor Author

@willmcgugan should I make other changes (CHANGELOG/CONTRIBUTORS/...) or is this good to go?

@willmcgugan
Copy link
Member

Definitely changelog. Contributors is up to you, if you would like a credit.

PR looks good, thanks. Give me a little time to give it a proper review...

This change improves the performance of the `Console.log` method by avoiding an expensive call to `stack()` and using `currentframe` instead. The same information is then available by navigating backward as many levels as required and peeking at the `code` object.
@willmcgugan willmcgugan merged commit c924e4e into Textualize:master Jun 9, 2021
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.

2 participants