Skip to content

Commit

Permalink
refactor: use static raw pointers for global imports
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Nov 25, 2022
1 parent d33cc88 commit 02e5b82
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,39 @@ namespace py = pybind11;
using size_t = py::size_t;
using ssize_t = py::ssize_t;

#define PyCollectionsModule (*ImportCollections())
#define PyOrderedDictTypeObject (*ImportOrderedDict())
#define PyDefaultDictTypeObject (*ImportDefaultDict())
#define PyDequeTypeObject (*ImportDeque())

inline py::module_* ImportCollections() {
static auto collectionsUptr = std::make_unique<py::module_>(
py::reinterpret_borrow<py::module_>(py::module_::import("collections").release()));
return collectionsUptr.get();
}

inline py::object* ImportOrderedDict() {
static auto OrderedDictUptr = std::make_unique<py::object>(
py::reinterpret_borrow<py::object>(py::getattr(PyCollectionsModule, "OrderedDict")));
return OrderedDictUptr.get();
}

inline py::object* ImportDefaultDict() {
static auto defaultdictUptr = std::make_unique<py::object>(
py::reinterpret_borrow<py::object>(py::getattr(PyCollectionsModule, "defaultdict")));
return defaultdictUptr.get();
}

inline py::object* ImportDeque() {
static auto dequeUptr = std::make_unique<py::object>(
py::reinterpret_borrow<py::object>(py::getattr(PyCollectionsModule, "deque")));
return dequeUptr.get();
#define PyCollectionsModule (ImportCollections())
#define PyOrderedDictTypeObject (ImportOrderedDict())
#define PyDefaultDictTypeObject (ImportDefaultDict())
#define PyDequeTypeObject (ImportDeque())

inline const py::module_& ImportCollections() {
// NOTE: Use raw pointers to leak the memory intentionally to avoid py::object deallocation and
// garbage collection
static const py::module_* collectionsPtr = new py::module_(py::module_::import("collections"));
return *collectionsPtr;
}

inline const py::object& ImportOrderedDict() {
// NOTE: Use raw pointers to leak the memory intentionally to avoid py::object deallocation and
// garbage collection
static const py::object* OrderedDictPtr =
new py::object(py::getattr(PyCollectionsModule, "OrderedDict"));
return *OrderedDictPtr;
}

inline const py::object& ImportDefaultDict() {
// NOTE: Use raw pointers to leak the memory intentionally to avoid py::object deallocation and
// garbage collection
static const py::object* defaultdictPtr =
new py::object(py::getattr(PyCollectionsModule, "defaultdict"));
return *defaultdictPtr;
}

inline const py::object& ImportDeque() {
// NOTE: Use raw pointers to leak the memory intentionally to avoid py::object deallocation and
// garbage collection
static const py::object* dequePtr = new py::object(py::getattr(PyCollectionsModule, "deque"));
return *dequePtr;
}

template <typename T>
Expand Down

0 comments on commit 02e5b82

Please sign in to comment.