Skip to content

Commit d7361d6

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 e2d9541 commit d7361d6

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
@@ -2825,17 +2825,24 @@ _PyObject_DebugMallocStats(FILE *out)
28252825
# error "arena size must be < 2^32"
28262826
#endif
28272827

2828+
/* Current 64-bit processors are limited to 48-bit physical addresses. For
2829+
* now, the top 17 bits of addresses will all be equal to bit 2**47. If that
2830+
* changes in the future, this must be adjusted upwards.
2831+
*/
2832+
#define PHYSICAL_BITS 48
2833+
28282834
/* bits used for MAP1 and MAP2 nodes */
2829-
#define INTERIOR_BITS ((BITS - ARENA_BITS + 2) / 3)
2835+
#define INTERIOR_BITS ((PHYSICAL_BITS - ARENA_BITS + 2) / 3)
28302836

28312837
#define MAP1_BITS INTERIOR_BITS
28322838
#define MAP1_LENGTH (1 << MAP1_BITS)
2839+
#define MAP1_MASK (MAP3_LENGTH - 1)
28332840

28342841
#define MAP2_BITS INTERIOR_BITS
28352842
#define MAP2_LENGTH (1 << MAP2_BITS)
28362843
#define MAP2_MASK (MAP2_LENGTH - 1)
28372844

2838-
#define MAP3_BITS (BITS - ARENA_BITS - 2*INTERIOR_BITS)
2845+
#define MAP3_BITS (PHYSICAL_BITS - ARENA_BITS - 2*INTERIOR_BITS)
28392846
#define MAP3_LENGTH (1 << MAP3_BITS)
28402847
#define MAP3_MASK (MAP3_LENGTH - 1)
28412848

@@ -2846,7 +2853,8 @@ _PyObject_DebugMallocStats(FILE *out)
28462853
#define AS_UINT(p) ((uintptr_t)(p))
28472854
#define MAP3_INDEX(p) ((AS_UINT(p) >> MAP3_SHIFT) & MAP3_MASK)
28482855
#define MAP2_INDEX(p) ((AS_UINT(p) >> MAP2_SHIFT) & MAP2_MASK)
2849-
#define MAP1_INDEX(p) (AS_UINT(p) >> MAP1_SHIFT)
2856+
#define MAP1_INDEX(p) ((AS_UINT(p) >> MAP1_SHIFT) & MAP1_MASK)
2857+
#define HIGH_BITS(p) (AS_UINT(p) >> PHYSICAL_BITS)
28502858

28512859
/* See arena_map_mark_used() for the meaning of these members. */
28522860
typedef struct {
@@ -2882,6 +2890,8 @@ static arena_map1_t arena_map_root;
28822890
static arena_map3_t *
28832891
arena_map_get(block *p, int create)
28842892
{
2893+
/* sanity check that PHYSICAL_BITS is correct */
2894+
assert(HIGH_BITS(p) == HIGH_BITS(&arena_map_root));
28852895
int i1 = MAP1_INDEX(p);
28862896
if (arena_map_root.ptrs[i1] == NULL) {
28872897
if (!create) {
@@ -2936,6 +2946,8 @@ arena_map_get(block *p, int create)
29362946
static int
29372947
arena_map_mark_used(uintptr_t arena_base, int is_used)
29382948
{
2949+
/* sanity check that PHYSICAL_BITS is correct */
2950+
assert(HIGH_BITS(arena_base) == HIGH_BITS(&arena_map_root));
29392951
arena_map3_t *n_hi = arena_map_get((block *)arena_base, is_used);
29402952
if (n_hi == NULL) {
29412953
assert(is_used); /* otherwise node should already exist */

0 commit comments

Comments
 (0)