Skip to content

Commit 2b45efe

Browse files
committed
Use DECREF_INPUTS() more
Also convert END_FOR (the only macro inst) to a regular inst.
1 parent cb944d0 commit 2b45efe

File tree

3 files changed

+53
-81
lines changed

3 files changed

+53
-81
lines changed

Python/bytecodes.c

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ dummy_func(
137137
res = NULL;
138138
}
139139

140-
macro(END_FOR) = POP_TOP + POP_TOP;
140+
inst(END_FOR, (second, first --)) {
141+
DECREF_INPUTS();
142+
}
141143

142144
inst(UNARY_NEGATIVE, (value -- res)) {
143145
res = PyNumber_Negative(value);
@@ -395,8 +397,7 @@ dummy_func(
395397
if (!_PyErr_Occurred(tstate)) {
396398
_PyErr_SetKeyError(sub);
397399
}
398-
Py_DECREF(dict);
399-
Py_DECREF(sub);
400+
DECREF_INPUTS();
400401
ERROR_IF(true, error);
401402
}
402403
Py_INCREF(res); // Do this before DECREF'ing dict, sub
@@ -431,7 +432,7 @@ dummy_func(
431432

432433
inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
433434
int err = PySet_Add(set, v);
434-
Py_DECREF(v);
435+
DECREF_INPUTS();
435436
ERROR_IF(err, error);
436437
PREDICT(JUMP_BACKWARD);
437438
}
@@ -910,7 +911,7 @@ dummy_func(
910911
#endif /* ENABLE_SPECIALIZATION */
911912
PyObject **top = stack_pointer + oparg - 1;
912913
int res = unpack_iterable(tstate, seq, oparg, -1, top);
913-
Py_DECREF(seq);
914+
DECREF_INPUTS();
914915
ERROR_IF(res == 0, error);
915916
}
916917

@@ -921,7 +922,7 @@ dummy_func(
921922
STAT_INC(UNPACK_SEQUENCE, hit);
922923
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
923924
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
924-
Py_DECREF(seq);
925+
DECREF_INPUTS();
925926
}
926927

927928
inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
@@ -932,7 +933,7 @@ dummy_func(
932933
for (int i = oparg; --i >= 0; ) {
933934
*values++ = Py_NewRef(items[i]);
934935
}
935-
Py_DECREF(seq);
936+
DECREF_INPUTS();
936937
}
937938

938939
inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
@@ -943,14 +944,14 @@ dummy_func(
943944
for (int i = oparg; --i >= 0; ) {
944945
*values++ = Py_NewRef(items[i]);
945946
}
946-
Py_DECREF(seq);
947+
DECREF_INPUTS();
947948
}
948949

949950
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
950951
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
951952
PyObject **top = stack_pointer + totalargs - 1;
952953
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
953-
Py_DECREF(seq);
954+
DECREF_INPUTS();
954955
ERROR_IF(res == 0, error);
955956
}
956957

@@ -978,22 +979,21 @@ dummy_func(
978979
#endif /* ENABLE_SPECIALIZATION */
979980
PyObject *name = GETITEM(names, oparg);
980981
int err = PyObject_SetAttr(owner, name, v);
981-
Py_DECREF(v);
982-
Py_DECREF(owner);
982+
DECREF_INPUTS();
983983
ERROR_IF(err, error);
984984
}
985985

986986
inst(DELETE_ATTR, (owner --)) {
987987
PyObject *name = GETITEM(names, oparg);
988988
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
989-
Py_DECREF(owner);
989+
DECREF_INPUTS();
990990
ERROR_IF(err, error);
991991
}
992992

993993
inst(STORE_GLOBAL, (v --)) {
994994
PyObject *name = GETITEM(names, oparg);
995995
int err = PyDict_SetItem(GLOBALS(), name, v);
996-
Py_DECREF(v);
996+
DECREF_INPUTS();
997997
ERROR_IF(err, error);
998998
}
999999

@@ -1260,9 +1260,7 @@ dummy_func(
12601260

12611261
inst(BUILD_STRING, (pieces[oparg] -- str)) {
12621262
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
1263-
for (int i = 0; i < oparg; i++) {
1264-
Py_DECREF(pieces[i]);
1265-
}
1263+
DECREF_INPUTS();
12661264
ERROR_IF(str == NULL, error);
12671265
}
12681266

@@ -1325,10 +1323,7 @@ dummy_func(
13251323
if (map == NULL)
13261324
goto error;
13271325

1328-
for (int i = 0; i < oparg; i++) {
1329-
Py_DECREF(values[i*2]);
1330-
Py_DECREF(values[i*2+1]);
1331-
}
1326+
DECREF_INPUTS();
13321327
ERROR_IF(map == NULL, error);
13331328
}
13341329

@@ -1384,10 +1379,7 @@ dummy_func(
13841379
map = _PyDict_FromItems(
13851380
&PyTuple_GET_ITEM(keys, 0), 1,
13861381
values, 1, oparg);
1387-
Py_DECREF(keys);
1388-
for (int i = 0; i < oparg; i++) {
1389-
Py_DECREF(values[i]);
1390-
}
1382+
DECREF_INPUTS();
13911383
ERROR_IF(map == NULL, error);
13921384
}
13931385

@@ -1475,7 +1467,7 @@ dummy_func(
14751467
14761468
NULL | meth | arg1 | ... | argN
14771469
*/
1478-
Py_DECREF(owner);
1470+
DECREF_INPUTS();
14791471
ERROR_IF(meth == NULL, error);
14801472
res2 = NULL;
14811473
res = meth;
@@ -1484,7 +1476,7 @@ dummy_func(
14841476
else {
14851477
/* Classic, pushes one value. */
14861478
res = PyObject_GetAttr(owner, name);
1487-
Py_DECREF(owner);
1479+
DECREF_INPUTS();
14881480
ERROR_IF(res == NULL, error);
14891481
}
14901482
}
@@ -1503,7 +1495,7 @@ dummy_func(
15031495
STAT_INC(LOAD_ATTR, hit);
15041496
Py_INCREF(res);
15051497
res2 = NULL;
1506-
Py_DECREF(owner);
1498+
DECREF_INPUTS();
15071499
}
15081500

15091501
inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1520,7 +1512,7 @@ dummy_func(
15201512
STAT_INC(LOAD_ATTR, hit);
15211513
Py_INCREF(res);
15221514
res2 = NULL;
1523-
Py_DECREF(owner);
1515+
DECREF_INPUTS();
15241516
}
15251517

15261518
inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1551,7 +1543,7 @@ dummy_func(
15511543
STAT_INC(LOAD_ATTR, hit);
15521544
Py_INCREF(res);
15531545
res2 = NULL;
1554-
Py_DECREF(owner);
1546+
DECREF_INPUTS();
15551547
}
15561548

15571549
inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1565,7 +1557,7 @@ dummy_func(
15651557
STAT_INC(LOAD_ATTR, hit);
15661558
Py_INCREF(res);
15671559
res2 = NULL;
1568-
Py_DECREF(owner);
1560+
DECREF_INPUTS();
15691561
}
15701562

15711563
inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
@@ -1581,7 +1573,7 @@ dummy_func(
15811573
res = descr;
15821574
assert(res != NULL);
15831575
Py_INCREF(res);
1584-
Py_DECREF(cls);
1576+
DECREF_INPUTS();
15851577
}
15861578

15871579
inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
@@ -1718,8 +1710,7 @@ dummy_func(
17181710
STAT_INC(COMPARE_OP, deferred);
17191711
assert((oparg >> 4) <= Py_GE);
17201712
res = PyObject_RichCompare(left, right, oparg>>4);
1721-
Py_DECREF(left);
1722-
Py_DECREF(right);
1713+
DECREF_INPUTS();
17231714
ERROR_IF(res == NULL, error);
17241715
}
17251716

@@ -1745,8 +1736,7 @@ dummy_func(
17451736
#endif /* ENABLE_SPECIALIZATION */
17461737
assert((oparg >> 4) <= Py_GE);
17471738
PyObject *cond = PyObject_RichCompare(left, right, oparg>>4);
1748-
Py_DECREF(left);
1749-
Py_DECREF(right);
1739+
DECREF_INPUTS();
17501740
ERROR_IF(cond == NULL, error);
17511741
assert(next_instr[1].op.code == POP_JUMP_IF_FALSE ||
17521742
next_instr[1].op.code == POP_JUMP_IF_TRUE);
@@ -1896,7 +1886,7 @@ dummy_func(
18961886
}
18971887
else {
18981888
int err = PyObject_IsTrue(cond);
1899-
Py_DECREF(cond);
1889+
DECREF_INPUTS();
19001890
if (err == 0) {
19011891
JUMPBY(oparg);
19021892
}
@@ -1916,7 +1906,7 @@ dummy_func(
19161906
}
19171907
else {
19181908
int err = PyObject_IsTrue(cond);
1919-
Py_DECREF(cond);
1909+
DECREF_INPUTS();
19201910
if (err > 0) {
19211911
JUMPBY(oparg);
19221912
}
@@ -1928,7 +1918,7 @@ dummy_func(
19281918

19291919
inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
19301920
if (!Py_IsNone(value)) {
1931-
Py_DECREF(value);
1921+
DECREF_INPUTS();
19321922
JUMPBY(oparg);
19331923
}
19341924
else {
@@ -1942,7 +1932,7 @@ dummy_func(
19421932
JUMPBY(oparg);
19431933
}
19441934
else {
1945-
Py_DECREF(value);
1935+
DECREF_INPUTS();
19461936
}
19471937
}
19481938

@@ -1959,7 +1949,7 @@ dummy_func(
19591949
else {
19601950
err = PyObject_IsTrue(cond);
19611951
if (err > 0) {
1962-
Py_DECREF(cond);
1952+
DECREF_INPUTS();
19631953
}
19641954
else if (err == 0) {
19651955
JUMPBY(oparg);
@@ -1988,7 +1978,7 @@ dummy_func(
19881978
jump = true;
19891979
}
19901980
else if (err == 0) {
1991-
Py_DECREF(cond);
1981+
DECREF_INPUTS();
19921982
}
19931983
else {
19941984
goto error;
@@ -2076,7 +2066,7 @@ dummy_func(
20762066
if (iter == NULL) {
20772067
goto error;
20782068
}
2079-
Py_DECREF(iterable);
2069+
DECREF_INPUTS();
20802070
}
20812071
PREDICT(LOAD_CONST);
20822072
}
@@ -2121,7 +2111,7 @@ dummy_func(
21212111
}
21222112
/* iterator ended normally */
21232113
assert(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == END_FOR);
2124-
Py_DECREF(iter);
2114+
DECREF_INPUTS();
21252115
STACK_SHRINK(1);
21262116
/* Jump forward oparg, then skip following END_FOR instruction */
21272117
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
@@ -2144,7 +2134,7 @@ dummy_func(
21442134
it->it_seq = NULL;
21452135
Py_DECREF(seq);
21462136
}
2147-
Py_DECREF(iter);
2137+
DECREF_INPUTS();
21482138
STACK_SHRINK(1);
21492139
/* Jump forward oparg, then skip following END_FOR instruction */
21502140
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
@@ -2167,7 +2157,7 @@ dummy_func(
21672157
it->it_seq = NULL;
21682158
Py_DECREF(seq);
21692159
}
2170-
Py_DECREF(iter);
2160+
DECREF_INPUTS();
21712161
STACK_SHRINK(1);
21722162
/* Jump forward oparg, then skip following END_FOR instruction */
21732163
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
@@ -2951,9 +2941,7 @@ dummy_func(
29512941
assert(PyTuple_CheckExact(callargs));
29522942

29532943
result = do_call_core(tstate, func, callargs, kwargs, cframe.use_tracing);
2954-
Py_DECREF(func);
2955-
Py_DECREF(callargs);
2956-
Py_XDECREF(kwargs);
2944+
DECREF_INPUTS();
29572945

29582946
assert(PEEK(3 + (oparg & 1)) == NULL);
29592947
ERROR_IF(result == NULL, error);
@@ -3020,9 +3008,7 @@ dummy_func(
30203008

30213009
inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
30223010
slice = PySlice_New(start, stop, step);
3023-
Py_DECREF(start);
3024-
Py_DECREF(stop);
3025-
Py_XDECREF(step);
3011+
DECREF_INPUTS();
30263012
ERROR_IF(slice == NULL, error);
30273013
}
30283014

@@ -3068,8 +3054,7 @@ dummy_func(
30683054
} else {
30693055
/* Actually call format(). */
30703056
result = PyObject_Format(value, fmt_spec);
3071-
Py_DECREF(value);
3072-
Py_XDECREF(fmt_spec);
3057+
DECREF_INPUTS();
30733058
ERROR_IF(result == NULL, error);
30743059
}
30753060
}
@@ -3095,8 +3080,7 @@ dummy_func(
30953080
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
30963081
assert(binary_ops[oparg]);
30973082
res = binary_ops[oparg](lhs, rhs);
3098-
Py_DECREF(lhs);
3099-
Py_DECREF(rhs);
3083+
DECREF_INPUTS();
31003084
ERROR_IF(res == NULL, error);
31013085
}
31023086

0 commit comments

Comments
 (0)