Skip to content

Improve as_traceback performance #81

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 3 commits into from
Mar 31, 2025
Merged

Conversation

wengh
Copy link
Contributor

@wengh wengh commented Mar 25, 2025

This PR improves Traceback.as_traceback() performance by calling compile only once. This leads to ~50% speedup for long tracebacks.

Instead of setting the line number by adding blank lines, we now do it by changing co_firstlineno. This change also stops pathological input (e.g. File "tests/examples.py", line 2147483647, in func_a) from causing slowdown or blowing up memory.

@ionelmc
Copy link
Owner

ionelmc commented Mar 25, 2025

You could also remove the branch were CodeType doesn't have a replace method (it's available since python 3.8).

@wengh
Copy link
Contributor Author

wengh commented Mar 27, 2025

You could also remove the branch were CodeType doesn't have a replace method (it's available since python 3.8).

@ionelmc
done, please take a look!

@ionelmc ionelmc merged commit 10413a2 into ionelmc:master Mar 31, 2025
17 of 27 checks passed
@ionelmc
Copy link
Owner

ionelmc commented Mar 31, 2025

Thank you.

@ionelmc
Copy link
Owner

ionelmc commented Mar 31, 2025

Released as 3.1.0.

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