Skip to content

Conversation

@hpkfft
Copy link
Contributor

@hpkfft hpkfft commented Nov 1, 2025

Please consider this PR, which adds a variant of the complex dispatcher that applies when no overloads have kwargs and all overloads have 8 or fewer arguments.

When binding an existing C++ library in order to provide Python support, it's common to have functions that either have default values, or accept nullptr (i.e., None), or have parameters for which noconvert is desirable (especially for nd-array parameters). These are features of C++, as is having only positional arguments.

More than eight parameters is rare. It is arguably a poorly designed API, which would benefit from kwargs in the Python interface (and therefore use the full-featured complex dispatcher).

Given

    auto test_02 = [](int up, int down) -> int { return up - down; };
    m.def("test_02p", (int (*)(int, int)) test_02, nb::arg()=8, nb::arg()=1);
    m.def("test_02nc", (int (*)(int, int)) test_02, nb::arg().noconvert()=8,
                                                    nb::arg().noconvert()=1);

the performance on Python 3.14 is as follows:

test old new ratio
test_02p 27.5ns 22.5ns 1.22
test_02nc 26.9ns 22.5ns 1.20

measured using

python -m timeit -n 100000000 -r 10 -s "import test_functions_ext as t" "t.test_02p(7)"
python -m timeit -n 100000000 -r 10 -s "import test_functions_ext as t" "t.test_02nc(5, 3)"

This PR adds a variant of the complex dispatcher that applies when no
overloads have kwargs and all overloads have 8 or fewer arguments.
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