Skip to content

Commit

Permalink
pythongh-111786: Use separate opcode vars for Tier 1 and Tier 2
Browse files Browse the repository at this point in the history
This makes Windows about 3% faster on pyperformance benchmarks.
  • Loading branch information
mdboom committed Nov 20, 2023
1 parent 8deb8bc commit f362f9a
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
#ifdef Py_STATS
int lastopcode = 0;
#endif
int opcode; /* Current opcode */
int oparg; /* Current opcode argument, if any */
uint8_t opcode; /* Current opcode */
int oparg; /* Current opcode argument, if any */
#ifdef LLTRACE
int lltrace = 0;
#endif
Expand Down Expand Up @@ -776,9 +776,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
/* Start instructions */
#if !USE_COMPUTED_GOTOS
dispatch_opcode:
// Cast to an 8-bit value to improve the code generated by MSVC
// (in combination with the EXTRA_CASES macro).
switch ((uint8_t)opcode)
switch (opcode)
#endif
{

Expand Down Expand Up @@ -822,7 +820,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
#if USE_COMPUTED_GOTOS
_unknown_opcode:
#else
EXTRA_CASES // From pycore_opcode.h, a 'case' for each unused opcode
EXTRA_CASES // From pycore_opcode_metadata.h, a 'case' for each unused opcode
#endif
/* Tell C compilers not to hold the opcode variable in the loop.
next_instr points the current instruction without TARGET(). */
Expand Down Expand Up @@ -994,28 +992,29 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

OPT_STAT_INC(traces_executed);
_PyUOpInstruction *next_uop = current_executor->trace;
uint16_t uopcode;
#ifdef Py_STATS
uint64_t trace_uop_execution_counter = 0;
#endif

for (;;) {
opcode = next_uop->opcode;
uopcode = next_uop->opcode;
DPRINTF(3,
"%4d: uop %s, oparg %d, operand %" PRIu64 ", target %d, stack_level %d\n",
(int)(next_uop - current_executor->trace),
_PyUopName(opcode),
_PyUopName(uopcode),
next_uop->oparg,
next_uop->operand,
next_uop->target,
(int)(stack_pointer - _PyFrame_Stackbase(frame)));
next_uop++;
OPT_STAT_INC(uops_executed);
UOP_STAT_INC(opcode, execution_count);
UOP_STAT_INC(uopcode, execution_count);
#ifdef Py_STATS
trace_uop_execution_counter++;
#endif

switch (opcode) {
switch (uopcode) {

#include "executor_cases.c.h"

Expand Down Expand Up @@ -1053,7 +1052,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
STACK_SHRINK(1);
error_tier_two:
DPRINTF(2, "Error: [Uop %d (%s), oparg %d, operand %" PRIu64 ", target %d @ %d]\n",
opcode, _PyUopName(opcode), next_uop[-1].oparg, next_uop[-1].operand, next_uop[-1].target,
uopcode, _PyUopName(uopcode), next_uop[-1].oparg, next_uop[-1].operand, next_uop[-1].target,
(int)(next_uop - current_executor->trace - 1));
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
frame->return_offset = 0; // Don't leave this random
Expand All @@ -1066,10 +1065,10 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
// On DEOPT_IF we just repeat the last instruction.
// This presumes nothing was popped from the stack (nor pushed).
DPRINTF(2, "DEOPT: [Uop %d (%s), oparg %d, operand %" PRIu64 ", target %d @ %d]\n",
opcode, _PyUopName(opcode), next_uop[-1].oparg, next_uop[-1].operand, next_uop[-1].target,
uopcode, _PyUopName(uopcode), next_uop[-1].oparg, next_uop[-1].operand, next_uop[-1].target,
(int)(next_uop - current_executor->trace - 1));
OPT_HIST(trace_uop_execution_counter, trace_run_length_hist);
UOP_STAT_INC(opcode, miss);
UOP_STAT_INC(uopcode, miss);
frame->return_offset = 0; // Dispatch to frame->instr_ptr
_PyFrame_SetStackPointer(frame, stack_pointer);
frame->instr_ptr = next_uop[-1].target + _PyCode_CODE(_PyFrame_GetCode(frame));
Expand Down

0 comments on commit f362f9a

Please sign in to comment.