There's a simple change that we can make to increase the preformance of these three methods. Right now they are defined as:
|
{"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
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
However, they only ever use one argument:
cpython/Objects/exceptions.c
Lines 883 to 885 in f4b5588
So, it would be much faster to use
METH_Oinstead. I did these measurements, before and after:Linked PRs
BaseExceptionGroup.{derive,split,subgroup}#111667