Skip to content

Commit b7c3995

Browse files
committed
Make sure that the uses of instruction index versus uses of nexti (index+1) is clear.
1 parent 55e673b commit b7c3995

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

Include/internal/pycore_code.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ _GetSpecializedCacheEntry(_Py_CODEUNIT *first_instr, Py_ssize_t n)
8282
/* Following two functions form a pair.
8383
*
8484
* oparg_from_offset_and_index() is used to compute the oparg
85-
* when quickening, so that offset_from_oparg_and_index()
85+
* when quickening, so that offset_from_oparg_and_nexti()
8686
* can be used at runtime to compute the offset.
8787
*
8888
* The relationship between the three values is currently
@@ -103,25 +103,27 @@ _GetSpecializedCacheEntry(_Py_CODEUNIT *first_instr, Py_ssize_t n)
103103
*
104104
*/
105105
static inline int
106-
oparg_from_offset_and_index(int offset, int index)
106+
oparg_from_offset_and_nexti(int offset, int nexti)
107107
{
108-
return offset-(index>>1);
108+
return offset-(nexti>>1);
109109
}
110110

111111
static inline int
112-
offset_from_oparg_and_index(int oparg, int index)
112+
offset_from_oparg_and_nexti(int oparg, int nexti)
113113
{
114-
return (index>>1)+oparg;
114+
return (nexti>>1)+oparg;
115115
}
116116

117117
/* Get pointer to the cache entry associated with an instruction.
118-
This doesn't check that an entry has been allocated for that instruction. */
118+
* nexti is the index of the instruction plus one.
119+
* nexti is used as it corresponds to the instruction pointer in the interpreter.
120+
* This doesn't check that an entry has been allocated for that instruction. */
119121
static inline SpecializedCacheEntry *
120-
_GetSpecializedCacheEntryForInstruction(_Py_CODEUNIT *first_instr, int index, int oparg)
122+
_GetSpecializedCacheEntryForInstruction(_Py_CODEUNIT *first_instr, int nexti, int oparg)
121123
{
122124
return _GetSpecializedCacheEntry(
123125
first_instr,
124-
offset_from_oparg_and_index(oparg, index)
126+
offset_from_oparg_and_nexti(oparg, nexti)
125127
);
126128
}
127129

Python/specialize.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,20 @@ static uint8_t cache_requirements[256] = { 0 };
7171
* instruction index and oparg are related */
7272
static int
7373
oparg_from_instruction_and_update_offset(int index, int opcode, int original_oparg, int *cache_offset) {
74+
/* The instruction pointer in the interpreter points to the next
75+
* instruction, so we compute the offset using nexti (index + 1) */
76+
int nexti = index + 1;
7477
uint8_t need = cache_requirements[opcode];
7578
if (need == 0) {
7679
return original_oparg;
7780
}
7881
assert(adaptive_opcodes[opcode] != 0);
79-
int oparg = oparg_from_offset_and_index(*cache_offset, index);
80-
assert(*cache_offset == offset_from_oparg_and_index(oparg, index));
81-
/* Some cache space is wasted here as the minimum possible offset is (index>>1) */
82+
int oparg = oparg_from_offset_and_nexti(*cache_offset, nexti);
83+
assert(*cache_offset == offset_from_oparg_and_nexti(oparg, nexti));
84+
/* Some cache space is wasted here as the minimum possible offset is (nexti>>1) */
8285
if (oparg < 0) {
8386
oparg = 0;
84-
*cache_offset = offset_from_oparg_and_index(oparg, index);
87+
*cache_offset = offset_from_oparg_and_nexti(oparg, nexti);
8588
}
8689
else if (oparg > 255) {
8790
return -1;
@@ -105,7 +108,6 @@ entries_needed(_Py_CODEUNIT *code, int len)
105108
return cache_offset + 1; // One extra for the count entry
106109
}
107110

108-
109111
static inline _Py_CODEUNIT *
110112
first_instruction(SpecializedCacheOrInstruction *quickened)
111113
{
@@ -125,15 +127,12 @@ optimize(SpecializedCacheOrInstruction *quickened, int len)
125127
int cache_offset = 0;
126128
int previous_opcode = -1;
127129
for(int i = 0; i < len; i++) {
128-
/* The instruction pointer in the intperpreter points to the next
129-
* instruction. We want to use index+1 for efficiency. */
130-
int nexti = i + 1;
131130
int opcode = _Py_OPCODE(instructions[i]);
132131
int oparg = _Py_OPARG(instructions[i]);
133132
uint8_t adaptive_opcode = adaptive_opcodes[opcode];
134133
if (adaptive_opcode && previous_opcode != EXTENDED_ARG) {
135134
int new_oparg = oparg_from_instruction_and_update_offset(
136-
nexti, opcode, oparg, &cache_offset
135+
i, opcode, oparg, &cache_offset
137136
);
138137
if (new_oparg < 0) {
139138
/* Not possible to allocate a cache for this instruction */

0 commit comments

Comments
 (0)