Skip to content

Speed up BaseExceptionGroup.{derive,subgroup,split} by ~20% #111666

Closed

Description

@sobolevn

There's a simple change that we can make to increase the preformance of these three methods. Right now they are defined as:

cpython/Objects/exceptions.c

Lines 1491 to 1493 in f4b5588

{"derive", (PyCFunction)BaseExceptionGroup_derive, METH_VARARGS},
{"split", (PyCFunction)BaseExceptionGroup_split, METH_VARARGS},
{"subgroup", (PyCFunction)BaseExceptionGroup_subgroup, METH_VARARGS},

However, they only ever use one argument:

if (!PyArg_ParseTuple(args, "O", &excs)) {
return NULL;
}

So, it would be much faster to use METH_O instead. I did these measurements, before and after:

» pyperf timeit -s 'e = BaseExceptionGroup("Message", [ValueError(1)] * 10); i = [TypeError(2)] * 10' 'e.derive(i)'
.....................
Mean +- std dev: 353 ns +- 2 ns

» pyperf timeit -s 'e = BaseExceptionGroup("Message", [ValueError(1)] * 10); i = [TypeError(2)] * 10' 'e.derive(i)'
.....................
Mean +- std dev: 319 ns +- 4 ns
» pyperf timeit -s 'e = BaseExceptionGroup("Message", [ValueError(n) if n % 2 == 0 else TypeError(n) for n in range(100)]); f = lambda e: True' 'e.split(f)'
.....................
Mean +- std dev: 180 ns +- 1 ns

» pyperf timeit -s 'e = BaseExceptionGroup("Message", [ValueError(n) if n % 2 == 0 else TypeError(n) for n in range(100)]); f = lambda e: True' 'e.split(f)'
.....................
Mean +- std dev: 151 ns +- 1 ns
» pyperf timeit -s 'e = BaseExceptionGroup("Message", [ValueError(n) if n % 2 == 0 else TypeError(n) for n in range(100)]); f = lambda e: True' 'e.subgroup(f)'
.....................
Mean +- std dev: 153 ns +- 0 ns

» pyperf timeit -s 'e = BaseExceptionGroup("Message", [ValueError(n) if n % 2 == 0 else TypeError(n) for n in range(100)]); f = lambda e: True' 'e.subgroup(f)'
.....................
Mean +- std dev: 121 ns +- 0 ns

Linked PRs

Activity

  1. added
    performance
    interpreter-core
    on Nov 2, 2023
  2. self-assigned this
    on Nov 2, 2023
  3. added a commit that references this issue on Nov 2, 2023
  4. added a commit that references this issue on Nov 4, 2023
  5. hugovk commented on Nov 9, 2023

    @hugovk
    Member

    Closing because #111667 was merged, feel free to re-open if needed.

  6. added a commit that references this issue on Feb 11, 2024
  7. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-coreperformance

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions