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

[Experiment] Switching from pybind11 to nanobind for function call overhead improvements #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

TkTech
Copy link
Owner

@TkTech TkTech commented Mar 17, 2024

Switching from pybind11 to nanobind offers some performance improvements with minimal code changes. Our new benchmarks are:

------------------------------------------------------------------------------------- benchmark: 3 tests ------------------------------------------------------------------------------------
Name (time in ms)              Min                 Max                Mean            StdDev              Median               IQR            Outliers      OPS            Rounds  Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_can_ada_parse         38.3641 (1.0)       38.7200 (1.0)       38.5535 (1.0)      0.0861 (1.0)       38.5595 (1.0)      0.1098 (1.0)           9;0  25.9380 (1.0)          26           1
test_ada_python_parse     111.0045 (2.89)     111.3101 (2.87)     111.1474 (2.88)     0.1099 (1.28)     111.1436 (2.88)     0.1624 (1.48)          4;0   8.9971 (0.35)         10           1
test_urllib_parse         255.1016 (6.65)     275.0980 (7.10)     259.3193 (6.73)     8.8238 (102.44)   255.5814 (6.63)     5.3559 (48.77)         1;1   3.8562 (0.15)          5           1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I'm routinely seeing 6-7x better performance over urllib, and significantly improved performance when actually using the results (ie accessing result.pathname) due to lowered attribute access overhead.

However, this introduces CMake as a build time dependency, and reduces the available targets (CPython 3.8+, PyPy > 3.8). Have not yet found a way to eliminate CMake as a dependency. I don't really mind if we only target newer versions of Python.

@lemire @wjakob

@wjakob
Copy link

wjakob commented Mar 19, 2024

@TkTech Did you see the instructions I included here? https://github.com/wjakob/nanobind/blob/master/src/nb_combined.cpp. This should allow you to compile with essentially any other kind of build system, though some work will be needed to replicate all the bells and whistles of what nanobind's cmake tooling provides out of the box. Out of curiosity, what's the relative speedup over the previous pybind11-based version?

@TkTech
Copy link
Owner Author

TkTech commented Mar 19, 2024

@wjakob That's fantastic, I'll give it a full read this weekend and give it a try.

Relative speedup is 30-33%.

Copy link

@anonrig anonrig left a comment

Choose a reason for hiding this comment

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

Amazing.

@raceychan
Copy link

raceychan commented Mar 7, 2025

Any update on this? I'd like to see a switch to nanobind. I was going to implement a cython version but if there is a nanobind version then there is no need since it's pretty much as fast as cython.

Let me know if I can help!

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.

4 participants