René's URL Explorer Experiment
Title: gh-161242: Add a freelist for traceback objects by eendebakpt · Pull Request #161243 · python/cpython · GitHub
Open Graph Title: gh-161242: Add a freelist for traceback objects by eendebakpt · Pull Request #161243 · python/cpython
X Title: gh-161242: Add a freelist for traceback objects by eendebakpt · Pull Request #161243 · python/cpython
Description: A traceback object is created for every exception that is raised. This PR adds a freelist for traceback objects and removes some redundant checks.
benchmark
main
this PR
raise ValueError / except
454 ns
418 ns (1.09x faster)
raise ValueError(x) / except
495 ns
476 ns (1.04x faster)
raise through one frame
792 ns
764 ns (1.04x faster)
d[key] KeyError
630 ns
604 ns (1.04x faster)
obj.x AttributeError
1.53 us
1.45 us (1.05x faster)
Exception-heavy code in the style of real programs:
benchmark
main
this PR
parse mixed column (int/float/str)
953 us
938 us (1.02x faster)
word count via KeyError
411 us
403 us (1.02x faster)
template variable resolve
368 us
356 us (1.03x faster)
duck typing AttributeError
464 us
439 us (1.06x faster)
pyperformance chameleon
7.04 ms
6.86 ms (1.03x faster)
A 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)
"""pyperf micro-benchmarks for raising and catching exceptions.
Usage: python bench_exc.py [pyperf args...]
"""
import sys
import pyperf
def keyerror(d, key, fallback):
try:
return d[key]
except KeyError:
return fallback(key, d)
def attrerror(obj, name, default):
try:
return obj.x
except AttributeError:
return default + name
def valueerror_raise(x):
try:
raise ValueError
except ValueError:
return x
def valueerror_raise_inst(x):
try:
raise ValueError(x)
except ValueError:
return x
def stopiter(it):
try:
return next(it)
except StopIteration:
return it
def through_frame(x):
try:
raiser(x)
except ValueError:
return x
def raiser(x):
raise ValueError(x)
class NoX:
pass
def make_bench(fn, *args):
def bench(loops):
r = range(loops)
t0 = pyperf.perf_counter()
for _ in r:
fn(*args); fn(*args); fn(*args); fn(*args)
return pyperf.perf_counter() - t0
return bench
if __name__ == "__main__":
a = float(len(sys.argv)) * 2.5
runner = pyperf.Runner()
runner.bench_time_func("raise ValueError", make_bench(valueerror_raise, a))
runner.bench_time_func("raise ValueError(x)", make_bench(valueerror_raise_inst, a))
runner.bench_time_func("raise through frame", make_bench(through_frame, a))
runner.bench_time_func("d[key] KeyError", make_bench(keyerror, {}, a, lambda k, d: k))
runner.bench_time_func("next() StopIteration", make_bench(stopiter, iter(())))
runner.bench_time_func("obj.x AttributeError", make_bench(attrerror, NoX(), "n" * 2, "d" * 2))
Macro-benchmark script (bench_exc_macro.py)
"""Exception-heavy workloads in the style of real code, for pyperf.
Each benchmark raises and catches exceptions through Python frames as a
side effect of ordinary work, which is the case the traceback and
exception freelists target. Usage: python bench_exc_macro.py [pyperf args]
"""
import random
import pyperf
random.seed(12345)
# 1. Data cleaning: parse a column of mixed strings with try/except fallbacks
# (int -> float -> keep string), the usual idiom for CSV cleanup.
_RAW_COLUMN = []
for _i in range(2000):
_r = random.random()
if _r < 0.5:
_RAW_COLUMN.append(str(random.randint(-10**6, 10**6)))
elif _r < 0.8:
_RAW_COLUMN.append(repr(random.uniform(-1e6, 1e6)))
else:
_RAW_COLUMN.append(random.choice(["n/a", "", "NULL", "12,5", "1e", "x"]))
def to_number(s):
try:
return int(s)
except ValueError:
pass
try:
return float(s)
except ValueError:
return s
def bench_parse_column(loops):
column = _RAW_COLUMN
t0 = pyperf.perf_counter()
for _ in range(loops):
total = 0
for s in column:
v = to_number(s)
if type(v) is not str:
total += 1
return pyperf.perf_counter() - t0
# 2. Counting with the classic try/except KeyError idiom on a text with a
# long tail of rare words (every first occurrence raises).
_WORDS = ([f"w{i}" for i in range(50) for _ in range(20)]
+ [f"rare{i}" for i in range(3000)])
random.shuffle(_WORDS)
def bench_count_keyerror(loops):
words = _WORDS
t0 = pyperf.perf_counter()
for _ in range(loops):
counts = {}
for w in words:
try:
counts[w] += 1
except KeyError:
counts[w] = 1
return pyperf.perf_counter() - t0
# 3. Template-engine style variable resolution: try a dict lookup, then an
# attribute, then an index, like Django's Variable._resolve_lookup().
class _Ctx:
def __init__(self, **kw):
self.__dict__.update(kw)
def resolve(obj, bit):
try:
return obj[bit]
except (TypeError, KeyError, IndexError):
pass
try:
return getattr(obj, bit)
except AttributeError:
pass
try:
return obj[int(bit)]
except (ValueError, TypeError, IndexError, KeyError):
raise LookupError(bit)
_CONTEXT = {"user": _Ctx(name="Ada", tags=["a", "b", "c"], profile={"city": "London"}),
"items": [_Ctx(title=f"t{i}", price=i) for i in range(20)]}
_LOOKUPS = ["user.name", "user.tags.1", "user.profile.city", "items.3.title",
"items.7.price", "user.missing", "items.25.title"]
def render_var(context, path):
obj = context
for bit in path.split("."):
try:
obj = resolve(obj, bit)
except LookupError:
return ""
return obj
def bench_template_resolve(loops):
ctx = _CONTEXT
lookups = _LOOKUPS
t0 = pyperf.perf_counter()
for _ in range(loops):
for _r in range(50):
for p in lookups:
render_var(ctx, p)
return pyperf.perf_counter() - t0
# 4. Duck typing: optional protocol methods probed with try/except
# AttributeError in Python code, over a mix of object kinds.
class _WithClose:
def close(self):
return 1
class _WithFlush:
def flush(self):
return 2
class _Plain:
pass
_OBJECTS = [random.choice([_WithClose, _WithFlush, _Plain])() for _ in range(1000)]
def finish(obj):
n = 0
try:
obj.flush()
n += 1
except AttributeError:
pass
try:
obj.close()
n += 1
except AttributeError:
pass
return n
def bench_duck_typing(loops):
objs = _OBJECTS
t0 = pyperf.perf_counter()
for _ in range(loops):
for o in objs:
finish(o)
return pyperf.perf_counter() - t0
# 5. Python-level iterator classes: StopIteration raised from __next__
# through a Python frame once per (short) iteration.
class Countdown:
def __init__(self, n):
self.n = n
def __iter__(self):
return self
def __next__(self):
if self.n <= 0:
raise StopIteration
self.n -= 1
return self.n
def bench_iterator_class(loops):
t0 = pyperf.perf_counter()
for _ in range(loops):
for _r in range(500):
for _x in Countdown(3):
pass
return pyperf.perf_counter() - t0
if __name__ == "__main__":
runner = pyperf.Runner()
runner.bench_time_func("parse mixed column (int/float/str)", bench_parse_column)
runner.bench_time_func("word count via KeyError", bench_count_keyerror)
runner.bench_time_func("template variable resolve", bench_template_resolve)
runner.bench_time_func("duck typing AttributeError", bench_duck_typing)
runner.bench_time_func("iterator class StopIteration", bench_iterator_class)
Many thanks to Claude Code
Issue: gh-161242
Open Graph Description: A traceback object is created for every exception that is raised. This PR adds a freelist for traceback objects and removes some redundant checks.
benchmark
main
this PR
raise ValueError / e...
X Description: A traceback object is created for every exception that is raised. This PR adds a freelist for traceback objects and removes some redundant checks.
benchmark
main
this PR
raise ValueError / e...
Opengraph URL: https://github.com/python/cpython/pull/161243
X: @github
direct link
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:a46f9a57-fd4f-0c3a-69f0-543512ce9d62 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AFB4:2270F4:3772A89:4983511:6B020A95 |
| html-safe-nonce | a250f364c34b40a0838bcd07b50f57389f352aa9060688847fd9468249009368 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkI0OjIyNzBGNDozNzcyQTg5OjQ5ODM1MTE6NkIwMjBBOTUiLCJ2aXNpdG9yX2lkIjoiMjUxNjIxNzA0NTA4NTA4ODIxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 54499207846ba0fc21ea58e547e5cce9a08f7dc4b5d8d1279da52e6d315d42a3 |
| hovercard-subject-tag | pull_request:5134556311 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | ///pull_requests/show/files |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/161243/files |
| twitter:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| og:image:alt | A traceback object is created for every exception that is raised. This PR adds a freelist for traceback objects and removes some redundant checks.
benchmark
main
this PR
raise ValueError / e... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Skip to content | https://github.com/python/cpython/pull/161243/files#start-of-content |
| https://github.com/ |
| Sign in | https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F161243%2Ffiles |
| GitHubRepository hosting, code review, and collaboration | https://github.com/platform |
| CopilotAI coding agent for tasks, projects, and automated work | https://github.com/features/copilot |
| ActionsCI/CD and automation | https://github.com/features/actions |
| Code QualityQuality analysis, fixes, and enforcement | https://github.com/features/code-quality |
| CodespacesCloud development environments | https://github.com/features/codespaces |
| Code SecurityVulnerability detection and remediation | https://github.com/security/advanced-security/code-security |
| Secret ProtectionSecret scanning and leak prevention | https://github.com/security/advanced-security/secret-protection |
| All Features | https://github.com/features |
| Documentation | https://docs.github.com/en |
| Training | https://learn.github.com/skills |
| Certifications | https://learn.github.com/credentials |
| Build with Copilot | https://github.com/features/ai |
| Customize Copilot | https://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview |
| Integrations | https://github.com/marketplace |
| Trending repos | https://github.com/trending |
| GitHub Sponsors | https://github.com/open-source/sponsors |
| Developer articles | https://github.com/resources/articles |
| ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place. | https://github.blog/changelog/ |
| Enterprises | https://github.com/enterprise |
| Team | https://github.com/team |
| Startups | https://github.com/enterprise/startups |
| Nonprofits | https://github.com/solutions/industry/nonprofits |
| Healthcare | https://github.com/solutions/industry/healthcare |
| Financial services | https://github.com/solutions/industry/financial-services |
| Manufacturing | https://github.com/solutions/industry/manufacturing |
| Government | https://github.com/solutions/industry/government |
| See all industries | https://github.com/solutions/industry |
| Why GitHub | https://github.com/why-github |
| Customer Stories | https://github.com/customer-stories |
| Business insights | https://github.com/solutions/executive-insights |
| Trust Center | https://github.com/trust-center |
| GitHub Advanced SecurityEnterprise-grade code security | https://github.com/security/advanced-security |
| Copilot for businessEnterprise-grade AI features | https://github.com/features/copilot/copilot-business |
| Premium SupportEnterprise-grade 24/7 support | https://github.com/enterprise/premium-support |
| Documentation | https://docs.github.com/en |
| Resource center | https://github.com/resources |
| Blog | https://github.blog/ |
| Events & webinars | https://github.com/resources/events |
| Community Forum | https://github.com/orgs/community/discussions |
| Customer Stories | https://github.com/customer-stories |
| Integrations | https://github.com/marketplace |
| Customer support | https://help.github.com/ |
| Training | https://learn.github.com/skills |
| GitHub Status | https://www.githubstatus.com/ |
| About | https://github.com/about |
| Partners | https://github.com/partners |
| Changelog | https://github.blog/changelog/ |
| GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub. | https://githubuniverse.com/ |
| Pricing | https://github.com/pricing |
| Sign in | https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F161243%2Ffiles |
| Sign up | https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython |
| Reload | https://github.com/python/cpython/pull/161243/files |
| Reload | https://github.com/python/cpython/pull/161243/files |
| Reload | https://github.com/python/cpython/pull/161243/files |
| python | https://github.com/python |
| cpython | https://github.com/python/cpython |
| Code | https://github.com/python/cpython |
| Issues5k+ (5k+) | https://github.com/python/cpython/issues |
| Pull requests2.4k (2.4k) | https://github.com/python/cpython/pulls |
| Actions | https://github.com/python/cpython/actions |
| Projects | https://github.com/python/cpython/projects |
| Security and quality | https://github.com/python/cpython/security |
| Insights | https://github.com/python/cpython/insights |
| Sign up for GitHub
| https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose |
| terms of service | https://docs.github.com/terms |
| privacy statement | https://docs.github.com/privacy |
| Sign in | https://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose |
| eendebakpt | https://github.com/eendebakpt |
| python:main | https://github.com/python/cpython/tree/main |
| eendebakpt:traceback_freelist_only | https://github.com/eendebakpt/cpython/tree/traceback_freelist_only |
|
Conversation
7
| https://github.com/python/cpython/pull/161243 |
|
Commits
4
| https://github.com/python/cpython/pull/161243/commits |
|
Checks
51
| https://github.com/python/cpython/pull/161243/checks |
|
Files changed
| https://github.com/python/cpython/pull/161243/files |
| Please reload this page | https://github.com/python/cpython/pull/161243/files |
|
gh-161242: Add a freelist for traceback objects
| https://github.com/python/cpython/pull/161243/files#top |
|
Show all changes
4 commits
| https://github.com/python/cpython/pull/161243/files |
|
0251311
Add a freelist for traceback objects
eendebakpt Nov 3, 2026
| https://github.com/python/cpython/pull/161243/commits/0251311e5d570c8727800060b670735eb0d9305d |
|
6765b38
Apply suggestion from @eendebakpt
eendebakpt Nov 3, 2026
| https://github.com/python/cpython/pull/161243/commits/6765b383bed06786448dc6b4c81e53d65c88d5e2 |
|
ed4db4c
Apply suggestion from @eendebakpt
eendebakpt Nov 6, 2026
| https://github.com/python/cpython/pull/161243/commits/ed4db4c8f5bd7323fc1b1c45144d5fe44f0d91ca |
|
81dd418
Merge branch 'main' into traceback_freelist_only
eendebakpt Nov 6, 2026
| https://github.com/python/cpython/pull/161243/commits/81dd418cfe634d63a7fb132c83a9aa90df6593ce |
|
Clear filters
| https://github.com/python/cpython/pull/161243/files |
| Please reload this page | https://github.com/python/cpython/pull/161243/files |
| Please reload this page | https://github.com/python/cpython/pull/161243/files |
|
pycore_freelist_state.h
| https://github.com/python/cpython/pull/161243/files#diff-5c500bbf0471a60d503937a1f89c936211b82fe1dc76cda6df9a0935b2e3e959 |
|
object.c
| https://github.com/python/cpython/pull/161243/files#diff-ba56d44ce0dd731d979970b966fde9d8dd15d12a82f727a052a8ad48d4a49363 |
|
traceback.c
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
|
| https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L321 |
| Include/internal/pycore_freelist_state.h | https://github.com/python/cpython/pull/161243/files#diff-5c500bbf0471a60d503937a1f89c936211b82fe1dc76cda6df9a0935b2e3e959 |
|
View file
| https://github.com/eendebakpt/cpython/blob/81dd418cfe634d63a7fb132c83a9aa90df6593ce/Include/internal/pycore_freelist_state.h |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/python/cpython/pull/161243/{{ revealButtonHref }} |
|
| https://github.com/python/cpython/pull/161243/files#diff-5c500bbf0471a60d503937a1f89c936211b82fe1dc76cda6df9a0935b2e3e959 |
|
| https://github.com/python/cpython/pull/161243/files#diff-5c500bbf0471a60d503937a1f89c936211b82fe1dc76cda6df9a0935b2e3e959 |
|
| https://github.com/python/cpython/pull/161243/files#diff-5c500bbf0471a60d503937a1f89c936211b82fe1dc76cda6df9a0935b2e3e959 |
|
| https://github.com/python/cpython/pull/161243/files#diff-5c500bbf0471a60d503937a1f89c936211b82fe1dc76cda6df9a0935b2e3e959 |
|
| https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L215 |
| Objects/object.c | https://github.com/python/cpython/pull/161243/files#diff-ba56d44ce0dd731d979970b966fde9d8dd15d12a82f727a052a8ad48d4a49363 |
|
View file
| https://github.com/eendebakpt/cpython/blob/81dd418cfe634d63a7fb132c83a9aa90df6593ce/Objects/object.c |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/python/cpython/pull/161243/{{ revealButtonHref }} |
|
| https://github.com/python/cpython/pull/161243/files#diff-ba56d44ce0dd731d979970b966fde9d8dd15d12a82f727a052a8ad48d4a49363 |
|
| https://github.com/python/cpython/pull/161243/files#diff-ba56d44ce0dd731d979970b966fde9d8dd15d12a82f727a052a8ad48d4a49363 |
| Python/traceback.c | https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
|
View file
| https://github.com/eendebakpt/cpython/blob/81dd418cfe634d63a7fb132c83a9aa90df6593ce/Python/traceback.c |
|
Open in desktop
| https://desktop.github.com |
| https://github.co/hiddenchars |
| https://github.com/python/cpython/pull/161243/{{ revealButtonHref }} |
|
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
|
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
|
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
| sobolevn | https://github.com/sobolevn |
| Nov 3, 2026 | https://github.com/python/cpython/pull/161243/files#r4453269186 |
| Learn more | https://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment |
| Please reload this page | https://github.com/python/cpython/pull/161243/files |
| eendebakpt | https://github.com/eendebakpt |
| Nov 3, 2026 | https://github.com/python/cpython/pull/161243/files#r4453665265 |
| Learn more | https://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment |
| Please reload this page | https://github.com/python/cpython/pull/161243/files |
|
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
|
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
|
| https://github.com/python/cpython/pull/161243/files#diff-8bca5f3e0435e43986da8b0359c72b3c60915101d3c0c84d27009eb8c15918f7 |
| Please reload this page | https://github.com/python/cpython/pull/161243/files |
|
| https://github.com |
| Terms | https://docs.github.com/site-policy/github-terms/github-terms-of-service |
| Privacy | https://docs.github.com/site-policy/privacy-policies/github-privacy-statement |
| Security | https://github.com/security |
| Status | https://www.githubstatus.com/ |
| Community | https://github.community/ |
| Docs | https://docs.github.com/ |
| Contact | https://support.github.com?tags=dotcom-footer |
Viewport: width=device-width
URLs of crawlers that visited me.