Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3b836d

Browse files
committed
* Improve readability and remove data dependencies by converting
pre-increment forms to post-increment forms. Post-incrementing also eliminates the need for negative array indices for oparg fetches. * In exception handling code, check for class based exceptions before the older string based exceptions.
1 parent 467a698 commit d3b836d

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

Python/ceval.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ eval_frame(PyFrameObject *f)
625625

626626
#define INSTR_OFFSET() (next_instr - first_instr)
627627
#define NEXTOP() (*next_instr++)
628-
#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
628+
#define OPARG() (next_instr[0] + (next_instr[1]<<8))
629+
#define SKIPARG() (next_instr += 2)
629630
#define JUMPTO(x) (next_instr = first_instr + (x))
630631
#define JUMPBY(x) (next_instr += (x))
631632

@@ -861,8 +862,10 @@ eval_frame(PyFrameObject *f)
861862
/* Extract opcode and argument */
862863

863864
opcode = NEXTOP();
864-
if (HAS_ARG(opcode))
865-
oparg = NEXTARG();
865+
if (HAS_ARG(opcode)) {
866+
oparg = OPARG();
867+
SKIPARG();
868+
}
866869
dispatch_opcode:
867870
#ifdef DYNAMIC_EXECUTION_PROFILE
868871
#ifdef DXPAIRS
@@ -1653,7 +1656,7 @@ eval_frame(PyFrameObject *f)
16531656
if (why & (WHY_RETURN | WHY_CONTINUE))
16541657
retval = POP();
16551658
}
1656-
else if (PyString_Check(v) || PyClass_Check(v)) {
1659+
else if (PyClass_Check(v) || PyString_Check(v)) {
16571660
w = POP();
16581661
u = POP();
16591662
PyErr_Restore(v, w, u);
@@ -1892,7 +1895,7 @@ eval_frame(PyFrameObject *f)
18921895
case BUILD_TUPLE:
18931896
x = PyTuple_New(oparg);
18941897
if (x != NULL) {
1895-
for (; --oparg >= 0;) {
1898+
while (oparg--) {
18961899
w = POP();
18971900
PyTuple_SET_ITEM(x, oparg, w);
18981901
}
@@ -1904,7 +1907,7 @@ eval_frame(PyFrameObject *f)
19041907
case BUILD_LIST:
19051908
x = PyList_New(oparg);
19061909
if (x != NULL) {
1907-
for (; --oparg >= 0;) {
1910+
while (oparg--) {
19081911
w = POP();
19091912
PyList_SET_ITEM(x, oparg, w);
19101913
}
@@ -2176,7 +2179,7 @@ eval_frame(PyFrameObject *f)
21762179
x = NULL;
21772180
break;
21782181
}
2179-
while (--oparg >= 0) {
2182+
while (oparg--) {
21802183
w = POP();
21812184
PyTuple_SET_ITEM(v, oparg, w);
21822185
}
@@ -2201,7 +2204,7 @@ eval_frame(PyFrameObject *f)
22012204
x = NULL;
22022205
break;
22032206
}
2204-
while (--nfree >= 0) {
2207+
while (nfree--) {
22052208
w = POP();
22062209
PyTuple_SET_ITEM(v, nfree, w);
22072210
}
@@ -2215,7 +2218,7 @@ eval_frame(PyFrameObject *f)
22152218
x = NULL;
22162219
break;
22172220
}
2218-
while (--oparg >= 0) {
2221+
while (oparg--) {
22192222
w = POP();
22202223
PyTuple_SET_ITEM(v, oparg, w);
22212224
}
@@ -2243,7 +2246,8 @@ eval_frame(PyFrameObject *f)
22432246

22442247
case EXTENDED_ARG:
22452248
opcode = NEXTOP();
2246-
oparg = oparg<<16 | NEXTARG();
2249+
oparg = oparg<<16 | OPARG();
2250+
SKIPARG();
22472251
goto dispatch_opcode;
22482252

22492253
default:
@@ -3192,7 +3196,7 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
31923196
}
31933197

31943198
if (size > 0) {
3195-
while (--size >= 0) {
3199+
while (size--) {
31963200
addr += *p++;
31973201
if (*p++)
31983202
break;
@@ -3607,7 +3611,7 @@ update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
36073611
}
36083612
if (kwdict == NULL)
36093613
return NULL;
3610-
while (--nk >= 0) {
3614+
while (nk--) {
36113615
int err;
36123616
PyObject *value = EXT_POP(*pp_stack);
36133617
PyObject *key = EXT_POP(*pp_stack);
@@ -3652,7 +3656,7 @@ update_star_args(int nstack, int nstar, PyObject *stararg,
36523656
PyTuple_SET_ITEM(callargs, nstack + i, a);
36533657
}
36543658
}
3655-
while (--nstack >= 0) {
3659+
while (nstack--) {
36563660
w = EXT_POP(*pp_stack);
36573661
PyTuple_SET_ITEM(callargs, nstack, w);
36583662
}
@@ -3667,7 +3671,7 @@ load_args(PyObject ***pp_stack, int na)
36673671

36683672
if (args == NULL)
36693673
return NULL;
3670-
while (--na >= 0) {
3674+
while (na--) {
36713675
w = EXT_POP(*pp_stack);
36723676
PyTuple_SET_ITEM(args, na, w);
36733677
}

0 commit comments

Comments
 (0)