Skip to content

Commit b33d636

Browse files
committed
Exploit current hardware physical address bits
Current 64-bit chips only have 48 bits of physical addresses. Exploit that to further shrink the size of the radix tree.
1 parent c52c610 commit b33d636

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Objects/obmalloc.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,17 +2831,24 @@ _PyObject_DebugMallocStats(FILE *out)
28312831
# error "arena size must be < 2^32"
28322832
#endif
28332833

2834+
/* Current 64-bit processors are limited to 48-bit physical addresses. For
2835+
* now, the top 17 bits of addresses will all be equal to bit 2**47. If that
2836+
* changes in the future, this must be adjusted upwards.
2837+
*/
2838+
#define PHYSICAL_BITS 48
2839+
28342840
/* bits used for MAP1 and MAP2 nodes */
2835-
#define INTERIOR_BITS ((BITS - ARENA_BITS + 2) / 3)
2841+
#define INTERIOR_BITS ((PHYSICAL_BITS - ARENA_BITS + 2) / 3)
28362842

28372843
#define MAP1_BITS INTERIOR_BITS
28382844
#define MAP1_LENGTH (1 << MAP1_BITS)
2845+
#define MAP1_MASK (MAP3_LENGTH - 1)
28392846

28402847
#define MAP2_BITS INTERIOR_BITS
28412848
#define MAP2_LENGTH (1 << MAP2_BITS)
28422849
#define MAP2_MASK (MAP2_LENGTH - 1)
28432850

2844-
#define MAP3_BITS (BITS - ARENA_BITS - 2*INTERIOR_BITS)
2851+
#define MAP3_BITS (PHYSICAL_BITS - ARENA_BITS - 2*INTERIOR_BITS)
28452852
#define MAP3_LENGTH (1 << MAP3_BITS)
28462853
#define MAP3_MASK (MAP3_LENGTH - 1)
28472854

@@ -2852,7 +2859,8 @@ _PyObject_DebugMallocStats(FILE *out)
28522859
#define AS_UINT(p) ((uintptr_t)(p))
28532860
#define MAP3_INDEX(p) ((AS_UINT(p) >> MAP3_SHIFT) & MAP3_MASK)
28542861
#define MAP2_INDEX(p) ((AS_UINT(p) >> MAP2_SHIFT) & MAP2_MASK)
2855-
#define MAP1_INDEX(p) (AS_UINT(p) >> MAP1_SHIFT)
2862+
#define MAP1_INDEX(p) ((AS_UINT(p) >> MAP1_SHIFT) & MAP1_MASK)
2863+
#define HIGH_BITS(p) (AS_UINT(p) >> PHYSICAL_BITS)
28562864

28572865
/* See arena_map_mark_used() for the meaning of these members. */
28582866
typedef struct {
@@ -2888,6 +2896,8 @@ static arena_map1_t arena_map_root;
28882896
static arena_map3_t *
28892897
arena_map_get(block *p, int create)
28902898
{
2899+
/* sanity check that PHYSICAL_BITS is correct */
2900+
assert(HIGH_BITS(p) == HIGH_BITS(&arena_map_root));
28912901
int i1 = MAP1_INDEX(p);
28922902
if (arena_map_root.ptrs[i1] == NULL) {
28932903
if (!create) {
@@ -2942,6 +2952,8 @@ arena_map_get(block *p, int create)
29422952
static int
29432953
arena_map_mark_used(uintptr_t arena_base, int is_used)
29442954
{
2955+
/* sanity check that PHYSICAL_BITS is correct */
2956+
assert(HIGH_BITS(arena_base) == HIGH_BITS(&arena_map_root));
29452957
arena_map3_t *n_hi = arena_map_get((block *)arena_base, is_used);
29462958
if (n_hi == NULL) {
29472959
assert(is_used); /* otherwise node should already exist */

0 commit comments

Comments
 (0)