summaryrefslogtreecommitdiffstats
path: root/Include/cpython
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2024-04-04 15:03:27 (GMT)
committerGitHub <noreply@github.com>2024-04-04 15:03:27 (GMT)
commit060a96f1a9a901b01ed304aa82b886d248ca1cb6 (patch)
treecb3e95ecac1f90440b7d3752c4aad015ea734bf0 /Include/cpython
parent63bbe77d9bb2be4db83ed09b96dd22f2a44ef55b (diff)
downloadcpython-060a96f1a9a901b01ed304aa82b886d248ca1cb6.zip
cpython-060a96f1a9a901b01ed304aa82b886d248ca1cb6.tar.gz
cpython-060a96f1a9a901b01ed304aa82b886d248ca1cb6.tar.bz2
gh-116968: Reimplement Tier 2 counters (#117144)
Introduce a unified 16-bit backoff counter type (``_Py_BackoffCounter``), shared between the Tier 1 adaptive specializer and the Tier 2 optimizer. The API used for adaptive specialization counters is changed but the behavior is (supposed to be) identical. The behavior of the Tier 2 counters is changed: - There are no longer dynamic thresholds (we never varied these). - All counters now use the same exponential backoff. - The counter for ``JUMP_BACKWARD`` starts counting down from 16. - The ``temperature`` in side exits starts counting down from 64.
Diffstat (limited to 'Include/cpython')
-rw-r--r--Include/cpython/code.h11
-rw-r--r--Include/cpython/optimizer.h15
2 files changed, 12 insertions, 14 deletions
diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index d5dac17..b0e226e 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -24,6 +24,16 @@ typedef struct _Py_GlobalMonitors {
uint8_t tools[_PY_MONITORING_UNGROUPED_EVENTS];
} _Py_GlobalMonitors;
+typedef struct {
+ union {
+ struct {
+ uint16_t backoff : 4;
+ uint16_t value : 12;
+ };
+ uint16_t as_counter; // For printf("%#x", ...)
+ };
+} _Py_BackoffCounter;
+
/* Each instruction in a code object is a fixed-width value,
* currently 2 bytes: 1-byte opcode + 1-byte oparg. The EXTENDED_ARG
* opcode allows for larger values but the current limit is 3 uses
@@ -39,6 +49,7 @@ typedef union {
uint8_t code;
uint8_t arg;
} op;
+ _Py_BackoffCounter counter; // First cache entry of specializable op
} _Py_CODEUNIT;
diff --git a/Include/cpython/optimizer.h b/Include/cpython/optimizer.h
index bc960c5..819251a 100644
--- a/Include/cpython/optimizer.h
+++ b/Include/cpython/optimizer.h
@@ -89,7 +89,7 @@ static inline uint16_t uop_get_error_target(const _PyUOpInstruction *inst)
typedef struct _exit_data {
uint32_t target;
- int16_t temperature;
+ _Py_BackoffCounter temperature;
const struct _PyExecutorObject *executor;
} _PyExitData;
@@ -115,11 +115,6 @@ typedef int (*optimize_func)(
struct _PyOptimizerObject {
PyObject_HEAD
optimize_func optimize;
- /* These thresholds are treated as signed so do not exceed INT16_MAX
- * Use INT16_MAX to indicate that the optimizer should never be called */
- uint16_t resume_threshold;
- uint16_t side_threshold;
- uint16_t backedge_threshold;
/* Data needed by the optimizer goes here, but is opaque to the VM */
};
@@ -151,14 +146,6 @@ extern void _Py_Executors_InvalidateAll(PyInterpreterState *interp, int is_inval
PyAPI_FUNC(PyObject *)PyUnstable_Optimizer_NewCounter(void);
PyAPI_FUNC(PyObject *)PyUnstable_Optimizer_NewUOpOptimizer(void);
-#define OPTIMIZER_BITS_IN_COUNTER 4
-/* Minimum of 16 additional executions before retry */
-#define MIN_TIER2_BACKOFF 4
-#define MAX_TIER2_BACKOFF (15 - OPTIMIZER_BITS_IN_COUNTER)
-#define OPTIMIZER_BITS_MASK ((1 << OPTIMIZER_BITS_IN_COUNTER) - 1)
-/* A value <= UINT16_MAX but large enough that when shifted is > UINT16_MAX */
-#define OPTIMIZER_UNREACHABLE_THRESHOLD UINT16_MAX
-
#define _Py_MAX_ALLOWED_BUILTINS_MODIFICATIONS 3
#define _Py_MAX_ALLOWED_GLOBALS_MODIFICATIONS 6