Skip to content

Commit b99fe61

Browse files
committed
optimize some constants
1 parent 21f7122 commit b99fe61

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ extern "C" {
1818
/* Default fitness configuration values for trace quality control.
1919
* FITNESS_INITIAL and FITNESS_INITIAL_SIDE can be overridden via
2020
* PYTHON_JIT_FITNESS_INITIAL and PYTHON_JIT_FITNESS_INITIAL_SIDE */
21-
#define FITNESS_PER_INSTRUCTION 2
22-
#define FITNESS_INITIAL 2000
23-
#define FITNESS_INITIAL_SIDE 800
24-
#define FITNESS_BRANCH_BASE 5
25-
#define FITNESS_BACKWARD_EDGE 80
21+
#define FITNESS_PER_INSTRUCTION 2
22+
#define FITNESS_BRANCH_BASE 5
23+
#define FITNESS_INITIAL (FITNESS_PER_INSTRUCTION * 1000)
24+
#define FITNESS_INITIAL_SIDE (FITNESS_INITIAL / 2)
25+
#define FITNESS_BACKWARD_EDGE (FITNESS_PER_INSTRUCTION / 10)
2626

2727
/* Exit quality constants for fitness-based trace termination.
2828
* Higher values mean better places to stop the trace. */
29-
#define EXIT_QUALITY_CLOSE_LOOP 800
30-
#define EXIT_QUALITY_ENTER_EXECUTOR 500
29+
3130
#define EXIT_QUALITY_DEFAULT 200
32-
#define EXIT_QUALITY_SPECIALIZABLE 50
31+
#define EXIT_QUALITY_CLOSE_LOOP (4 * EXIT_QUALITY_DEFAULT)
32+
#define EXIT_QUALITY_ENTER_EXECUTOR (2 * EXIT_QUALITY_DEFAULT + 100)
33+
#define EXIT_QUALITY_SPECIALIZABLE (EXIT_QUALITY_DEFAULT / 4)
3334

3435

3536
typedef struct _PyJitUopBuffer {

Python/optimizer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,10 @@ compute_branch_penalty(uint16_t history, bool branch_taken)
605605
int taken_count = _Py_popcount32((uint32_t)history);
606606
int on_trace_count = branch_taken ? taken_count : 16 - taken_count;
607607
int off_trace = 16 - on_trace_count;
608-
/* Quadratic scaling: off_trace^2 ranges from 0 (fully biased our way)
609-
* to 256 (fully biased against us, e.g. 15/16 left but traced right). */
610-
return FITNESS_BRANCH_BASE + off_trace * off_trace;
608+
/* Linear scaling: off_trace ranges from 0 (fully biased our way)
609+
* to 16 (fully biased against us), so the penalty ranges from
610+
* FITNESS_BRANCH_BASE to FITNESS_BRANCH_BASE + 48. */
611+
return FITNESS_BRANCH_BASE + off_trace * 2;
611612
}
612613

613614
/* Compute exit quality for the current trace position.

0 commit comments

Comments
 (0)