You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Split pybind11 into headers and cpp files to speedup compilation
TODO For now, only pybind11.h and options.h have been split. If maintainers
feel that this is going in the right direction, I will update this commit
to split all files.
As discussed at #2322, the split
is opt-in, and has been observed to speed up the compilation of the gem5
project by around 40% from 25 minutes to 15 minutes.
If the PYBIND11_DECLARATIONS_ONLY is not defined, then the .cpp files are
included in the .h files, and everything works header-only as was the case
before this commit.
If PYBIND11_DECLARATIONS_ONLY=1, then only declarations are made visible
from, the header files, and the user must the user must compile the
pybind11 .cpp files, also using PYBIND11_DECLARATIONS_ONLY=1, into objects
and add link those into the final link. This commit also updates CMakeLists
to automate building those object files.
This commit has been tested as follows.
First, the original build and pytest (without PYBIND11_DECLARATIONS_ONLY)
work as before:
mkdir build
cmake ..
make -j `nproc`
make pytest
TODO: if this commit gets traction, I will try to add a test for the
PYBIND11_DECLARATIONS_ONLY version too, otherwise it will likely break.
I'd just re-run the entire pybind with PYBIND11_DECLARATIONS_ONLY as well
every time.
The commit has also been tested with the following minimal manual example:
class_test.cpp
```
struct ClassTest {
ClassTest(const std::string &name) : name(name) {}
void setName(const std::string &name_) { name = name_; }
const std::string &getName() const { return name; }
std::string name;
};
struct ClassTestDerived : ClassTest {
ClassTestDerived(const std::string &name, const std::string &name2) :
ClassTest(name), name2(name2) {}
std::string getName2() { return name + name2 + "2"; }
std::string name2;
};
namespace py = pybind11;
PYBIND11_MODULE(class_test, m) {
m.doc() = "pybind11 example plugin";
py::class_<ClassTest>(m, "ClassTest")
.def(py::init<const std::string &>())
.def("setName", &ClassTest::setName)
.def("getName", &ClassTest::getName)
.def_readwrite("name", &ClassTest::name);
py::class_<ClassTestDerived, ClassTest>(m, "ClassTestDerived")
.def(py::init<const std::string &, const std::string &>())
.def("getName2", &ClassTestDerived::getName2)
.def_readwrite("name", &ClassTestDerived::name);
}
```
class_test_main.py
```
import class_test
my_class_test = class_test.ClassTest('abc');
assert(my_class_test.getName() == 'abc')
my_class_test.setName('012')
assert(my_class_test.name == '012')
my_class_test_derived = class_test.ClassTestDerived('abc', 'def');
assert(my_class_test_derived.getName2() == 'abcdef2')
```
without PYBIND11_DECLARATIONS_ONLY (`python3-config --cflags`) is opened up
and hacked to point to the custom pybind11 source:
```
g++ \
-save-temps \
-I/usr/include/python3.8 \
-I/home/ciro/git/pybind11/include \
-Wno-unused-result \
-Wsign-compare \
-g \
-fdebug-prefix-map=/build/python3.8-fKk4GY/python3.8-3.8.2=. \
-specs=/usr/share/dpkg/no-pie-compile.specs \
-fstack-protector \
-Wformat \
-Werror=format-security \
-DNDEBUG \
-g \
-fwrapv \
-O3 \
-Wall \
-shared \
-std=c++11 \
-fPIC class_test.cpp \
-o class_test`python3-config --extension-suffix` \
`python3-config --libs` \
;
./class_test_main.py
```
with PYBIND11_DECLARATIONS_ONLY:
```
g++ \
-DPYBIND11_DECLARATIONS_ONLY=1 \
-save-temps \
-I/usr/include/python3.8 \
-I/home/ciro/git/pybind11/include \
-Wno-unused-result \
-Wsign-compare \
-g \
-fdebug-prefix-map=/build/python3.8-fKk4GY/python3.8-3.8.2=. \
-specs=/usr/share/dpkg/no-pie-compile.specs \
-fstack-protector \
-Wformat \
-Werror=format-security \
-DNDEBUG \
-g \
-fwrapv \
-O3 \
-Wall \
-shared \
-std=c++11 \
-fPIC class_test.cpp \
/home/ciro/git/pybind11/build/libpybind11.a \
-o class_test`python3-config --extension-suffix` \
`python3-config --libs` \
;
./class_test_main.py
```
0 commit comments