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-profiling0
react-import-mapreact,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-controllerpull_requests
route-actionfiles
fetch-noncev2:a46f9a57-fd4f-0c3a-69f0-543512ce9d62
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idAFB4:2270F4:3772A89:4983511:6B020A95
html-safe-noncea250f364c34b40a0838bcd07b50f57389f352aa9060688847fd9468249009368
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkI0OjIyNzBGNDozNzcyQTg5OjQ5ODM1MTE6NkIwMjBBOTUiLCJ2aXNpdG9yX2lkIjoiMjUxNjIxNzA0NTA4NTA4ODIxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac54499207846ba0fc21ea58e547e5cce9a08f7dc4b5d8d1279da52e6d315d42a3
hovercard-subject-tagpull_request:5134556311
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/161243/files
twitter:imagehttps://avatars.githubusercontent.com/u/883786?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/883786?s=400&v=4
og:image:altA 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_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/161243/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F161243%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F161243%2Ffiles
Sign uphttps://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
Reloadhttps://github.com/python/cpython/pull/161243/files
Reloadhttps://github.com/python/cpython/pull/161243/files
Reloadhttps://github.com/python/cpython/pull/161243/files
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
eendebakpthttps://github.com/eendebakpt
python:mainhttps://github.com/python/cpython/tree/main
eendebakpt:traceback_freelist_onlyhttps://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 pagehttps://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 pagehttps://github.com/python/cpython/pull/161243/files
Please reload this pagehttps://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.hhttps://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.chttps://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.chttps://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
sobolevnhttps://github.com/sobolevn
Nov 3, 2026https://github.com/python/cpython/pull/161243/files#r4453269186
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
Please reload this pagehttps://github.com/python/cpython/pull/161243/files
eendebakpthttps://github.com/eendebakpt
Nov 3, 2026https://github.com/python/cpython/pull/161243/files#r4453665265
Learn morehttps://docs.github.com/articles/managing-disruptive-comments/#hiding-a-comment
Please reload this pagehttps://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 pagehttps://github.com/python/cpython/pull/161243/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.