GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve performance of exceptions · Issue #161242 · python/cpython

Improve performance of exceptions #161242

New issue
Copy link
New issue
Copy link
Open
Open
Improve performance of exceptions#161242
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

Description

@eendebakpt
eendebakpt
opened on Nov 3, 2026
Issue body actions

Feature or enhancement

Raising exceptions in python is rather expensive:

d = {'a': 'b', 'c': 1}

def f():
    try: return d['x']
    except: pass
def g():
    if 'x' in d:
        return d['x']
%timeit f()
%timeit g()

211 ns ± 1.08 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
31.5 ns ± 0.265 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

We can improve this by using freelists (for tracebacks and exceptions), using managed dicts for exceptions and improving the performance of attribute setters.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

  • gh-161242: Add a freelist for traceback objects #161243
  • gh-161242: Cache strerror() messages per errno #161455
  • gh-161242: Use AC vectorcall for AttributeError, ImportError, NameError #161462
Reacted by Cody Maloney

Activity

  1. eendebakpt
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Nov 3, 2026
  2. aisk
    added
    performancePerformance or resource usagePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Nov 3, 2026
  3. cmaloney commented on Nov 3, 2026

    @cmaloney
    cmaloney
    on Nov 3, 2026
    Contributor
    More actions

    👍 to adding a freelist.

    I'm planning to use AC Vectorcall on the core exception hierarchy including AttributeError and KeyError which should help a number of cases although have to be careful in application. Some exceptions store the args tuple and going tuple -> Vectorcall -> tuple is slower than not doing vectorcall unless take special care.

    OSError currently spends most its time re-encoding the output of strerror_r as a str which I plan to fix (just keep all the ones we've encountered as a dict of immortal strings).

    Can I use this issue for that work or would you rather a separate one?

    Reacted by Pieter Eendebak
  4. cmaloney
    added 2 commits that reference this issue on Nov 6, 2026

    pythongh-161242: Cache strerror() messages per errno

    241ed72

    pythongh-161242: Cache strerror() messages per errno

    98e6d6e
  5. eendebakpt
    added a commit that references this issue on Nov 6, 2026

    gh-161242: Add a freelist for traceback objects (#161243)

    3feccd1
  6. cmaloney
    added a commit that references this issue on Nov 7, 2026

    gh-161242: Use AC vectorcall for AttributeError, ImportError, NameErr…

    f767423
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.