Skip to content

Commit 302db6a

Browse files
committed
chore: use simple GIL
1 parent 389e17c commit 302db6a

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

include/optree/pytypes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ inline bool IsNamedTupleClass(const py::handle &type) {
261261
static read_write_mutex mutex{};
262262

263263
{
264+
const py::gil_scoped_release_simple gil_release{};
264265
const scoped_read_lock lock{mutex};
265266
const auto it = cache.find(type);
266267
if (it != cache.end()) [[likely]] {
@@ -270,8 +271,10 @@ inline bool IsNamedTupleClass(const py::handle &type) {
270271

271272
const bool result = EVALUATE_WITH_LOCK_HELD(IsNamedTupleClassImpl(type), type);
272273
{
274+
const py::gil_scoped_release_simple gil_release{};
273275
const scoped_write_lock lock{mutex};
274276
if (cache.size() < MAX_TYPE_CACHE_SIZE) [[likely]] {
277+
const py::gil_scoped_acquire_simple gil_acquire{};
275278
cache.emplace(type, result);
276279
(void)py::weakref(type, py::cpp_function([type](py::handle weakref) -> void {
277280
const scoped_write_lock lock{mutex};
@@ -363,6 +366,7 @@ inline bool IsStructSequenceClass(const py::handle &type) {
363366
static read_write_mutex mutex{};
364367

365368
{
369+
const py::gil_scoped_release_simple gil_release{};
366370
const scoped_read_lock lock{mutex};
367371
const auto it = cache.find(type);
368372
if (it != cache.end()) [[likely]] {
@@ -372,8 +376,10 @@ inline bool IsStructSequenceClass(const py::handle &type) {
372376

373377
const bool result = EVALUATE_WITH_LOCK_HELD(IsStructSequenceClassImpl(type), type);
374378
{
379+
const py::gil_scoped_release_simple gil_release{};
375380
const scoped_write_lock lock{mutex};
376381
if (cache.size() < MAX_TYPE_CACHE_SIZE) [[likely]] {
382+
const py::gil_scoped_acquire_simple gil_acquire{};
377383
cache.emplace(type, result);
378384
(void)py::weakref(type, py::cpp_function([type](py::handle weakref) -> void {
379385
const scoped_write_lock lock{mutex};
@@ -446,17 +452,21 @@ inline py::tuple StructSequenceGetFields(const py::handle &object) {
446452
static read_write_mutex mutex{};
447453

448454
{
455+
const py::gil_scoped_release_simple gil_release{};
449456
const scoped_read_lock lock{mutex};
450457
const auto it = cache.find(type);
451458
if (it != cache.end()) [[likely]] {
459+
const py::gil_scoped_acquire_simple gil_acquire{};
452460
return py::reinterpret_borrow<py::tuple>(it->second);
453461
}
454462
}
455463

456464
const py::tuple fields = EVALUATE_WITH_LOCK_HELD(StructSequenceGetFieldsImpl(type), type);
457465
{
466+
const py::gil_scoped_release_simple gil_release{};
458467
const scoped_write_lock lock{mutex};
459468
if (cache.size() < MAX_TYPE_CACHE_SIZE) [[likely]] {
469+
const py::gil_scoped_acquire_simple gil_acquire{};
460470
cache.emplace(type, fields);
461471
fields.inc_ref();
462472
(void)py::weakref(type, py::cpp_function([type](py::handle weakref) -> void {

src/treespec/traversal.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,14 @@ py::object PyTreeIter::NextImpl() {
162162
}
163163

164164
py::object PyTreeIter::Next() {
165-
#if !defined(Py_GIL_DISABLED)
166-
const py::gil_scoped_release gil_release;
167-
#endif
165+
const py::gil_scoped_release_simple gil_release{};
166+
const scoped_lock lock{m_mutex};
168167
{
169-
const scoped_lock lock{m_mutex};
170-
{
171-
#if !defined(Py_GIL_DISABLED)
172-
const py::gil_scoped_acquire gil_acquire;
173-
#endif
174-
if (m_none_is_leaf) [[unlikely]] {
175-
return NextImpl<NONE_IS_LEAF>();
176-
} else [[likely]] {
177-
return NextImpl<NONE_IS_NODE>();
178-
}
168+
const py::gil_scoped_acquire_simple gil_acquire{};
169+
if (m_none_is_leaf) [[unlikely]] {
170+
return NextImpl<NONE_IS_LEAF>();
171+
} else [[likely]] {
172+
return NextImpl<NONE_IS_NODE>();
179173
}
180174
}
181175
}

0 commit comments

Comments
 (0)