Skip to content

Conversation

@swolchok
Copy link
Contributor

If a handle or object is called with only positional arguments, it is straightforward to use PyObject_Vectorcall instead of PyObject_CallObject.

Benchmarked by adding a trivial function to pybind11_benchmark:

    m.def("call_func_with_int", [](py::object func) {
      return func(py::cast(1));
    });

and then running python -m timeit --setup 'from pybind11_benchmark import call_func_with_int; f = lambda x: x + 1' 'call_func_with_int(f)'.

Before on M4 mac: 57.6 nsec per loop
After on M4 mac: 48.4 nsec per loop

For comparison, the included collatz benchmark takes 33.1 nsec per loop, just calling f(1) directly takes 17.8 nec per loop, and simply running pass takes 4.19 nsec per loop.

Suggested changelog entry:

  • Use vectorcall for simple C++-to-Python calls (only positional arguments, no tuple expansion).

If a handle or object is called with only positional arguments, it is
straightforward to use PyObject_Vectorcall instead of
PyObject_CallObject.

Benchmarked by adding a trivial function to pybind11_benchmark:
```
    m.def("call_func_with_int", [](py::object func) {
      return func(py::cast(1));
    });
```

and then running `python -m timeit --setup 'from pybind11_benchmark import call_func_with_int; f = lambda x: x + 1' 'call_func_with_int(f)'`.

Before on M4 mac: 57.6 nsec per loop
After on M4 mac: 48.4 nsec per loop

For comparison, the included collatz benchmark takes 33.1 nsec per
loop, just calling `f(1)` directly takes 17.8 nec per loop, and simply
running `pass` takes 4.19 nsec per loop.
// Disable warnings about useless comparisons when N == 0.
PYBIND11_WARNING_PUSH
PYBIND11_WARNING_DISABLE_GCC("-Wtype-limits")
PYBIND11_WARNING_DISABLE_INTEL(186)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why suppressing the icc warning didn't work :(

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant