Skip to content

Commit 16b8406

Browse files
committed
Use DECREF_INPUTS() more
Also convert END_FOR (the only macro inst) to a regular inst.
1 parent 634cb61 commit 16b8406

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);
@@ -386,8 +388,7 @@ dummy_func(
386388
if (!_PyErr_Occurred(tstate)) {
387389
_PyErr_SetKeyError(sub);
388390
}
389-
Py_DECREF(dict);
390-
Py_DECREF(sub);
391+
DECREF_INPUTS();
391392
ERROR_IF(true, error);
392393
}
393394
Py_INCREF(res); // Do this before DECREF'ing dict, sub
@@ -422,7 +423,7 @@ dummy_func(
422423

423424
inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
424425
int err = PySet_Add(set, v);
425-
Py_DECREF(v);
426+
DECREF_INPUTS();
426427
ERROR_IF(err, error);
427428
PREDICT(JUMP_BACKWARD);
428429
}
@@ -901,7 +902,7 @@ dummy_func(
901902
#endif /* ENABLE_SPECIALIZATION */
902903
PyObject **top = stack_pointer + oparg - 1;
903904
int res = unpack_iterable(tstate, seq, oparg, -1, top);
904-
Py_DECREF(seq);
905+
DECREF_INPUTS();
905906
ERROR_IF(res == 0, error);
906907
}
907908

@@ -912,7 +913,7 @@ dummy_func(
912913
STAT_INC(UNPACK_SEQUENCE, hit);
913914
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
914915
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
915-
Py_DECREF(seq);
916+
DECREF_INPUTS();
916917
}
917918

918919
inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
@@ -923,7 +924,7 @@ dummy_func(
923924
for (int i = oparg; --i >= 0; ) {
924925
*values++ = Py_NewRef(items[i]);
925926
}
926-
Py_DECREF(seq);
927+
DECREF_INPUTS();
927928
}
928929

929930
inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
@@ -934,14 +935,14 @@ dummy_func(
934935
for (int i = oparg; --i >= 0; ) {
935936
*values++ = Py_NewRef(items[i]);
936937
}
937-
Py_DECREF(seq);
938+
DECREF_INPUTS();
938939
}
939940

940941
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
941942
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
942943
PyObject **top = stack_pointer + totalargs - 1;
943944
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
944-
Py_DECREF(seq);
945+
DECREF_INPUTS();
945946
ERROR_IF(res == 0, error);
946947
}
947948

@@ -969,22 +970,21 @@ dummy_func(
969970
#endif /* ENABLE_SPECIALIZATION */
970971
PyObject *name = GETITEM(names, oparg);
971972
int err = PyObject_SetAttr(owner, name, v);
972-
Py_DECREF(v);
973-
Py_DECREF(owner);
973+
DECREF_INPUTS();
974974
ERROR_IF(err, error);
975975
}
976976

977977
inst(DELETE_ATTR, (owner --)) {
978978
PyObject *name = GETITEM(names, oparg);
979979
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
980-
Py_DECREF(owner);
980+
DECREF_INPUTS();
981981
ERROR_IF(err, error);
982982
}
983983

984984
inst(STORE_GLOBAL, (v --)) {
985985
PyObject *name = GETITEM(names, oparg);
986986
int err = PyDict_SetItem(GLOBALS(), name, v);
987-
Py_DECREF(v);
987+
DECREF_INPUTS();
988988
ERROR_IF(err, error);
989989
}
990990

@@ -1251,9 +1251,7 @@ dummy_func(
12511251

12521252
inst(BUILD_STRING, (pieces[oparg] -- str)) {
12531253
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
1254-
for (int i = 0; i < oparg; i++) {
1255-
Py_DECREF(pieces[i]);
1256-
}
1254+
DECREF_INPUTS();
12571255
ERROR_IF(str == NULL, error);
12581256
}
12591257

@@ -1316,10 +1314,7 @@ dummy_func(
13161314
if (map == NULL)
13171315
goto error;
13181316

1319-
for (int i = 0; i < oparg; i++) {
1320-
Py_DECREF(values[i*2]);
1321-
Py_DECREF(values[i*2+1]);
1322-
}
1317+
DECREF_INPUTS();
13231318
ERROR_IF(map == NULL, error);
13241319
}
13251320

@@ -1375,10 +1370,7 @@ dummy_func(
13751370
map = _PyDict_FromItems(
13761371
&PyTuple_GET_ITEM(keys, 0), 1,
13771372
values, 1, oparg);
1378-
Py_DECREF(keys);
1379-
for (int i = 0; i < oparg; i++) {
1380-
Py_DECREF(values[i]);
1381-
}
1373+
DECREF_INPUTS();
13821374
ERROR_IF(map == NULL, error);
13831375
}
13841376

@@ -1466,7 +1458,7 @@ dummy_func(
14661458
14671459
NULL | meth | arg1 | ... | argN
14681460
*/
1469-
Py_DECREF(owner);
1461+
DECREF_INPUTS();
14701462
ERROR_IF(meth == NULL, error);
14711463
res2 = NULL;
14721464
res = meth;
@@ -1475,7 +1467,7 @@ dummy_func(
14751467
else {
14761468
/* Classic, pushes one value. */
14771469
res = PyObject_GetAttr(owner, name);
1478-
Py_DECREF(owner);
1470+
DECREF_INPUTS();
14791471
ERROR_IF(res == NULL, error);
14801472
}
14811473
}
@@ -1494,7 +1486,7 @@ dummy_func(
14941486
STAT_INC(LOAD_ATTR, hit);
14951487
Py_INCREF(res);
14961488
res2 = NULL;
1497-
Py_DECREF(owner);
1489+
DECREF_INPUTS();
14981490
}
14991491

15001492
inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1511,7 +1503,7 @@ dummy_func(
15111503
STAT_INC(LOAD_ATTR, hit);
15121504
Py_INCREF(res);
15131505
res2 = NULL;
1514-
Py_DECREF(owner);
1506+
DECREF_INPUTS();
15151507
}
15161508

15171509
inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1542,7 +1534,7 @@ dummy_func(
15421534
STAT_INC(LOAD_ATTR, hit);
15431535
Py_INCREF(res);
15441536
res2 = NULL;
1545-
Py_DECREF(owner);
1537+
DECREF_INPUTS();
15461538
}
15471539

15481540
inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1556,7 +1548,7 @@ dummy_func(
15561548
STAT_INC(LOAD_ATTR, hit);
15571549
Py_INCREF(res);
15581550
res2 = NULL;
1559-
Py_DECREF(owner);
1551+
DECREF_INPUTS();
15601552
}
15611553

15621554
inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
@@ -1572,7 +1564,7 @@ dummy_func(
15721564
res = descr;
15731565
assert(res != NULL);
15741566
Py_INCREF(res);
1575-
Py_DECREF(cls);
1567+
DECREF_INPUTS();
15761568
}
15771569

15781570
inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
@@ -1709,8 +1701,7 @@ dummy_func(
17091701
STAT_INC(COMPARE_OP, deferred);
17101702
assert((oparg >> 4) <= Py_GE);
17111703
res = PyObject_RichCompare(left, right, oparg>>4);
1712-
Py_DECREF(left);
1713-
Py_DECREF(right);
1704+
DECREF_INPUTS();
17141705
ERROR_IF(res == NULL, error);
17151706
}
17161707

@@ -1736,8 +1727,7 @@ dummy_func(
17361727
#endif /* ENABLE_SPECIALIZATION */
17371728
assert((oparg >> 4) <= Py_GE);
17381729
PyObject *cond = PyObject_RichCompare(left, right, oparg>>4);
1739-
Py_DECREF(left);
1740-
Py_DECREF(right);
1730+
DECREF_INPUTS();
17411731
ERROR_IF(cond == NULL, error);
17421732
assert(next_instr[1].op.code == POP_JUMP_IF_FALSE ||
17431733
next_instr[1].op.code == POP_JUMP_IF_TRUE);
@@ -1887,7 +1877,7 @@ dummy_func(
18871877
}
18881878
else {
18891879
int err = PyObject_IsTrue(cond);
1890-
Py_DECREF(cond);
1880+
DECREF_INPUTS();
18911881
if (err == 0) {
18921882
JUMPBY(oparg);
18931883
}
@@ -1907,7 +1897,7 @@ dummy_func(
19071897
}
19081898
else {
19091899
int err = PyObject_IsTrue(cond);
1910-
Py_DECREF(cond);
1900+
DECREF_INPUTS();
19111901
if (err > 0) {
19121902
JUMPBY(oparg);
19131903
}
@@ -1919,7 +1909,7 @@ dummy_func(
19191909

19201910
inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
19211911
if (!Py_IsNone(value)) {
1922-
Py_DECREF(value);
1912+
DECREF_INPUTS();
19231913
JUMPBY(oparg);
19241914
}
19251915
else {
@@ -1933,7 +1923,7 @@ dummy_func(
19331923
JUMPBY(oparg);
19341924
}
19351925
else {
1936-
Py_DECREF(value);
1926+
DECREF_INPUTS();
19371927
}
19381928
}
19391929

@@ -1950,7 +1940,7 @@ dummy_func(
19501940
else {
19511941
err = PyObject_IsTrue(cond);
19521942
if (err > 0) {
1953-
Py_DECREF(cond);
1943+
DECREF_INPUTS();
19541944
}
19551945
else if (err == 0) {
19561946
JUMPBY(oparg);
@@ -1979,7 +1969,7 @@ dummy_func(
19791969
jump = true;
19801970
}
19811971
else if (err == 0) {
1982-
Py_DECREF(cond);
1972+
DECREF_INPUTS();
19831973
}
19841974
else {
19851975
goto error;
@@ -2067,7 +2057,7 @@ dummy_func(
20672057
if (iter == NULL) {
20682058
goto error;
20692059
}
2070-
Py_DECREF(iterable);
2060+
DECREF_INPUTS();
20712061
}
20722062
PREDICT(LOAD_CONST);
20732063
}
@@ -2112,7 +2102,7 @@ dummy_func(
21122102
}
21132103
/* iterator ended normally */
21142104
assert(next_instr[INLINE_CACHE_ENTRIES_FOR_ITER + oparg].op.code == END_FOR);
2115-
Py_DECREF(iter);
2105+
DECREF_INPUTS();
21162106
STACK_SHRINK(1);
21172107
/* Jump forward oparg, then skip following END_FOR instruction */
21182108
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
@@ -2135,7 +2125,7 @@ dummy_func(
21352125
it->it_seq = NULL;
21362126
Py_DECREF(seq);
21372127
}
2138-
Py_DECREF(iter);
2128+
DECREF_INPUTS();
21392129
STACK_SHRINK(1);
21402130
/* Jump forward oparg, then skip following END_FOR instruction */
21412131
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
@@ -2158,7 +2148,7 @@ dummy_func(
21582148
it->it_seq = NULL;
21592149
Py_DECREF(seq);
21602150
}
2161-
Py_DECREF(iter);
2151+
DECREF_INPUTS();
21622152
STACK_SHRINK(1);
21632153
/* Jump forward oparg, then skip following END_FOR instruction */
21642154
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
@@ -2942,9 +2932,7 @@ dummy_func(
29422932
assert(PyTuple_CheckExact(callargs));
29432933

29442934
result = do_call_core(tstate, func, callargs, kwargs, cframe.use_tracing);
2945-
Py_DECREF(func);
2946-
Py_DECREF(callargs);
2947-
Py_XDECREF(kwargs);
2935+
DECREF_INPUTS();
29482936

29492937
assert(PEEK(3 + (oparg & 1)) == NULL);
29502938
ERROR_IF(result == NULL, error);
@@ -3011,9 +2999,7 @@ dummy_func(
30112999

30123000
inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
30133001
slice = PySlice_New(start, stop, step);
3014-
Py_DECREF(start);
3015-
Py_DECREF(stop);
3016-
Py_XDECREF(step);
3002+
DECREF_INPUTS();
30173003
ERROR_IF(slice == NULL, error);
30183004
}
30193005

@@ -3059,8 +3045,7 @@ dummy_func(
30593045
} else {
30603046
/* Actually call format(). */
30613047
result = PyObject_Format(value, fmt_spec);
3062-
Py_DECREF(value);
3063-
Py_XDECREF(fmt_spec);
3048+
DECREF_INPUTS();
30643049
ERROR_IF(result == NULL, error);
30653050
}
30663051
}
@@ -3086,8 +3071,7 @@ dummy_func(
30863071
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
30873072
assert(binary_ops[oparg]);
30883073
res = binary_ops[oparg](lhs, rhs);
3089-
Py_DECREF(lhs);
3090-
Py_DECREF(rhs);
3074+
DECREF_INPUTS();
30913075
ERROR_IF(res == NULL, error);
30923076
}
30933077

0 commit comments

Comments
 (0)