Skip to content

Commit a95d364

Browse files
committed
Eliminate redundant refcounting for LOAD_ATTR_MODULE
1 parent 0e0d51c commit a95d364

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

Include/internal/pycore_moduleobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern "C" {
1313

1414
extern void _PyModule_Clear(PyObject *);
1515
extern void _PyModule_ClearDict(PyObject *);
16+
extern void _PyModule_ExactDealloc(PyObject *);
1617
extern int _PyModuleSpec_IsInitializing(PyObject *);
1718
extern int _PyModuleSpec_GetFileOrigin(PyObject *, PyObject **);
1819
extern int _PyModule_IsPossiblyShadowing(PyObject *);

Objects/moduleobject.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,13 @@ module_dealloc(PyObject *self)
11261126
Py_TYPE(m)->tp_free((PyObject *)m);
11271127
}
11281128

1129+
void
1130+
_PyModule_ExactDealloc(PyObject *self)
1131+
{
1132+
assert(PyModule_CheckExact(self));
1133+
module_dealloc(self);
1134+
}
1135+
11291136
static PyObject *
11301137
module_repr(PyObject *self)
11311138
{

Python/bytecodes.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ dummy_func(
371371
PyStackRef_CLOSE_SPECIALIZED(value, _PyUnicode_ExactDealloc);
372372
}
373373

374+
op(_POP_TOP_MODULE, (value --)) {
375+
assert(PyModule_CheckExact(PyStackRef_AsPyObjectBorrow(value)));
376+
PyStackRef_CLOSE_SPECIALIZED(value, _PyModule_ExactDealloc);
377+
}
378+
374379
tier2 op(_POP_TWO, (nos, tos --)) {
375380
PyStackRef_CLOSE(tos);
376381
PyStackRef_CLOSE(nos);
@@ -2446,7 +2451,7 @@ dummy_func(
24462451
unused/5 +
24472452
_PUSH_NULL_CONDITIONAL;
24482453

2449-
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr)) {
2454+
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr, owner)) {
24502455
PyObject *owner_o = PyStackRef_AsPyObjectBorrow(owner);
24512456
DEOPT_IF(Py_TYPE(owner_o)->tp_getattro != PyModule_Type.tp_getattro);
24522457
PyDictObject *dict = (PyDictObject *)((PyModuleObject *)owner_o)->md_dict;
@@ -2467,12 +2472,13 @@ dummy_func(
24672472
attr = PyStackRef_FromPyObjectNew(attr_o);
24682473
#endif
24692474
STAT_INC(LOAD_ATTR, hit);
2470-
PyStackRef_CLOSE(owner);
2475+
DEAD(owner);
24712476
}
24722477

24732478
macro(LOAD_ATTR_MODULE) =
24742479
unused/1 +
24752480
_LOAD_ATTR_MODULE +
2481+
_POP_TOP_MODULE +
24762482
unused/5 +
24772483
_PUSH_NULL_CONDITIONAL;
24782484

Python/optimizer_bytecodes.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ dummy_func(void) {
637637
}
638638
}
639639

640+
op(_POP_TOP_MODULE, (value --)) {
641+
if (PyJitRef_IsBorrowed(value)) {
642+
REPLACE_OP(this_instr, _POP_TOP_NOP, 0, 0);
643+
}
644+
}
645+
640646
op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
641647
assert(oparg > 0);
642648
top = bottom;
@@ -655,7 +661,7 @@ dummy_func(void) {
655661
o = owner;
656662
}
657663

658-
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr)) {
664+
op(_LOAD_ATTR_MODULE, (dict_version/2, index/1, owner -- attr, owner)) {
659665
(void)dict_version;
660666
(void)index;
661667
attr = PyJitRef_NULL;

0 commit comments

Comments
 (0)