Skip to content
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

Keyword argument handling performance optimization. #2050

Open
kkimdev opened this issue Jan 1, 2020 · 3 comments
Open

Keyword argument handling performance optimization. #2050

kkimdev opened this issue Jan 1, 2020 · 3 comments

Comments

@kkimdev
Copy link

kkimdev commented Jan 1, 2020

Issue description

Keyword argument handling is slower than CPython.

CPython has an optimization that assumes the keyword name string objects are interned and performs pointer comparisons first instead https://github.com/python/cpython/blob/22424c02e51fab3b62cbe255d0b87d1b55b9a6c3/Python/ceval.c#L3975 .

Whereas Pybind constructs string objects and hashes them

if (kwargs_in && arg_rec && arg_rec->name && PyDict_GetItemString(kwargs_in, arg_rec->name)) {
.

What do you think about having a similar optimization in Pybind?

Reproducible example code

void matmul(py::handle a,
            py::handle b,
            py::handle transpose_a,
            py::handle transpose_b,
            py::handle adjoint_a,
            py::handle adjoint_b,
            py::handle a_is_sparse,
            py::handle b_is_sparse,
            py::handle name) {
}
 
PYBIND11_MODULE(test, m) {
 m.def("matmul", &matmul,
       py::arg("a"),
       py::arg("b"),
       py::arg("transpose_a") = false,
       py::arg("transpose_b") = false,
       py::arg("adjoint_a") = false,
       py::arg("adjoint_b") = false,
       py::arg("a_is_sparse") = false,
       py::arg("b_is_sparse") = false,
       py::arg("name") = "");
}
test.matmul(1, 2, transpose_a = True)
@bstaletic
Copy link
Collaborator

This is interesting and worth looking into.

@EricCousineau-TRI
Copy link
Collaborator

EricCousineau-TRI commented Dec 31, 2020

Relates #2760 which benchmarking of alternative implementations, provided via @rwgk and @kkimdev - thanks!
#2760 (comment)

@XuehaiPan
Copy link
Contributor

I'm encountering a similar issue. My focus is on caching compile-time string literals to enhance the efficiency of my program.

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

No branches or pull requests

4 participants