Skip to content

Commit 8e24f1c

Browse files
committed
Add per-size freelists (as yet unused)
1 parent 4b36668 commit 8e24f1c

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

Include/internal/pycore_freelist_state.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
12+
/*
13+
* Alignment of addresses returned to the user. 2 word alignment works
14+
* on most current architectures (with 32-bit or 64-bit address buses).
15+
* The alignment value is also used for grouping small requests in size
16+
* classes spaced ALIGNMENT bytes apart.
17+
*
18+
* Don't change this. Lots of code implicitly relies on it.
19+
*/
20+
21+
#if SIZEOF_VOID_P > 4
22+
#define ALIGNMENT 16 /* must be 2^N */
23+
#define ALIGNMENT_SHIFT 4
24+
#else
25+
#define ALIGNMENT 8 /* must be 2^N */
26+
#define ALIGNMENT_SHIFT 3
27+
#endif
28+
29+
30+
/*
31+
* Max size threshold below which malloc requests are considered to be
32+
* small enough in order to use freelists. You can tune
33+
* this value according to your application behaviour and memory needs.
34+
*
35+
* Note: a size threshold of 512 guarantees that newly created dictionaries
36+
* will be allocated from freelists or preallocated memory pools on 64-bit.
37+
*
38+
* The following invariants must hold:
39+
* 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 512
40+
* 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
41+
*
42+
* Although not required, for better performance and space efficiency,
43+
* it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
44+
*/
45+
#define SMALL_REQUEST_THRESHOLD 512
46+
#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
47+
1148
# define PyTuple_MAXSAVESIZE 20 // Largest tuple to save on freelist
1249
# define Py_tuple_MAXFREELIST 2000 // Maximum number of tuples of each size to save
1350
# define Py_lists_MAXFREELIST 80
@@ -43,6 +80,7 @@ struct _Py_freelist {
4380
};
4481

4582
struct _Py_freelists {
83+
struct _Py_freelist by_size[NB_SMALL_SIZE_CLASSES];
4684
struct _Py_freelist floats;
4785
struct _Py_freelist ints;
4886
struct _Py_freelist tuples[PyTuple_MAXSAVESIZE];

Include/internal/pycore_obmalloc.h

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_freelist_state.h" // NB_SMALL_SIZE_CLASSES
1112

1213
typedef unsigned int pymem_uint; /* assuming >= 16 bits */
1314

@@ -119,43 +120,9 @@ typedef unsigned int pymem_uint; /* assuming >= 16 bits */
119120
* -- Main tunable settings section --
120121
*/
121122

122-
/*
123-
* Alignment of addresses returned to the user. 8-bytes alignment works
124-
* on most current architectures (with 32-bit or 64-bit address buses).
125-
* The alignment value is also used for grouping small requests in size
126-
* classes spaced ALIGNMENT bytes apart.
127-
*
128-
* You shouldn't change this unless you know what you are doing.
129-
*/
130-
131-
#if SIZEOF_VOID_P > 4
132-
#define ALIGNMENT 16 /* must be 2^N */
133-
#define ALIGNMENT_SHIFT 4
134-
#else
135-
#define ALIGNMENT 8 /* must be 2^N */
136-
#define ALIGNMENT_SHIFT 3
137-
#endif
138-
139123
/* Return the number of bytes in size class I, as a uint. */
140124
#define INDEX2SIZE(I) (((pymem_uint)(I) + 1) << ALIGNMENT_SHIFT)
141125

142-
/*
143-
* Max size threshold below which malloc requests are considered to be
144-
* small enough in order to use preallocated memory pools. You can tune
145-
* this value according to your application behaviour and memory needs.
146-
*
147-
* Note: a size threshold of 512 guarantees that newly created dictionaries
148-
* will be allocated from preallocated memory pools on 64-bit.
149-
*
150-
* The following invariants must hold:
151-
* 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 512
152-
* 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
153-
*
154-
* Although not required, for better performance and space efficiency,
155-
* it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
156-
*/
157-
#define SMALL_REQUEST_THRESHOLD 512
158-
#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
159126

160127
/*
161128
* The system's VMM page size can be obtained on most unices with a

0 commit comments

Comments
 (0)