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 8cda056
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,34 @@ 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_* ptr = new py::module_{py::module_::import("collections")};
return *ptr;
}
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* ptr = new py::object{py::getattr(PyCollectionsModule, "OrderedDict")};
return *ptr;
}
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* ptr = new py::object{py::getattr(PyCollectionsModule, "defaultdict")};
return *ptr;
}
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* ptr = new py::object{py::getattr(PyCollectionsModule, "deque")};
return *ptr;
}

template <typename T>
Expand Down

0 comments on commit 8cda056

Please sign in to comment.