Repository navigation
gh-161242: Add a freelist for traceback objects - #161243
Open
eendebakpt wants to merge 2 commits into
Open
eendebakpt wants to merge 2 commits into
eendebakpt wants to merge 2 commits into
Conversation
A traceback object is created for every raised exception. Keep up to 8 of them on a per-thread freelist, and replace the argument check in tb_create_raw() with asserts since all callers validate their arguments. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
eendebakpt
requested review from
ZeroIntensity and
ericsnowcurrently
as code owners
November 3, 2026 09:04
sobolevn
reviewed
Nov 3, 2026
| PyTracebackObject *tb; | ||
| if ((next != NULL && !PyTraceBack_Check(next)) || | ||
| frame == NULL || !PyFrame_Check(frame)) { | ||
| PyErr_BadInternalCall(); |
Member
There was a problem hiding this comment.
Is it safe to remove this call? tb_create_raw is used in several C-API calls that can in theory reach this point.
Contributor
Author
There was a problem hiding this comment.
All callers have their own guards: tb_new_impl is checked via AC, _PyTraceBack_FromFrame checks with asserts as well (callers of _PyTraceBack_FromFrame do perform actual checks, _PyTraceBack_FromFrame is internal api). Maybe I missed a path?
picnixz
reviewed
Nov 3, 2026
eendebakpt
commented
Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A traceback object is created for every exception that is raised. This PR adds a freelist for traceback objects and removes some redundant checks.
raise ValueError/ exceptraise ValueError(x)/ exceptd[key]KeyErrorobj.xAttributeErrorException-heavy code in the style of real programs:
chameleonA freelist for the exception instances themselves is possible as well, but needs more care (allocator slots on the static exception types) and is left for a separate PR.
Micro-benchmark script (bench_exc_pyperf.py)
Macro-benchmark script (bench_exc_macro.py)
Many thanks to Claude Code