Skip to content

Commit e0601c1

Browse files
committed
Merge branch 'main' into func-watchers
2 parents e1dd4e4 + 00ee6d5 commit e0601c1

File tree

92 files changed

+1525
-1921
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1525
-1921
lines changed

Doc/library/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ as a collection of packages, so it may be necessary to use the packaging
2727
tools provided with the operating system to obtain some or all of the
2828
optional components.
2929

30-
In addition to the standard library, there is a growing collection of
31-
several thousand components (from individual programs and modules to
30+
In addition to the standard library, there is an active collection of
31+
hundreds of thousands of components (from individual programs and modules to
3232
packages and entire application development frameworks), available from
3333
the `Python Package Index <https://pypi.org>`_.
3434

Include/internal/pycore_code.h

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ typedef struct {
9191

9292
#define INLINE_CACHE_ENTRIES_FOR_ITER CACHE_ENTRIES(_PyForIterCache)
9393

94-
extern uint8_t _PyOpcode_Adaptive[256];
95-
9694
// Borrowed references to common callables:
9795
struct callable_cache {
9896
PyObject *isinstance;
@@ -219,11 +217,14 @@ extern int _Py_Specialize_LoadAttr(PyObject *owner, _Py_CODEUNIT *instr,
219217
PyObject *name);
220218
extern int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr,
221219
PyObject *name);
222-
extern int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name);
223-
extern int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);
224-
extern int _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr);
225-
extern int _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr,
226-
int nargs, PyObject *kwnames);
220+
extern void _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins,
221+
_Py_CODEUNIT *instr, PyObject *name);
222+
extern void _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container,
223+
_Py_CODEUNIT *instr);
224+
extern void _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub,
225+
_Py_CODEUNIT *instr);
226+
extern void _Py_Specialize_Call(PyObject *callable, _Py_CODEUNIT *instr,
227+
int nargs, PyObject *kwnames);
227228
extern void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
228229
int oparg, PyObject **locals);
229230
extern void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs,
@@ -377,8 +378,22 @@ write_location_entry_start(uint8_t *ptr, int code, int length)
377378

378379
/* With a 16-bit counter, we have 12 bits for the counter value, and 4 bits for the backoff */
379380
#define ADAPTIVE_BACKOFF_BITS 4
380-
/* The initial counter value is 1 == 2**ADAPTIVE_BACKOFF_START - 1 */
381-
#define ADAPTIVE_BACKOFF_START 1
381+
382+
// A value of 1 means that we attempt to specialize the *second* time each
383+
// instruction is executed. Executing twice is a much better indicator of
384+
// "hotness" than executing once, but additional warmup delays only prevent
385+
// specialization. Most types stabilize by the second execution, too:
386+
#define ADAPTIVE_WARMUP_VALUE 1
387+
#define ADAPTIVE_WARMUP_BACKOFF 1
388+
389+
// A value of 52 means that we attempt to re-specialize after 53 misses (a prime
390+
// number, useful for avoiding artifacts if every nth value is a different type
391+
// or something). Setting the backoff to 0 means that the counter is reset to
392+
// the same state as a warming-up instruction (value == 1, backoff == 1) after
393+
// deoptimization. This isn't strictly necessary, but it is bit easier to reason
394+
// about when thinking about the opcode transitions as a state machine:
395+
#define ADAPTIVE_COOLDOWN_VALUE 52
396+
#define ADAPTIVE_COOLDOWN_BACKOFF 0
382397

383398
#define MAX_BACKOFF_VALUE (16 - ADAPTIVE_BACKOFF_BITS)
384399

@@ -390,9 +405,15 @@ adaptive_counter_bits(int value, int backoff) {
390405
}
391406

392407
static inline uint16_t
393-
adaptive_counter_start(void) {
394-
unsigned int value = (1 << ADAPTIVE_BACKOFF_START) - 1;
395-
return adaptive_counter_bits(value, ADAPTIVE_BACKOFF_START);
408+
adaptive_counter_warmup(void) {
409+
return adaptive_counter_bits(ADAPTIVE_WARMUP_VALUE,
410+
ADAPTIVE_WARMUP_BACKOFF);
411+
}
412+
413+
static inline uint16_t
414+
adaptive_counter_cooldown(void) {
415+
return adaptive_counter_bits(ADAPTIVE_COOLDOWN_VALUE,
416+
ADAPTIVE_COOLDOWN_BACKOFF);
396417
}
397418

398419
static inline uint16_t
@@ -435,6 +456,16 @@ _PyCode_LineNumberFromArray(PyCodeObject *co, int index)
435456
}
436457
}
437458

459+
typedef struct _PyShimCodeDef {
460+
const uint8_t *code;
461+
int codelen;
462+
int stacksize;
463+
const char *cname;
464+
} _PyShimCodeDef;
465+
466+
extern PyCodeObject *
467+
_Py_MakeShimCode(const _PyShimCodeDef *code);
468+
438469

439470
#ifdef __cplusplus
440471
}

Include/internal/pycore_frame.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ typedef enum _framestate {
4242
enum _frameowner {
4343
FRAME_OWNED_BY_THREAD = 0,
4444
FRAME_OWNED_BY_GENERATOR = 1,
45-
FRAME_OWNED_BY_FRAME_OBJECT = 2
45+
FRAME_OWNED_BY_FRAME_OBJECT = 2,
46+
FRAME_OWNED_BY_CSTACK = 3,
4647
};
4748

4849
typedef struct _PyInterpreterFrame {
4950
/* "Specials" section */
50-
PyObject *f_funcobj; /* Strong reference */
51-
PyObject *f_globals; /* Borrowed reference */
52-
PyObject *f_builtins; /* Borrowed reference */
53-
PyObject *f_locals; /* Strong reference, may be NULL */
51+
PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
52+
PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
53+
PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
54+
PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
5455
PyCodeObject *f_code; /* Strong reference */
55-
PyFrameObject *frame_obj; /* Strong reference, may be NULL */
56+
PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
5657
/* Linkage section */
5758
struct _PyInterpreterFrame *previous;
5859
// NOTE: This is not necessarily the last instruction started in the given
5960
// frame. Rather, it is the code unit *prior to* the *next* instruction. For
6061
// example, it may be an inline CACHE entry, an instruction we just jumped
6162
// over, or (in the case of a newly-created frame) a totally invalid value:
6263
_Py_CODEUNIT *prev_instr;
63-
int stacktop; /* Offset of TOS from localsplus */
64+
int stacktop; /* Offset of TOS from localsplus */
6465
uint16_t yield_offset;
65-
bool is_entry; // Whether this is the "root" frame for the current _PyCFrame.
6666
char owner;
6767
/* Locals and stack */
6868
PyObject *localsplus[1];
@@ -110,7 +110,6 @@ _PyFrame_InitializeSpecials(
110110
frame->stacktop = code->co_nlocalsplus;
111111
frame->frame_obj = NULL;
112112
frame->prev_instr = _PyCode_CODE(code) - 1;
113-
frame->is_entry = false;
114113
frame->yield_offset = 0;
115114
frame->owner = FRAME_OWNED_BY_THREAD;
116115
}

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct _Py_global_strings {
4848
STRUCT_FOR_STR(newline, "\n")
4949
STRUCT_FOR_STR(open_br, "{")
5050
STRUCT_FOR_STR(percent, "%")
51+
STRUCT_FOR_STR(shim_name, "<shim>")
5152
STRUCT_FOR_STR(utf_8, "utf-8")
5253
} literals;
5354

Include/internal/pycore_interp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ struct _is {
188188
struct ast_state ast;
189189
struct types_state types;
190190
struct callable_cache callable_cache;
191+
PyCodeObject *interpreter_trampoline;
191192

192193
/* The following fields are here to avoid allocation during init.
193194
The data is exposed through PyInterpreterState pointer fields.

0 commit comments

Comments
 (0)