Skip to content

Implement vectorcall for BaseException to optimize creation of exception instances #118702

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

Closed
vstinner opened this issue May 7, 2024 · 0 comments
Labels
performance Performance or resource usage

Comments

@vstinner
Copy link
Member

vstinner commented May 7, 2024

Currently, creating an instance of an built-in exception uses the old METH_VARARGS calling convention which creates a temporary tuple to pass positional arguments.

I propose to implement vectorcall for BaseException.

Linked PRs

@vstinner vstinner added the performance Performance or resource usage label May 7, 2024
vstinner added a commit to vstinner/cpython that referenced this issue May 7, 2024
* BaseException_vectorcall() now creates a tuple from 'args' array.
* Creation an exception using BaseException_vectorcall() is now a
  single function call, rather than having to call
  BaseException_new() and then BaseException_init().
  Calling BaseException_init() is inefficient since it overrides
  the 'args' attribute.
* _PyErr_SetKeyError() now uses PyObject_CallOneArg() to create the
  KeyError instance to use BaseException_vectorcall().

Micro-benchmark on creating a KeyError on accessing a non-existent
dictionary key:

    Mean +- std dev: 447 ns +- 31 ns -> 373 ns +- 15 ns: 1.20x faster
vstinner added a commit to vstinner/cpython that referenced this issue May 10, 2024
* BaseException_vectorcall() now creates a tuple from 'args' array.
* Creation an exception using BaseException_vectorcall() is now a
  single function call, rather than having to call
  BaseException_new() and then BaseException_init().
  Calling BaseException_init() is inefficient since it overrides
  the 'args' attribute.
* _PyErr_SetKeyError() now uses PyObject_CallOneArg() to create the
  KeyError instance to use BaseException_vectorcall().

Micro-benchmark on creating a KeyError on accessing a non-existent
dictionary key:

    Mean +- std dev: 447 ns +- 31 ns -> 373 ns +- 15 ns: 1.20x faster
vstinner added a commit to vstinner/cpython that referenced this issue May 10, 2024
* BaseException_vectorcall() now creates a tuple from 'args' array.
* Creation an exception using BaseException_vectorcall() is now a
  single function call, rather than having to call
  BaseException_new() and then BaseException_init().
  Calling BaseException_init() is inefficient since it overrides
  the 'args' attribute.
* _PyErr_SetKeyError() now uses PyObject_CallOneArg() to create the
  KeyError instance to use BaseException_vectorcall().

Micro-benchmark on creating a KeyError on accessing a non-existent
dictionary key:

    Mean +- std dev: 447 ns +- 31 ns -> 373 ns +- 15 ns: 1.20x faster
vstinner added a commit that referenced this issue May 10, 2024
* BaseException_vectorcall() now creates a tuple from 'args' array.
* Creation an exception using BaseException_vectorcall() is now a
  single function call, rather than having to call
  BaseException_new() and then BaseException_init().
  Calling BaseException_init() is inefficient since it overrides
  the 'args' attribute.
* _PyErr_SetKeyError() now uses PyObject_CallOneArg() to create the
  KeyError instance to use BaseException_vectorcall().
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
* BaseException_vectorcall() now creates a tuple from 'args' array.
* Creation an exception using BaseException_vectorcall() is now a
  single function call, rather than having to call
  BaseException_new() and then BaseException_init().
  Calling BaseException_init() is inefficient since it overrides
  the 'args' attribute.
* _PyErr_SetKeyError() now uses PyObject_CallOneArg() to create the
  KeyError instance to use BaseException_vectorcall().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

No branches or pull requests

1 participant